Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proc wrapper for strdefine #474

Closed
metagn opened this issue Aug 14, 2022 · 0 comments
Closed

Proc wrapper for strdefine #474

metagn opened this issue Aug 14, 2022 · 0 comments

Comments

@metagn
Copy link
Contributor

metagn commented Aug 14, 2022

Abstract

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:

template getDefineAs[T](name: untyped, _: type T, default: T): T =
  when defined(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 system
proc getStrDefine*(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 semStrDefine
  let name = considerQuotedIdentOrDot(c, n[1], n).s
  if isDefined(c.config, name):
    result = newStrNodeT(c.config.symbols[name], n, c)
  else:
    result = n[2]

Code Examples

From #181:

const bar = getStrDefine(myproj.mykey2, "foo2")

Backwards Compatibility

Overloading on the name getStrDefine might be affected.

@metagn metagn changed the title Proc wrapper for strdefine etc Proc wrapper for strdefine Aug 14, 2022
@metagn metagn closed this as completed Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant