Does D have class' attributes like C#'s?
Jacob Carlborg
doob at me.com
Sun Dec 17 10:01:58 UTC 2017
On 2017-12-16 22:11, Marc wrote:
> I can't "pack" an object, right? In C#, TextSize is a class and 256 is
> constructor's first argument. In D it's pretty much an array but I guess
> it's close enough. Thanks!
In D it's an tuple of basically anything that is known at compile time,
values or types:
alias foo = int;
struct TextSize
{
int value;
}
int bar(int a) { return a; }
@(foo, 3, "asd", TextSize(256), bar(3))
class A {}
The following syntax is syntax sugar:
@TextSize(256)
class B {}
For:
@(TextSize(256))
class B {}
What's happening is that TextSize (all structs) has an implicit
construct for all fields. An instance of TextSize is constructed at
compile time and is used as a value in the UDA tuple.
The UDAs only exists during compile time (unless you explicitly save
them somehow). Runtime reflection cannot be used, but compile time
reflection can be used, using the "getAttribute" traits [1[
[1] https://dlang.org/spec/traits.html#getAttributes
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list