You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A getStrDefine (placeholder name) magic proc that gives the value of a define. Roughly const foo = getStrDefine(foo, default = "abc") is equal to const foo {.strdefine.} = "abc".
Motivation
The fact that strdefine is tied to constants can cause weird behavior. Its implementation is also pretty similar to a magic proc.
Using a name other than the constant name is easier here for the implementation ({.strdefine.} form would need nim-lang/Nim#20115 to be merged).
The multiple hardcoded variants of strdefine, namely booldefine and intdefine, are also not needed in this form. One can do:
templategetDefineAs[T](name: untyped, _: type T, default: T): T =whendefined(name):
T(getStrDefine(name, "unreachable"))
else:
default
const foo =getDefineAs(foo, bool, true)
One can also use custom converters this way rather than normal type conversion. This is not possible with booldefine/intdefine.
Description
An implementation could be:
# in systemprocgetStrDefine*(name: untyped, defaultValue: string) {.magic: "StrDefine", noSideEffect, compileTime.}
We can leverage the already existing magic mStrDefine, normally only applicable to constants, for a proc here. In semMagic, we can add an of branch, something like:
of mStrDefine:
markUsed(c, n.info, s)
# move out as semStrDefinelet name =considerQuotedIdentOrDot(c, n[1], n).s
ifisDefined(c.config, name):
result=newStrNodeT(c.config.symbols[name], n, c)
else:
result= n[2]
Abstract
A
getStrDefine
(placeholder name) magic proc that gives the value of a define. Roughlyconst foo = getStrDefine(foo, default = "abc")
is equal toconst foo {.strdefine.} = "abc"
.Motivation
The fact that strdefine is tied to constants can cause weird behavior. Its implementation is also pretty similar to a magic proc.
Using a name other than the constant name is easier here for the implementation (
{.strdefine.}
form would need nim-lang/Nim#20115 to be merged).The multiple hardcoded variants of strdefine, namely booldefine and intdefine, are also not needed in this form. One can do:
One can also use custom converters this way rather than normal type conversion. This is not possible with booldefine/intdefine.
Description
An implementation could be:
We can leverage the already existing magic
mStrDefine
, normally only applicable to constants, for a proc here. In semMagic, we can add anof
branch, something like:Code Examples
From #181:
Backwards Compatibility
Overloading on the name
getStrDefine
might be affected.The text was updated successfully, but these errors were encountered: