www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Source code annotations alla Java

reply Justin Johansson <jj nospam.com> writes:
Not long ago the Java Language people introduced the idea of annotations 
together with an annotation processing tool (apt).

Now perhaps the idea of source code annotations is not actually a Java 
invention per se, however for someone learning D is there any equivalent 
idiom [of Java annotations] in the D language?
Jan 20 2011
next sibling parent Justin Johansson <jj nospam.com> writes:
On 21/01/11 00:47, Justin Johansson wrote:
 Not long ago the Java Language people introduced the idea of annotations
 together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any equivalent
 idiom [of Java annotations] in the D language?
Fair to add that while finding the Java Language annotation concept interesting I am not entirely sure as to its usefulness. Thanks for answers, Justin Johansson
Jan 20 2011
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com> wrote:

 Not long ago the Java Language people introduced the idea of annotations  
 together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java  
 invention per se, however for someone learning D is there any equivalent  
 idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are like In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
Jan 20 2011
next sibling parent reply Justin Johansson <jj nospam.com> writes:
On 21/01/11 01:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com> wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
Thanks for answer. I wasn't expecting many enlightening responses. Yours was a pleasant reply, albeit a reflection of the current state of D. - Justin
Jan 20 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
Justin Johansson Wrote:

 Thanks for answer.  I wasn't expecting many enlightening responses. 
 Yours was a pleasant reply, albeit a reflection of the current state of D.
 
 - Justin
I think it is worth mentioning that the current syntax comes from DIP 6 http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP6 As whether custom annotations will happen, I haven't heard an official word. But it was somewhat an implied intent.
Jan 20 2011
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com> wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations. -- /Jacob Carlborg
Jan 20 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>  
 wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
Jan 20 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"? -- /Jacob Carlborg
Jan 20 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 20 Jan 2011 15:03:55 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a  
 Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"?
No. I meant int property; A keyword is specifically not allowed where the grammar would otherwise allow it. A symbol isn't allowed to have in it, so this naturally prevents a conflict. I realize the poor example, but it's definitely not a keyword. Otherwise, it would be listed here: http://www.digitalmars.com/d/2.0/lex.html#keyword (actually, are annotations part of the lexical grammar there?). It's more like Object, which is not a keyword, but you aren't allowed to use it to mean anything besides what it means. The end result is, it fails if you use it in the wrong place, but the keyword status makes it fail at the parsing stage. I am not a compiler writer, so I'm talking a bit from my ass here. -Steve
Jan 20 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-01-20 21:34, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 15:03:55 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a
 Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"?
No. I meant int property;
Of course that would work, isn't that like saying this won't work: int struct_; // ?
 A keyword is specifically not allowed where the grammar would otherwise
 allow it. A symbol isn't allowed to have   in it, so this naturally
 prevents a conflict. I realize the poor example, but it's definitely not
 a keyword. Otherwise, it would be listed here:
 http://www.digitalmars.com/d/2.0/lex.html#keyword (actually, are
 annotations part of the lexical grammar there?).

 It's more like Object, which is not a keyword, but you aren't allowed to
 use it to mean anything besides what it means.

 The end result is, it fails if you use it in the wrong place, but the
 keyword status makes it fail at the parsing stage. I am not a compiler
 writer, so I'm talking a bit from my ass here.

 -Steve
Ok, maybe you're right. I'm pretty sure, as you say, that a keyword in the wrong place would fail during parsing. But I don't know where a misplaced annotation/attribute would fail. -- /Jacob Carlborg
Jan 20 2011
parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 1/20/11 5:48 PM, Jacob Carlborg wrote:
 On 2011-01-20 21:34, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 15:03:55 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:

 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 wrote:

 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a
 Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"?
No. I meant int property;
Of course that would work, isn't that like saying this won't work: int struct_; // ?
 A keyword is specifically not allowed where the grammar would otherwise
 allow it. A symbol isn't allowed to have   in it, so this naturally
 prevents a conflict. I realize the poor example, but it's definitely not
 a keyword. Otherwise, it would be listed here:
 http://www.digitalmars.com/d/2.0/lex.html#keyword (actually, are
 annotations part of the lexical grammar there?).

 It's more like Object, which is not a keyword, but you aren't allowed to
 use it to mean anything besides what it means.

 The end result is, it fails if you use it in the wrong place, but the
 keyword status makes it fail at the parsing stage. I am not a compiler
 writer, so I'm talking a bit from my ass here.

 -Steve
