www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Annotations or custom attributes

reply Manu <turkeyman gmail.com> writes:
Does D have a nice way to add annotations or custom attributes to entities?

In Java/C# for example, it is common to annotate things with useful compile
time information. I'd like to be able to do that in D on occasion.

For instance, I'm serialising some struct/class using reflection to some
text format, but there are a couple of members in a particular class that I
don't want to be written.
A nice solution might be that I could annotate various members:
 DoNotSerialise int thing; ... or something along those lines, whatever
concept you want to apply, which I could then inspect in static if() logic
to produce some requested additional behaviour.

This is a trivial example, but looking at C#/Java, you can see how many
useful things can be done with this sort of system.
How would it be done currently?
Mar 09 2012
next sibling parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 09-03-2012 11:56, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to entities?

Unfortunately, no.
 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on occasion.

Yes. This is a very needed feature.
 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

Probably not at all (to my knowledge anyway). -- - Alex
Mar 09 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-03-09 11:56, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to entities?

Unfortunately no.
 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on occasion.

 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

Yeah, it would be so nice to have. As a workaround you can mixin some code, variables or similar in a class/struct. For serialization you could use my library Orange, which already supports this. You can have a look at the NonSerialized template at: http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html https://github.com/jacob-carlborg/orange -- /Jacob Carlborg
Mar 09 2012
prev sibling next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 09.03.2012 14:56, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to entities?

There is a potential for them. At least technically everything with in front of it was supposed to be an annotation (like property). I think it's just, sort of, reserved for future. [...] -- Dmitry Olshansky
Mar 09 2012
next sibling parent reply Manu <turkeyman gmail.com> writes:
Okay, so the consensus is, it doesn't currently exist, but there is no real
resistance, and is tentatively planned?
Sounds good to me.

On 9 March 2012 15:42, Dmitry Olshansky <dmitry.olsh gmail.com> wrote:

 On 09.03.2012 14:56, Manu wrote:

 Does D have a nice way to add annotations or custom attributes to
 entities?

  There is a potential for them. At least technically everything with   in

front of it was supposed to be an annotation (like property). I think it's just, sort of, reserved for future. [...] -- Dmitry Olshansky

Mar 09 2012
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Manu:

 Okay, so the consensus is, it doesn't currently exist, but 
 there is no real
 resistance, and is tentatively planned?
 Sounds good to me.

