Re: Does D have class' attributes like C#'s?

2017-12-17 Thread SealabJaster via Digitalmars-d-learn

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)


Re: Does D have class' attributes like C#'s?

2017-12-17 Thread Jacob Carlborg via Digitalmars-d-learn

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


Re: Does D have class' attributes like C#'s?

2017-12-16 Thread Basile B. via Digitalmars-d-learn

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:
C# has a quite nice way to store metadata about a property by 
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() { /* ... */ }
}


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!


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));

```


Re: Does D have class' attributes like C#'s?

2017-12-16 Thread Jonathan M Davis via Digitalmars-d-learn
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:
> >> C# has a quite nice way to store metadata about a property by
> >> 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() { /* ... */ }
> >
> > }
>
> 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!

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



Re: Does D have class' attributes like C#'s?

2017-12-16 Thread Marc via Digitalmars-d-learn

On Saturday, 16 December 2017 at 20:05:15 UTC, Anonymouse wrote:

On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:
C# has a quite nice way to store metadata about a property by 
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() { /* ... */ }
}


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!




Re: Does D have class' attributes like C#'s?

2017-12-16 Thread Anonymouse via Digitalmars-d-learn

On Saturday, 16 December 2017 at 19:57:30 UTC, Marc wrote:
C# has a quite nice way to store metadata about a property by 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() { /* ... */ }
}


Does D have class' attributes like C#'s?

2017-12-16 Thread Marc via Digitalmars-d-learn
C# has a quite nice way to store metadata about a property by 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?