generic define pragma + string alias (#20979)

* generic `define` pragma + string alias

* clean

* add tests and document

* remove char/float, minimize changelog
This commit is contained in:
metagn 2022-12-13 23:20:55 +03:00 • committed by GitHub
commit 9a50033d5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 218 additions and 53 deletions

View file

@ -8049,6 +8049,24 @@ used. To see if a value was provided, `defined(FooBar)` can be used.
The syntax `-d:flag`:option: is actually just a shortcut for
`-d:flag=true`:option:.
These pragmas also accept an optional string argument for qualified
define names.
```nim
const FooBar {.intdefine: "package.FooBar".}: int = 5
echo FooBar
```
```cmd
nim c -d:package.FooBar=42 foobar.nim
```
This helps disambiguate define names in different packages.
See also the [generic `define` pragma](manual_experimental.html#generic-define-pragma)
for a version of these pragmas that detects the type of the define based on
the constant value.
User-defined pragmas
====================

View file

@ -65,6 +65,29 @@ However, a `void` type cannot be inferred in generic code:
The `void` type is only valid for parameters and return types; other symbols
cannot have the type `void`.
Generic `define` pragma
=======================
Aside the [typed define pragmas for constants](manual.html#implementation-specific-pragmas-compileminustime-define-pragmas),
there is a generic `{.define.}` pragma that interprets the value of the define
based on the type of the constant value.
```nim
const foo {.define: "package.foo".} = 123
const bar {.define: "package.bar".} = false
```
```cmd
nim c -d:package.foo=456 -d:package.bar foobar.nim
```
The following types are supported:
* `string` and `cstring`
* Signed and unsigned integer types
* `bool`
* Enums
Top-down type inference
=======================