Ok, maybe you're right. I'm pretty sure, as you say, that a keyword in the wrong place would fail during parsing. But I don't know where a misplaced annotation/attribute would fail.
Jacob is right here. This, as you say, fails: int struct; And also this fails: int property; So yes, currently property is just a keyword with a prepended . D. For example, as it is said in DIP6, you could have: extern('C') void someFunc(); instead of having extern a keyword and thus being unable to use it for identifiers.
Jan 21 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, January 21, 2011 12:36:23 Ary Manzana wrote:
 On 1/20/11 5:48 PM, Jacob Carlborg wrote:
 On 2011-01-20 21:34, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 15:03:55 -0500, Jacob Carlborg <doob me.com> wrote:
 On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg <doob me.com> wrote:
 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 
 wrote:
 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).
 
 Now perhaps the idea of source code annotations is not actually a
 Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"?
No. I meant int property;
Of course that would work, isn't that like saying this won't work: int struct_; // ?
 A keyword is specifically not allowed where the grammar would otherwise
 allow it. A symbol isn't allowed to have   in it, so this naturally
 prevents a conflict. I realize the poor example, but it's definitely not
 a keyword. Otherwise, it would be listed here:
 http://www.digitalmars.com/d/2.0/lex.html#keyword (actually, are
 annotations part of the lexical grammar there?).
 
 It's more like Object, which is not a keyword, but you aren't allowed to
 use it to mean anything besides what it means.
 
 The end result is, it fails if you use it in the wrong place, but the
 keyword status makes it fail at the parsing stage. I am not a compiler
 writer, so I'm talking a bit from my ass here.
 
 -Steve
Ok, maybe you're right. I'm pretty sure, as you say, that a keyword in the wrong place would fail during parsing. But I don't know where a misplaced annotation/attribute would fail.
Jacob is right here. This, as you say, fails: int struct; And also this fails: int property; So yes, currently property is just a keyword with a prepended . D. For example, as it is said in DIP6, you could have: extern('C') void someFunc(); instead of having extern a keyword and thus being unable to use it for identifiers.
No. No attributes are keywords. The reasoning is simple. A keyword is a word that would be a valid identifier but isn't, because the language treats it as special. is not a valid part of an identifier. So, property can't be a keyword. And property certainly isn't a keyword, since you _can_ use it as an identifier, so it isn't a keyword with prepended on either. True, property is treated as a special symbol by the compiler, and I don't expect that it really treats it like an attribute with the name property (which it will pretty much have to do if we ever get user-defined attributes), but definitely isn't a keyword, because it was never possible for it to be a valid identifier in the first place. - Jonathan M Davis
Jan 21 2011
parent "Jay Norwood" <jayn prismnet.com> writes:
On Friday, 21 January 2011 at 20:50:39 UTC, Jonathan M Davis 
wrote:
 On Friday, January 21, 2011 12:36:23 Ary Manzana wrote:
 On 1/20/11 5:48 PM, Jacob Carlborg wrote:
 On 2011-01-20 21:34, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 15:03:55 -0500, Jacob Carlborg 
 <doob me.com> wrote:
 On 2011-01-20 19:18, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 13:07:58 -0500, Jacob Carlborg 
 <doob me.com> wrote:
 On 2011-01-20 15:02, Steven Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson 
 <jj nospam.com>
 
 wrote:
 Not long ago the Java Language people introduced the 
 idea of
 annotations together with an annotation processing 
 tool (apt).
 
 Now perhaps the idea of source code annotations is not 
 actually a
 Java
 invention per se, however for someone learning D is 
 there any
 equivalent idiom [of Java annotations] in the D 
 language?
Haven't used Java since they added annotations, but I think they are In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect. -Steve
I would rather formulate it like: currently D has a syntax for keywords that are similar to Java annotations.
I don't think it's the same thing. Keywords are not allowed to be used anywhere else, even for things that would parse properly were they not keywords. They are anchors for the parser to determine where it is. In contrast, a compiler-defined annotation is parsed just the same as a custom one, it's just that the meaning is predefined. For example, you can legally do: int property; without error, but this won't even get past the parsing stage: int struct; -Steve
I assume you meant "int property;"?
No. I meant int property;
Of course that would work, isn't that like saying this won't work: int struct_; // ?
 A keyword is specifically not allowed where the grammar 
 would otherwise
 allow it. A symbol isn't allowed to have   in it, so this 
 naturally
 prevents a conflict. I realize the poor example, but it's 
 definitely not
 a keyword. Otherwise, it would be listed here:
 http://www.digitalmars.com/d/2.0/lex.html#keyword 
 (actually, are
 annotations part of the lexical grammar there?).
 
 It's more like Object, which is not a keyword, but you 
 aren't allowed to
 use it to mean anything besides what it means.
 
 The end result is, it fails if you use it in the wrong 
 place, but the
 keyword status makes it fail at the parsing stage. I am not 
 a compiler
 writer, so I'm talking a bit from my ass here.
 
 -Steve
