pragma msg field name?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Tue Jun 27 05:03:01 UTC 2023
On Monday, June 26, 2023 10:25:13 PM MDT Chris Katko via Digitalmars-d-learn
wrote:
> inside a static foreach I can do
>
> ```
> enum rep;
>
> class myObject{
> int field1, field2, field3;
>
> static foreach(field; getSymbolsByUDA!(typeof(this), rep))
> {
> pragma(msg, field); // fails
> pragma(msg, fullyQualifiedName!field); // works
> }
> }
> ```
>
> error for pragma(msg, field)
> ```
> source/app.d(33,16): Error: value of `this` is not known at
> compile time
> source/app.d(33,4): while evaluating `pragma(msg, field)`
>
> [repeating for every variable in the class]
> ```
>
> How do I get just the field name? And why does it think this is a
> run-time value? I need to wrap it in some sort of template?
>
> All I see in std.traits docs are: fullyQualifiedName mangledName
> moduleName packageName
Well, on my machine, once the import for std.traits is added, that code
compiles but prints nothing, so I suspect that you paired it down too much
(likely related to the fact that none of the fields in question actually
have UDAs on them).
However, I would point out that getSymbolsByUDA gives you symbols, not
strings, whereas pragma(msg, ...) wants a string. fullyQualifiedName works,
because it results in a string. You can see what field is by using typeof on
it, but presumably, it's complaining about being a runtime value, because
when you use it, it's trying to evaluate the symbol (e.g. get the value of
field1). Using .stringof on field will give you a string, though I don't
know if it'll give you what you're looking for. In general, FieldNameTuple
would probably be what you would want for getting the names of the fields in
a struct or class, though obviously, that wouldn't be just getting the ones
with a specific UDA.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list