diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2021-04-16 00:16:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-16 09:16:39 +0200 |
commit | 8161b02897a75c4b30593dbcc189cbd49d3832ea (patch) | |
tree | 399ca0d086a7e6917fae6c47c8dd899bd80f9102 /lib/std | |
parent | 12783dbcf0075492a3d090e4dc3ead3628c18a3a (diff) | |
download | Nim-8161b02897a75c4b30593dbcc189cbd49d3832ea.tar.gz |
`import foo {.all.}` reboot (#17706)
Diffstat (limited to 'lib/std')
-rw-r--r-- | lib/std/importutils.nim | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/std/importutils.nim b/lib/std/importutils.nim new file mode 100644 index 000000000..4efe0e140 --- /dev/null +++ b/lib/std/importutils.nim @@ -0,0 +1,34 @@ +##[ +Utilities related to import and symbol resolution. + +Experimental API, subject to change. +]## + +#[ +Possible future APIs: +* module symbols (https://github.com/nim-lang/Nim/pull/9560) +* whichModule (subsumes canImport / moduleExists) (https://github.com/timotheecour/Nim/issues/376) +* getCurrentPkgDir (https://github.com/nim-lang/Nim/pull/10530) +* import from a computed string + related APIs (https://github.com/nim-lang/Nim/pull/10527) +]# + +when defined(nimImportutilsExample): + type Foo = object + x1: int # private + proc initFoo*(): auto = Foo() + +proc privateAccess*(t: typedesc) {.magic: "PrivateAccess".} = + ## Enables access to private fields of `t` in current scope. + runnableExamples("-d:nimImportutilsExample"): + # here we're importing a module containing: + # type Foo = object + # x1: int # private + # proc initFoo*(): auto = Foo() + var a = initFoo() + block: + assert not compiles(a.x1) + privateAccess(a.type) + a.x1 = 1 # accessible in this scope + block: + assert a.x1 == 1 # still in scope + assert not compiles(a.x1) |