As far as I know there are no concrete ideas yet for the semantics and precise usage of this feature. And I think there are different ideas regarding what this feature has to do (example: I think of it as as a user-defined extension of the type system. Other people think of this feature more like the C#, Scala or Java ones). So if you want this feature to happen and you have good ideas, then I suggest you to write down a little concrete proposal of definition syntax/semantics (implementation isn't needed now). Bye, bearophile
Mar 09 2012
parent Manu <turkeyman gmail.com> writes:
On 9 March 2012 17:20, bearophile <bearophileHUGS lycos.com> wrote:

 Manu:


  Okay, so the consensus is, it doesn't currently exist, but there is no
 real
 resistance, and is tentatively planned?
 Sounds good to me.

As far as I know there are no concrete ideas yet for the semantics and precise usage of this feature. And I think there are different ideas regarding what this feature has to do (example: I think of it as as a user-defined extension of the type system. Other people think of this feature more like the C#, Scala or Java ones). So if you want this feature to happen and you have good ideas, then I suggest you to write down a little concrete proposal of definition syntax/semantics (implementation isn't needed now). Bye, bearophile

Okay, well I'll do that at some point, but not now. My other multiple return values thread is about a thousand times more important to me at this point, so I'll keep driving that for the time being :P
Mar 09 2012
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, March 09, 2012 16:00:03 Manu wrote:
 Okay, so the consensus is, it doesn't currently exist, but there is no real
 resistance, and is tentatively planned?
 Sounds good to me.

I wouldn't really say that it's tentatively planned. I don't recall Walter ever weighing in on it at all, and no one is working on it. Rather, the current situation does not prevent it, and there's definitely some desire for it in the community, so there's a decent chance that it'll be implemented eventually. There are no plans for it though. Regardless, adding them should be backwards compatible, so it's really not a problem to wait until other more pressing issues have been addressed and the language has better stabilized before adding such a feature. - Jonathan M Davis
Mar 09 2012
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/9/12 2:56 AM, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to entities?

 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on occasion.

 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

I think a good approach in D would be to define mixins that work in conjunction with the feature involved, for example: class A { int thing; mixin(DoNotSerialize!"thing"); ... } or together: class A { mixin(DoNotSerialize!(int, "thing")); ... } Andrei
Mar 09 2012
next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
That's easy to implement and extremely ugly.

On Fri, Mar 9, 2012 at 8:15 PM, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:
 On 3/9/12 2:56 AM, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to
 entities?

 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on occasio=


n.
 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

I think a good approach in D would be to define mixins that work in conjunction with the feature involved, for example: class A { =C2=A0 =C2=A0int thing; =C2=A0 =C2=A0mixin(DoNotSerialize!"thing"); =C2=A0 =C2=A0... } or together: class A { =C2=A0 =C2=A0mixin(DoNotSerialize!(int, "thing")); =C2=A0 =C2=A0... } Andrei

--=20 Bye, Gor Gyolchanyan.
Mar 09 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-03-09 17:15, Andrei Alexandrescu wrote:
 On 3/9/12 2:56 AM, Manu wrote:
 Does D have a nice way to add annotations or custom attributes to
 entities?

 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on
 occasion.

 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

I think a good approach in D would be to define mixins that work in conjunction with the feature involved, for example: class A { int thing; mixin(DoNotSerialize!"thing"); ... } or together: class A { mixin(DoNotSerialize!(int, "thing")); ... } Andrei

As may serialization library Orange already does: http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html Look for "NonSerialized". -- /Jacob Carlborg
Mar 09 2012
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/9/12 8:30 AM, Jacob Carlborg wrote:
 As may serialization library Orange already does:
 http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html


 Look for "NonSerialized".

Saw that after posting. Cool! Andrei P.S. Please don't overquote, you systematically do so. Thanks!
Mar 09 2012
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 09 Mar 2012 18:23:17 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 On 3/9/12 8:30 AM, Jacob Carlborg wrote:
 As may serialization library Orange already does:
 http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html


 Look for "NonSerialized".

Saw that after posting. Cool! Andrei P.S. Please don't overquote, you systematically do so. Thanks!

Please continue to quote the previous two messages for context if you feel like it. I enjoy not having to look back to previous messages. Thanks! -Steve
Mar 09 2012
parent reply Ary Manzana <ary esperanto.org.ar> writes:
What are you talking about?
Mar 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 09 Mar 2012 19:05:00 -0500, Ary Manzana <ary esperanto.org.ar>  
wrote:

 What are you talking about?

Who are you talking to? :) Seriously, though, I think Andrej sometimes quotes *nothing* (no offense for picking on you Andrej), and couple that with the newsgroup's seemingly random decision to start a new thread, or put a reply at the same level, I sometimes have no idea what he's talking about :) -Steve
Mar 09 2012
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/10/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 Seriously, though, I think Andrej sometimes quotes *nothing* (no offense
 for picking on you Andrej), and couple that with the newsgroup's seemingly
 random decision to start a new thread, or put a reply at the same level, I
 sometimes have no idea what he's talking about :)

LOL. I was honestly wondering if this was an issue or not. I blame the gmail quick-reply box. It has two options: quote everything, or quote nothing. Otherwise there's a full reply button and then I can quickly select a piece of text to quote. But I *thought* that by using quick reply without fully quoting it would mean my reply would sit right next to the last poster's reply. I'm just used to using forum software and when I do a quick-reply there my post is right below the last user's post, and it's easy to figure out who I'm replying to. Anyway, I'll stop posting replies to the void. :p (thanks for letting me know btw)
Mar 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 09 Mar 2012 19:53:29 -0500, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 On 3/10/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 Seriously, though, I think Andrej sometimes quotes *nothing* (no offense
 for picking on you Andrej), and couple that with the newsgroup's  
 seemingly
 random decision to start a new thread, or put a reply at the same  
 level, I
 sometimes have no idea what he's talking about :)

LOL. I was honestly wondering if this was an issue or not. I blame the gmail quick-reply box. It has two options: quote everything, or quote nothing. Otherwise there's a full reply button and then I can quickly select a piece of text to quote. But I *thought* that by using quick reply without fully quoting it would mean my reply would sit right next to the last poster's reply.

