An idea for a potential use of annotations:
How about being able to annotate _anything_ with a template?
It would be the equivalent of Python's annotations: the template's
first parameter would be an alias to the thing being annotated, and
its return value would be the code generated for that element. Of
course, if there's no transformation being done, then it should
simply "return" the alias itself.
This would allow you to say something like:
auto memoize(alias Fn, T...)(T args) { /* memoize code here */ }
memoize
int fib(int n) { ... }
which would be somewhat (but not completely) "equivalent" to
mixin memoize!(function int(int n) { ... });
And more trivial things like property would simply be
template property(alias A) { alias A property; }
with their sole effect being extra information found with
reflection.
Implementing this would obviously be difficult as I would assume,
but does it sound like a reasonable idea?

An idea for a potential use of annotations:
How about being able to annotate _anything_ with a template?
It would be the equivalent of Python's annotations: the template's
first parameter would be an alias to the thing being annotated, and
its return value would be the code generated for that element. Of
course, if there's no transformation being done, then it should
simply "return" the alias itself.

An idea for a potential use of annotations:
How about being able to annotate _anything_ with a template?
It would be the equivalent of Python's annotations: the template's
first parameter would be an alias to the thing being annotated, and
its return value would be the code generated for that element. Of
course, if there's no transformation being done, then it should
simply "return" the alias itself.

doing this, if the return type is inferred and the annotation template
_depends_ on the return type?
You could either go on a "best-effort" basis on the compiler, or just
assume that internal references (whether direct or indirect) assume
un-annotated symbols. But we could have different problems that way too
(i.e. security holes: calling the same method from different places
producing different results, etc.) so this would need lots of thinking
to work correctly.
Hopefully we can figure something out, though! :)
On 8/1/2011 1:05 AM, Peter Alexander wrote:

On 1/08/11 6:42 AM, %u wrote:

An idea for a potential use of annotations:
How about being able to annotate _anything_ with a template?

worth complicating the language even more for this convenience?

(2) It documents the fact that you're doing a transformation on
something else
(3) It also works pretty well for non-transforming annotations (i.e.
those that are there just for metadata) -- you could easy add static
assertions inside the template and return the original alias, etc.
(4) We're going to have annotations sooner or later (I hope?) so this is
just an idea on how to make them work, without introducing a new kind of
annotation declaration (since it works with pretty much any old template).
(5) To my (VERY) limited knowledge, this could be incredibly useful
Aspect-Oriented Programming, if I've understood the concept correctly. I
could very well be wrong, though.

Implementing this would obviously be difficult as I would assume,
but does it sound like a reasonable idea?

An idea for a potential use of annotations:
How about being able to annotate _anything_ with a template?
It would be the equivalent of Python's annotations: the template's
first parameter would be an alias to the thing being annotated, and
its return value would be the code generated for that element. Of
course, if there's no transformation being done, then it should
simply "return" the alias itself.
This would allow you to say something like:
auto memoize(alias Fn, T...)(T args) { /* memoize code here */ }
memoize
int fib(int n) { ... }
which would be somewhat (but not completely) "equivalent" to
mixin memoize!(function int(int n) { ... });
And more trivial things like property would simply be
template property(alias A) { alias A property; }
with their sole effect being extra information found with
reflection.
Implementing this would obviously be difficult as I would assume,
but does it sound like a reasonable idea?

I have been thinking that exact same thing. I would love to see that in
the compiler.
--
/Jacob Carlborg

I really like the idea of annotations to use in traits
(compile-time-pseudoreflection).
I have the following code:
http://code.google.com/p/pspemu/source/browse/trunk/src/pspemu/hle/kd/rtc/sceRtc.d
Each function has associated a NID. And I have to specify the NID outside,
repeating the name of the function and creating a method for that.
Now I have to do:
void initNids() {
mixin(registerd!(0xC41C2853, sceRtcGetTickResolution));
}
u32 sceRtcGetTickResolution() {
//...
}
And with custom attributes/annotations I could do something like this:
struct NID {
uint nid;
}
[NID(0xC41C2853)]
u32 sceRtcGetTickResolution() {
//...
}
And then with traits: allMembers + get custom attributos from each member. The
function name only appears once, and the NID appears near the implementation.