Source Edit

Utilities related to import and symbol resolution.

Experimental API, subject to change.

Procs

  1. proc privateAccess(t: typedesc) {.magic: "PrivateAccess", ...raises: [], tags: [],
  2. forbids: [].}

Enables access to private fields of t in current scope.

Example: cmd: -d:nimImportutilsExample

  1. # here we're importing a module containing:
  2. # type
  3. # Foo = object
  4. # f0: int # private
  5. # Goo*[T] = object
  6. # g0: int # private
  7. # proc initFoo*(): auto = Foo()
  8. var f = initFoo()
  9. block:
  10. assert not compiles(f.f0)
  11. privateAccess(f.type)
  12. f.f0 = 1 # accessible in this scope
  13. block:
  14. assert f.f0 == 1 # still in scope
  15. assert not compiles(f.f0)
  16. # this also works with generics
  17. privateAccess(Goo)
  18. assert Goo[float](g0: 1).g0 == 1

Source Edit