Ok, maybe you're right. I'm pretty sure, as you say, that a keyword in the wrong place would fail during parsing. But I don't know where a misplaced annotation/attribute would fail.
Jacob is right here. This, as you say, fails: int struct; And also this fails: int property; So yes, currently property is just a keyword with a prepended . case in D. For example, as it is said in DIP6, you could have: extern('C') void someFunc(); instead of having extern a keyword and thus being unable to use it for identifiers.
No. No attributes are keywords. The reasoning is simple. A keyword is a word that would be a valid identifier but isn't, because the language treats it as special. is not a valid part of an identifier. So, property can't be a keyword. And property certainly isn't a keyword, since you _can_ use it as an identifier, so it isn't a keyword with prepended on either. True, property is treated as a special symbol by the compiler, and I don't expect that it really treats it like an attribute with the name property (which it will pretty much have to do if we ever get user-defined attributes), but definitely isn't a keyword, because it was never possible for it to be a valid identifier in the first place. - Jonathan M Davis
I see eclipse4 used java annotations heavily to mark up code for dependency injection in the new ide framework. Someone recommended use of mixin in this thread, in place of the java use of user defined annotations. Is that still the recommended D solution for handling ports of java annotations? https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection
Jan 29 2014
prev sibling next sibling parent Andrew Wiley <debio264 gmail.com> writes:
On Thu, Jan 20, 2011 at 8:02 AM, Steven Schveighoffer
<schveiguy yahoo.com>wrote:

 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson <jj nospam.com>
 wrote:

  Not long ago the Java Language people introduced the idea of annotations
 together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any equivalent
 idiom [of Java annotations] in the D language?
Haven't used Java since they added annotations, but I think they are like In any case, D has an annotation syntax like: property But I think at the moment, annotations have no custom ability. Only compiler-defined annotations are allowed. This may change in the future, but probably not short-term. FWIW, I think we need a much richer runtime-reflection capability before we can use custom annotations to any great effect.
I would find it useful to be able to access annotations as part of compile time reflection, actually, but AFAIK no progress has been made on that front so far.
Jan 20 2011
prev sibling parent "Paulo Pinto" <pjmlp progtools.org> writes:
On Thursday, 20 January 2011 at 14:04:54 UTC, Steven 
Schveighoffer wrote:
 On Thu, 20 Jan 2011 08:47:28 -0500, Justin Johansson 
 <jj nospam.com> wrote:

 Not long ago the Java Language people introduced the idea of 
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not 
 actually a Java invention per se, however for someone learning 
 D is there any equivalent idiom [of Java annotations] in the D 
 language?
Haven't used Java since they added annotations, but I think
-- Paulo
Dec 10 2014
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-01-20 14:47, Justin Johansson wrote:
 Not long ago the Java Language people introduced the idea of annotations
 together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any equivalent
 idiom [of Java annotations] in the D language?
