Weird behavior with UDAs
Adam D. Ruppe
destructionator at gmail.com
Sat Jun 13 13:08:28 UTC 2020
On Saturday, 13 June 2020 at 12:55:36 UTC, realhet wrote:
> My first question is, how to avoid that error with A.i4? Why
> is there a difference between @UNIFORM and @UNIFORM(), do the
> first returns a type and the later returns a value?
Basically yeah. a UDA in D is just whatever you write gets
directly attached - it does no additional processing. So if you
give it a type, it keeps a type. If a value, it keeps the value.
The simplest answer is probably "don't do that"; if it is meant
to be a value, always give it a value.
But you could also write your own get UDA thing that recognizes
the type (the check: static if(is(attribute)) for a type vs
static if(is(typeof(attribute))) for the value) and returns the
init value if you want in your code.
> My second quertion is, why the UNIFORM struct with
> uninitialized string producing UNIFORM(null).
> How can be a difference when I say name="", and it's just the
> same as the default string initializer, and then it produce
> UNIFORM("")?
null and "" can be used interchangeably and sometimes yield the
same thing, but they aren't exactly the same.
Since you specified it there in the definition as a default value
in a struct the compiler used that distinction. I'd suggest you
write what you mean even in cases where it is the same so you
cover the bases.
If you specifically want null, check `str is null` and use ` =
null`. If either is fine, just use `str.length == 0`.
More information about the Digitalmars-d-learn
mailing list