I think it does, actually. It's just the post you are responding to isn't always the last poster :D
 I'm just used to using forum software and when I do a quick-reply
 there my post is right below the last user's post, and it's easy to
 figure out who I'm replying to.

The forum software does not by default thread discussions, but my newsreader does.
 Anyway, I'll stop posting replies to the void. :p (thanks for letting
 me know btw)

I hope you realize I didn't mean it in a bad/condescending way :) I realize we all use software that sometimes doesn't do the right thing, and it's not always our fault for liking some shitty software. But whose fault is it? Yours for using a forum-like software, or mine for trying to thread the newsgroup? If a tree posts a reply in the wrong thread, and the forest doesn't use threading readers, does anyone care? -Steve
Mar 09 2012
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/10/12, Steven Schveighoffer <schveiguy yahoo.com> wrote:
 I hope you realize I didn't mean it in a bad/condescending way :)

No problemo! I don't take things too seriously around this place (or the internets in general).
 But whose fault is it?

I'd say it's the technology's fault. A newsgroup seems to be the equivalent of a source file, but with each person having a compiler with different semantics. Related: http://www.youtube.com/watch?v=Fc1P-AEaEp8
Mar 09 2012
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Mar 09, 2012 at 07:24:34PM -0500, Steven Schveighoffer wrote:
 On Fri, 09 Mar 2012 19:05:00 -0500, Ary Manzana
 <ary esperanto.org.ar> wrote:
 
What are you talking about?

Who are you talking to? :) Seriously, though, I think Andrej sometimes quotes *nothing* (no offense for picking on you Andrej), and couple that with the newsgroup's seemingly random decision to start a new thread, or put a reply at the same level, I sometimes have no idea what he's talking about :)

[...] You guys should just use a threading mail reader. Like mutt. :-P :-P (Though Ary's post didn't make sense to me either, because I'd deleted the parent post already. Ah well. The joys of impersonal online communication...) T -- I see that you JS got Bach.
Mar 09 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 09 Mar 2012 19:58:37 -0500, H. S. Teoh <hsteoh quickfur.ath.cx>  
wrote:

 On Fri, Mar 09, 2012 at 07:24:34PM -0500, Steven Schveighoffer wrote:
 Seriously, though, I think Andrej sometimes quotes *nothing* (no
 offense for picking on you Andrej), and couple that with the
 newsgroup's seemingly random decision to start a new thread, or put
 a reply at the same level, I sometimes have no idea what he's
 talking about :)

[...] You guys should just use a threading mail reader. Like mutt. :-P :-P (Though Ary's post didn't make sense to me either, because I'd deleted the parent post already. Ah well. The joys of impersonal online communication...)

You should not delete old posts :) I find it invaluable for searching for old discussions sometimes (I use opera with google-like searching). -Steve
Mar 09 2012
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Mar 09, 2012 at 08:07:47PM -0500, Steven Schveighoffer wrote:
 On Fri, 09 Mar 2012 19:58:37 -0500, H. S. Teoh
 <hsteoh quickfur.ath.cx> wrote:
 
On Fri, Mar 09, 2012 at 07:24:34PM -0500, Steven Schveighoffer wrote:
Seriously, though, I think Andrej sometimes quotes *nothing* (no
offense for picking on you Andrej), and couple that with the
newsgroup's seemingly random decision to start a new thread, or put
a reply at the same level, I sometimes have no idea what he's
talking about :)

[...] You guys should just use a threading mail reader. Like mutt. :-P :-P (Though Ary's post didn't make sense to me either, because I'd deleted the parent post already. Ah well. The joys of impersonal online communication...)

You should not delete old posts :) I find it invaluable for searching for old discussions sometimes (I use opera with google-like searching).

[...] While I agree with you on principle, after having accumulated YEARS of archives for several high-volume mailing lists in the past I'm starting to doubt the value of doing it. Sometimes the signal-to-noise ratio is just too low to justify archiving *everything*. So I usually only save threads that I find interesting and has lasting value and delete the rest (esp. since there's an online archive for it anyway). T -- In a world without fences, who needs Windows and Gates? -- Christian Surchi
Mar 09 2012
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/9/12 4:24 PM, Steven Schveighoffer wrote:
 On Fri, 09 Mar 2012 19:05:00 -0500, Ary Manzana <ary esperanto.org.ar>
 wrote:

 What are you talking about?

