importutils.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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
  14. Foo = object
  15. f0: int # private
  16. Goo*[T] = object
  17. g0: int # private
  18. proc initFoo*(): auto = Foo()
  19. proc privateAccess*(t: typedesc) {.magic: "PrivateAccess".} =
  20. ## Enables access to private fields of `t` in current scope.
  21. runnableExamples("-d:nimImportutilsExample"):
  22. # here we're importing a module containing:
  23. # type
  24. # Foo = object
  25. # f0: int # private
  26. # Goo*[T] = object
  27. # g0: int # private
  28. # proc initFoo*(): auto = Foo()
  29. var f = initFoo()
  30. block:
  31. assert not compiles(f.f0)
  32. privateAccess(f.type)
  33. f.f0 = 1 # accessible in this scope
  34. block:
  35. assert f.f0 == 1 # still in scope
  36. assert not compiles(f.f0)
  37. # this also works with generics
  38. privateAccess(Goo)
  39. assert Goo[float](g0: 1).g0 == 1