User Defined Attributes

David Nadlinger see at klickverbot.at
Tue Nov 6 09:32:54 PST 2012


On Tuesday, 6 November 2012 at 17:00:27 UTC, Walter Bright wrote:
> Ok, I ask again, what use case for a UDA is there for function 
> parameters? (Note that IDL isn't it, as D already has enough 
> parameter attributes to support IDL.)

What »IDL« are you referring to here? At least as far as I am 
aware, IDL usually just refers to an »interface definition 
language« in general, so I'm not quite sure what you mean if you 
are talking about »enough to support IDL«.

Actually, the Thrift IDL would be a perfect example of how 
attributes on parameters can be useful. A simple service 
definition in a .thrift could look like this:

---
service Calculator {
    i32 calculate(1:i32 a, 2:i32 b, 3:Op op)
}
---

Note that the parameters are given explicit ids, similar to 
regular struct fields in Thrift, to provide robustness of the 
generated code against future RPC protocol changes (e.g. addition 
of parameters).

Currently, the equivalent D code for the interface would look 
something like this:

---
interface Calculator : SharedService {
   int calculate(int a, int b, Op op);

   enum methodMeta = [
     TMethodMeta(`calculate`,
       [TParamMeta(`a`, 1), TParamMeta(`b`, 2), TParamMeta(`op`, 
3)]
     )
   ];
}
---

Being able to assign the IDs in-line using UDAs would make for a 
much more natural method declaration syntax.

David


More information about the Digitalmars-d-announce mailing list