Who are you talking to? :) Seriously, though, I think Andrej sometimes quotes *nothing* (no offense for picking on you Andrej), and couple that with the newsgroup's seemingly random decision to start a new thread, or put a reply at the same level, I sometimes have no idea what he's talking about :)

There are extremes, of course. I, for one, take pains to make sure I quote about the "right" amount of context. I know it's time well spent because every second I save for one reader is multiplied by many of them. I think it's very jarring to see a 300-line discussion quoted and followed by a brief answer. If that much context is necessary, I'd have no business reading that message unless I actually followed the conversation. Andrei
Mar 09 2012
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-03-10 00:23, Andrei Alexandrescu wrote:
 P.S. Please don't overquote, you systematically do so. Thanks!

I'll try to think about that. -- /Jacob Carlborg
Mar 10 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
On 9 March 2012 18:15, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org>wrote:

 On 3/9/12 2:56 AM, Manu wrote:

 Does D have a nice way to add annotations or custom attributes to
 entities?

 In Java/C# for example, it is common to annotate things with useful
 compile time information. I'd like to be able to do that in D on occasion.

 For instance, I'm serialising some struct/class using reflection to some
 text format, but there are a couple of members in a particular class
 that I don't want to be written.
 A nice solution might be that I could annotate various members:
  DoNotSerialise int thing; ... or something along those lines, whatever
 concept you want to apply, which I could then inspect in static if()
 logic to produce some requested additional behaviour.

 This is a trivial example, but looking at C#/Java, you can see how many
 useful things can be done with this sort of system.
 How would it be done currently?

I think a good approach in D would be to define mixins that work in conjunction with the feature involved, for example: class A { int thing; mixin(DoNotSerialize!"thing"); ... } or together: class A { mixin(DoNotSerialize!(int, "thing")); ... } Andrei

Yep, that sure is horribly ugly! Sadly, that's what I thought the case was currently. Let's hope for user defined attributes in the future ;)
Mar 09 2012
prev sibling parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2012-03-09 16:15:30 +0000, Andrei Alexandrescu 
<SeeWebsiteForEmail erdani.org> said:

 I think a good approach in D would be to define mixins that work in 
 conjunction with the feature involved, for example:
 
 class A {
      int thing;
      mixin(DoNotSerialize!"thing");
      ...
 }
 
 or together:
 
 class A {
      mixin(DoNotSerialize!(int, "thing"));
      ...
 }

It's ugly, but it works… only to a point though. Try to annotate overloaded functions and it'll become a mess: either you duplicate the function name and all the argument types for each function, or you wrap the whole function body in the mixin. If you need to annotate a struct or a class you'll have the same problem, just at a bigger scale. And it gets worse if you want to annotate templated types and functions: how can the attribute apply to each instance? I'm not saying any of this is impossible using mixins, just that it becomes impractical as you go beyond the most simple cases. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Mar 09 2012
parent Manu <turkeyman gmail.com> writes:
On 9 March 2012 19:23, Michel Fortin <michel.fortin michelf.com> wrote:

 On 2012-03-09 16:15:30 +0000, Andrei Alexandrescu <
 SeeWebsiteForEmail erdani.org**> said:

  I think a good approach in D would be to define mixins that work in
 conjunction with the feature involved, for example:

 class A {
     int thing;
     mixin(DoNotSerialize!"thing");
     ...
 }

 or together:

 class A {
     mixin(DoNotSerialize!(int, "thing"));
     ...
 }

It's ugly, but it works=E2=80=A6 only to a point though. Try to annotate overloaded functions and it'll become a mess: either you duplicate the function name and all the argument types for each function, or you wrap t=

he
 whole function body in the mixin.

 If you need to annotate a struct or a class you'll have the same problem,
 just at a bigger scale. And it gets worse if you want to annotate templat=

ed
 types and functions: how can the attribute apply to each instance?

 I'm not saying any of this is impossible using mixins, just that it
 becomes impractical as you go beyond the most simple cases.

It seems to me that everyone agrees on the usefulness of proper user defined annotations, so I'm personally satisfied that it *will* come :) .. It's certainly not urgent for me, but it sure would be nice!
Mar 09 2012