www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D have class' attributes like C#'s?

reply Marc <jckj33 gmail.com> writes:

feature called atributes[1]. For example, I can write something 
like this:

 class A {
    [TextSize(256)]
    string Name { get; set; }
 }
So using runtime/reflection I can retrieve the TextSize value associated to A.name property. Does D have something similar?
Dec 16 2017
next sibling parent reply Anonymouse <asdf asdf.net> writes:
On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:

 feature called atributes[1]. For example, I can write something 
 like this:

 class A {
    [TextSize(256)]
    string Name { get; set; }
 }
So using runtime/reflection I can retrieve the TextSize value associated to A.name property. Does D have something similar?
UDAs? User Defined Attributes. https://dlang.org/spec/attribute.html#UserDefinedAttribute http://ddili.org/ders/d.en/uda.html class A { TextSize(256) string name() { /* ... */ } }
Dec 16 2017
parent reply Marc <jckj33 gmail.com> writes:
On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:
 On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:

 a feature called atributes[1]. For example, I can write 
 something like this:

 class A {
    [TextSize(256)]
    string Name { get; set; }
 }
So using runtime/reflection I can retrieve the TextSize value associated to A.name property. Does D have something similar?
UDAs? User Defined Attributes. https://dlang.org/spec/attribute.html#UserDefinedAttribute http://ddili.org/ders/d.en/uda.html class A { TextSize(256) string name() { /* ... */ } }
256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!
Dec 16 2017
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, December 16, 2017 21:11:43 Marc via Digitalmars-d-learn wrote:
 On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:
 On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:

 a feature called atributes[1]. For example, I can write

 something like this:
 class A {

    [TextSize(256)]
    string Name { get; set; }

 }
So using runtime/reflection I can retrieve the TextSize value associated to A.name property. Does D have something similar?
UDAs? User Defined Attributes. https://dlang.org/spec/attribute.html#UserDefinedAttribute http://ddili.org/ders/d.en/uda.html class A { TextSize(256) string name() { /* ... */ } }
256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!
What do you mean by pack? You can mess with the alignment of a struct to reduce how much space it takes up if that's what you're talking about: https://dlang.org/spec/struct.html http://ddili.org/ders/d.en/memory.html But you can't do that with classes. The compiler controls their layout and can muck with it if it thinks that that makes it more efficient or whatnot, whereas structs have the layout you tell them so that they can match up with C structs as well as be used for managing how things are laid out in memory. As for string being "pretty much an array," it's exactly an array. string is just an alias for immutable(string)[], and underneath the hood, dynamic arrays are essentially struct DynamicArray(T) { size_t length; T* ptr; } - Jonathan M Davis
Dec 16 2017
prev sibling next sibling parent Basile B. <b2.temp gmx.com> writes:
On Saturday, 16 December 2017 at 21:11:43 UTC, Marc wrote:
 On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:
 On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:

 a feature called atributes[1]. For example, I can write 
 something like this:

 class A {
    [TextSize(256)]
    string Name { get; set; }
 }
So using runtime/reflection I can retrieve the TextSize value associated to A.name property. Does D have something similar?
UDAs? User Defined Attributes. https://dlang.org/spec/attribute.html#UserDefinedAttribute http://ddili.org/ders/d.en/uda.html class A { TextSize(256) string name() { /* ... */ } }
256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!
No, it's either a struct or a class and it's not included in the class itself. You use the __trait(getAttributes) to retrieve the value when needed: ``` struct TextSize {int value;} class A { TextSize(256) string name() { return ""; } } class B { string name() { return ""; } } static assert(__traits(classInstanceSize, A) == __traits(classInstanceSize, B)); ```
Dec 16 2017
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2017-12-16 22:11, Marc wrote:


 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
Dec 17 2017
prev sibling parent SealabJaster <sealabjaster gmail.com> writes:
On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:
 Does D have something similar?
As others have said, D has UDAs. An alternative to using __traits(getAttributes) is to use std.traits.getUDAs [1] I've made a small example using std.traits.getUDAs, based off of your example. [2] [1] https://dlang.org/library/std/traits/get_ud_as.html [2] https://run.dlang.io/is/PTfEcw (I hope that link works)
Dec 17 2017