Depending on what you want to do you can create a template mixin that accepts string(s)/alias(es). For example: for the serialization library (http://dsource.org/projects/orange) I'm working on the following syntax is used: class Foo { int x; int y; int z; mixin NonSerialized!(z); } The above mixin indicates that the "z" instance variable shouldn't be serialized. This is (currently) achieved by defining a field in the mixed in template (which will be added to the class) which is a struct containing the string of the filed which shouldn't be serialized. Then I iterate over all the fields in the class with .tupleof and collects all mixed in fields in a list. Then I know what fields to skip later during the serialization. -- /Jacob Carlborg
Jan 20 2011
next sibling parent reply Trass3r <un known.com> writes:
 class Foo
 {
      int x;
      int y;
      int z;

      mixin NonSerialized!(z);
 }
Had a quick look at http://dsource.org/projects/orange/browser/orange/serialization/Serializable.d 1. How come it works without 'mixin' in the template declaration (mixin template NonSerialized)? 2. What if several fields need to be tagged? 3. Is there a reason to use a struct instead of e.g. __nonSerialized = ["field1", "field2", ...]? 4. Couldn't that field be static to save space or maybe even enum?
Jan 20 2011
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Trass3r <un known.com> wrote:

 class Foo
 {
      int x;
      int y;
      int z;

      mixin NonSerialized!(z);
 }
Had a quick look at http://dsource.org/projects/orange/browser/orange/serialization/Serializable.d 1. How come it works without 'mixin' in the template declaration (mixin template NonSerialized)?
Templates are not currently required to be marked as mixin templates in order to be used as such. However, a mixin template can only be instantiated as a mixin. -- Simen
Jan 20 2011
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-01-20 20:10, Trass3r wrote:
 class Foo
 {
 int x;
 int y;
 int z;

 mixin NonSerialized!(z);
 }
Had a quick look at http://dsource.org/projects/orange/browser/orange/serialization/Serializable.d 1. How come it works without 'mixin' in the template declaration (mixin template NonSerialized)?
I think it's optional, it works with DMD 2.052
 2. What if several fields need to be tagged?
You use the mixin several times.
 3. Is there a reason to use a struct instead of e.g. __nonSerialized =
 ["field1", "field2", ...]?
Hmm, I think I had a good reason. First it needs to be const so it works during compile time. But I think I used a struct because I used that on another project which required more data than just then name the field. If I would store it in an array you could only do the mixin once, I think. And that would require you to use strings instead of aliases, I think. template foo (args...) {} Does that work with aliases?
 4. Couldn't that field be static to save space or maybe even enum?
That field should absolutely be static, I'm currently working on fixing that. I didn't think of that when I implemented it. This will also only work with one mixin (see the answer for the third comment). -- /Jacob Carlborg
Jan 20 2011
parent Trass3r <un known.com> writes:
 template foo (args...) {}

 Does that work with aliases?
Well at least the following compiles: template Foo(T...,) { int i; } int a,b; alias Foo!(a,b) f;
Jan 20 2011
prev sibling parent "Mengu" <mengukagan gmail.com> writes:
On Thursday, 20 January 2011 at 18:19:21 UTC, Jacob Carlborg 
wrote:
 On 2011-01-20 14:47, Justin Johansson wrote:
 Not long ago the Java Language people introduced the idea of 
 annotations
 together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not 
 actually a Java
 invention per se, however for someone learning D is there any 
 equivalent
 idiom [of Java annotations] in the D language?
Depending on what you want to do you can create a template mixin that accepts string(s)/alias(es). For example: for the serialization library (http://dsource.org/projects/orange) I'm working on the following syntax is used: class Foo { int x; int y; int z; mixin NonSerialized!(z); } The above mixin indicates that the "z" instance variable shouldn't be serialized. This is (currently) achieved by defining a field in the mixed in template (which will be added to the class) which is a struct containing the string of the filed which shouldn't be serialized. Then I iterate over all the fields in the class with .tupleof and collects all mixed in fields in a list. Then I know what fields to skip later during the serialization.
let's assume you are building a web application and you want to keep that controller only for the logged in users. in java you can create an annotation class called RequireLogin and put your logic there. mixins are not the way for this.
Jan 30 2014
prev sibling parent reply "Denis Gladkiy" <denis.gladkiy yahoo.com> writes:
On Thursday, 20 January 2011 at 13:49:30 UTC, Justin Johansson 
wrote:
 Not long ago the Java Language people introduced the idea of 
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually 
 a Java invention per se, however for someone learning D is 
 there any equivalent idiom [of Java annotations] in the D 
 language?
They are here: http://dlang.org/attribute.html#uda
Dec 09 2014
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/10/14 1:16 AM, Denis Gladkiy wrote:
 On Thursday, 20 January 2011 at 13:49:30 UTC, Justin Johansson wrote:
 Not long ago the Java Language people introduced the idea of
 annotations together with an annotation processing tool (apt).

 Now perhaps the idea of source code annotations is not actually a Java
 invention per se, however for someone learning D is there any
 equivalent idiom [of Java annotations] in the D language?
They are here: http://dlang.org/attribute.html#uda
This is an almost-4-year-old thread. -Steve
Dec 11 2014