Skip to content

Commit

Permalink
document
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 20, 2023
1 parent b85311f commit 467e02e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
35 changes: 35 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ slots when enlarging a sequence.
let (a, (b, c)): (byte, (float, cstring)) = (1, (2, "abc"))
```
- An experimental option `genericsOpenSym` has been added to allow captured
symbols in generic routine bodies to be replaced by symbols injected locally
by templates/macros at instantiation time. `bind` may be used to keep the
captured symbols over the injected ones regardless of enabling the option.
Since this change may affect runtime behavior, the experimental switch
`genericsOpenSym` needs to be enabled, and a warning is given in the case
where an injected symbol would replace a captured symbol not bound by `bind`
and the experimental switch isn't enabled.
```nim
const value = "captured"
template foo(x: int, body: untyped) =
let value {.inject.} = "injected"
body
proc old[T](): string =
foo(123):
return value # warning: a new `value` has been injected, use `bind` or turn on `experimental:genericsOpenSym`
echo old[int]() # "captured"
{.experimental: "genericsOpenSym".}
proc bar[T](): string =
foo(123):
return value
assert bar[int]() == "injected" # previously it would be "captured"
proc baz[T](): string =
bind value
foo(123):
return value
assert baz[int]() == "captured"
```

## Compiler changes

- `--nimcache` using a relative path as the argument in a config file is now relative to the config file instead of the current directory.
Expand Down
39 changes: 39 additions & 0 deletions doc/manual_experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -2520,3 +2520,42 @@ NimFunctor()(1)
```
Notice we use the overload of `()` to have the same semantics in Nim, but on the `importcpp` we import the functor as a function.
This allows to easy interop with functions that accepts for example a `const` operator in its signature.


Injected symbols in generic procs
=================================

With the experimental option `genericsOpenSym`, captured symbols in generic
routine bodies may be replaced by symbols injected locally by templates/macros
at instantiation time. `bind` may be used to keep the captured symbols over
the injected ones regardless of enabling the option.

Since this change may affect runtime behavior, the experimental switch
`genericsOpenSym` needs to be enabled, and a warning is given in the case
where an injected symbol would replace a captured symbol not bound by `bind`
and the experimental switch isn't enabled.
```nim
const value = "captured"
template foo(x: int, body: untyped) =
let value {.inject.} = "injected"
body
proc old[T](): string =
foo(123):
return value # warning: a new `value` has been injected, use `bind` or turn on `experimental:genericsOpenSym`
echo old[int]() # "captured"
{.experimental: "genericsOpenSym".}
proc bar[T](): string =
foo(123):
return value
assert bar[int]() == "injected" # previously it would be "captured"
proc baz[T](): string =
bind value
foo(123):
return value
assert baz[int]() == "captured"
```

0 comments on commit 467e02e

Please sign in to comment.