importutils.nim 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. ##[
  2. Utilities related to import and symbol resolution.
  3. Experimental API, subject to change.
  4. ]##
  5. #[
  6. Possible future APIs:
  7. * module symbols (https://github.com/nim-lang/Nim/pull/9560)
  8. * whichModule (subsumes canImport / moduleExists) (https://github.com/timotheecour/Nim/issues/376)
  9. * getCurrentPkgDir (https://github.com/nim-lang/Nim/pull/10530)
  10. * import from a computed string + related APIs (https://github.com/nim-lang/Nim/pull/10527)
  11. ]#
  12. when defined(nimImportutilsExample):
  13. type Foo = object
  14. x1: int # private
  15. proc initFoo*(): auto = Foo()
  16. proc privateAccess*(t: typedesc[object|(ref object)|(ptr object)]) {.magic: "PrivateAccess".} =
  17. ## Enables access to private fields of `t` in current scope.
  18. runnableExamples("-d:nimImportutilsExample"):
  19. # here we're importing a module containing:
  20. # type Foo = object
  21. # x1: int # private
  22. # proc initFoo*(): auto = Foo()
  23. var a = initFoo()
  24. block:
  25. assert not compiles(a.x1)
  26. privateAccess(a.type)
  27. a.x1 = 1 # accessible in this scope
  28. block:
  29. assert a.x1 == 1 # still in scope
  30. assert not compiles(a.x1)