digitalmars.D.learn - Does D have class' attributes like C#'s?
- Marc (6/10) Dec 16 2017 C# has a quite nice way to store metadata about a property by a
- Anonymouse (8/18) Dec 16 2017 UDAs? User Defined Attributes.
- Marc (4/25) Dec 16 2017 I can't "pack" an object, right? In C#, TextSize is a class and
- Jonathan M Davis (18/50) Dec 16 2017 What do you mean by pack? You can mess with the alignment of a struct to
- Basile B. (17/45) Dec 16 2017 No, it's either a struct or a class and it's not included in the
- Jacob Carlborg (26/29) Dec 17 2017 In D it's an tuple of basically anything that is known at compile time,
- SealabJaster (8/9) Dec 17 2017 As others have said, D has UDAs.
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
On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:feature called atributes[1]. For example, I can write something like this: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() { /* ... */ } }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
On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!a feature called atributes[1]. For example, I can write something like this: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() { /* ... */ } }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
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: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 DavisOn Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!a feature called atributes[1]. For example, I can write something like this: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() { /* ... */ } }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
On Saturday, 16 December 2017 at 21:11:43 UTC, Marc wrote:On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote: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)); ```On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:256 is constructor's first argument. In D it's pretty much an array but I guess it's close enough. Thanks!a feature called atributes[1]. For example, I can write something like this: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() { /* ... */ } }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
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
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