Custom attributes (again)
Piotr Szturmaj
bncrbme at jadamspam.pl
Fri Apr 6 07:08:33 PDT 2012
Jacob Carlborg wrote:
> On 2012-04-06 14:38, Piotr Szturmaj wrote:
>
>> Do you mean return type? I think your syntax has some serious
>> disadvantages. Consider parameters and multiple attributes.
>>
>> For return types I'd like to see something like this:
>>
>> @return: not_null
>> @return: MyAttr("foo")
>> Foo bar(@custom int a) {}
>>
>> This is similar to C#'s [return: MyAttr]. Alternatively it might be:
>>
>> @return(not_null)
>> @return(MyAttr("foo"))
>
> Hmm, or something like how Java does it. You attached this information
> when you declare the attribute:
>
> @Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible
> at runtime via reflection.
> @Target({ElementType.METHOD}) // This annotation can only be applied to
> class methods.
>
> public @interface Tweezable {
> }
>
> Or with another syntax:
>
> @attribute @target(type) struct not_null {}
>
I think that @target is a solution for simple types, but UDTs may use
another one:
struct MyAttr
{
template Attach(Symbol)
{
static if (!is(typeof(Symbol) == class) || !is(typeof(Symbol) : A))
static assert(0, "MyAttr must be attached to classes
derived from class A");
}
}
This allows more complex target checking possible. Besides above
example, some attributes can be mutually exclusive and
__traits(hasAttribute, ...) may be used to check that.
More information about the Digitalmars-d
mailing list