Custom Attributes for Function Parameters?

Jerry hurricane at hereiam.com
Sun Sep 24 03:49:12 UTC 2017


On Sunday, 24 September 2017 at 02:06:50 UTC, Jonathan Marler 
wrote:
> On Saturday, 23 September 2017 at 23:13:33 UTC, Jerry wrote:
>> Is there any reason why custom attributes aren't allowed for 
>> function parameters? It'd be useful for my situation to give 
>> the parameter some more information. Seems it is allowed in 
>> some other languages that have attributes, so was just 
>> wondering.
>
> Can you share your use case for them? I'm curious how they 
> could be useful.

Well for me it's for functions I'm using in my scripting 
language. Internally a lot of objects are simply represented as 
indices into an array. For the scripting language though they 
have their own type defined for them. So it'd be convenient to be 
able to add that information into the function signature along 
with the index.


void functionForScript(@(ScriptType.name) MyIndexType nameIndex, 
@(ScriptType.object) MyIndexType objectIndex)
{
}

There are other ways to do it but they aren't as convenient. If I 
just made it an attribute for the function, and manually declared 
all the types. I'd have to maintain that when most of the other 
types don't need extra data and can be determined just by the 
type.

As well for other uses right now you can't pass a function or 
delegate that is @nogc through to a function, without first 
making it an alias. It's annoying if it's just one function that 
needs it, now you need to give it a name somewhere else for a one 
off.

I'm sure there's other uses as well. C++ has maybe_unused 
attribute, though that doesn't really apply to D as it doesn't 
really have any warning messages at all. Being able to put in 
that extra information for use is nice, just like it is nice to 
be able to attach them to structs and classes. Which is why I was 
wondering why it wasn't included, even for special attributes 
like @nogc.


More information about the Digitalmars-d mailing list