Skip to content

Commit

Permalink
Specs for: Add __traits(getCmdlineConstant)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernesto Castellotti authored May 24, 2023
1 parent a4a0701 commit 587004f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions spec/traits.dd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ $(GNAME TraitsKeyword):
$(RELATIVE_LINK2 getOverloads, $(D getOverloads))
$(RELATIVE_LINK2 getParameterStorageClasses, $(D getParameterStorageClasses))
$(RELATIVE_LINK2 getPointerBitmap, $(D getPointerBitmap))
$(RELATIVE_LINK2 getCppNamespaces, $(D getCmdlineConstant))
$(RELATIVE_LINK2 getCppNamespaces, $(D getCppNamespaces))
$(RELATIVE_LINK2 getVisibility, $(D getVisibility))
$(RELATIVE_LINK2 getProtection, $(D getProtection))
Expand Down Expand Up @@ -1417,6 +1418,31 @@ bar(T)()
bar(int n)
)

$(H3 $(GNAME getCmdlineConstant))
$(P The first argument is a string representing the name of the constant, the optional second argument
is a type or false, the optional third argument is an expression and is used as default in case the constant
has not been passed to the compiler.

The name of the constant must contain the name of the fully qualified module, otherwise it will be added implicitly

If the second argument contains a type then the constant value will be returned after evaluation if the type of
the expression defined in the constant is equal to or implicitly convertible with the type passed to the second argument
If the second argument is not present or false then the value of the constant will be returned as a string without any evaluation
)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
# compiler defined constans: mymod.test1=some_text, mymod.test2=4

module mymod;
pragma(msg, __traits(getCmdlineConstant, "test1")); // Prints "some_text"
pragma(msg, __traits(getCmdlineConstant, "mymod.test1")); // Prints "some_text"
pragma(msg, __traits(getCmdlineConstant, "test2", false)); // Prints string "4"
pragma(msg, __traits(getCmdlineConstant, "test2", int)); // Prints integer 4
pragma(msg, __traits(getCmdlineConstant, "test3", int, 2)); // Constant not passed to dmd --> fallback default -> Prints integer 2
---
)

$(H3 $(GNAME getCppNamespaces))
$(P The argument is a symbol.
The result is a *ValueSeq* of strings, possibly empty, that correspond to the namespaces the symbol resides in.
Expand Down

0 comments on commit 587004f

Please sign in to comment.