Proposal: user defined attributes

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 21 11:32:41 PDT 2012


On Wed, 21 Mar 2012 13:47:56 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2012-03-21 17:00, Adam D. Ruppe wrote:

>> The other thing is @attribute struct {} rather than just
>> struct {}. I don't any benefit to that. If I want to
>> reuse a regular, runtime struct to store info at compile
>> time, why shouldn't I be able to do that?
>
> I just throw that in because Java has it. Don't know if it's needed or  
> not. Java has a kind of special syntax for default values with  
> annotations.

Really, the only requirement for an annotation expression should be that  
it's evaluated at compile time.

But we could see weird things with annotations if we don't specifically  
say what's an annotation and what is not.

For example:

// used as a normal datatype in most places
struct Point2d
{
   int X;
   int Y;
}

@Point2d int x; // ??
or
@note(Point2d) int x; // ??

In my proposal, I specified that the annotation can be applied only to  
module-level functions (normal or templated), and the result of those  
functions is what gets saved.  So with that capability, you should be able  
to store any CTFE-evaluatable struct:

@annotation auto makeAPoint() {Point2d p;  return p;}

@makeAPoint int x;

Which is as confusing as the above two examples, but at least *someone*  
declared it made sense to them ;)  Note that the @annotation function need  
not be stored in the EXE, it's only valid during CTFE.

I look at it similar to in C++ being able to throw any arbitrary type.   
Sure, you can just avoid creating an exception type that wraps an int, but  
it may not make sense to anyone if you just throw an int.

-Steve


More information about the Digitalmars-d mailing list