Optional tags and attributes

Steven Schveighoffer schveiguy at yahoo.com
Fri Jan 17 19:29:16 PST 2014


On Fri, 17 Jan 2014 22:21:15 -0500, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> On Fri, Jan 17, 2014 at 10:02:44PM -0500, Steven Schveighoffer wrote:
>> On Fri, 17 Jan 2014 21:50:07 -0500, Stanislav Blinov
>> <stanislav.blinov at gmail.com> wrote:
>>
>> >On Saturday, 18 January 2014 at 02:38:28 UTC, H. S. Teoh wrote:
>> >
>> >>What's wrong with just letting the compiler infer the maximal function
>> >>attributes?
>> >
>> >...
>> >
>> >>Sadly, this only works for template functions currently -- so I still
>> >>had to annotate the various method()'s by hand, but if you were to  
>> turn
>> >>them into template functions too, their attributes will also be  
>> inferred
>> >>automatically.
>> >
>> >Oh, I see now where it went the wrong way. From my own example, of
>> >course :)
>> >
>> >Surely I was meaning:
>> >
>> >class Careful(T) {
>> >//...
>> >   void thisIsSoPolymorphic() nothrow(isNoThrow!(T.foo)) { ... }
>> >//...
>> >}
>> >
>> >Something to that extent. So that yes, the function itself is not
>> >a template.
>>
>> I was about to respond with a similar point, but it seems you are
>> understanding now how the nothrow inference works :)
>>
>> I can't think of a correct way to do this without repeating code,
>> since it has to be polymorphic (and therefore not a template),
>> nothrow inference only works on templates.
>
> See https://d.puremagic.com/issues/show_bug.cgi?id=10329 and the
> associated pull, which has just been merged. I haven't tested it myself
> yet, but it would appear that methods inside template classes should now
> have their methods' attributes inferred too?

Hm... how does that work on a virtual function? You may not know the body.

It does seem like a good solution for most cases.

>> The idea to be able to attach attributes/annotations based on
>> compile-time introspection would be a worthy addition to the
>> language IMO, but I really don't like the syntax you have outlined
>> (I see you don't like it either). Especially if you have to do all
>> the attributes this way.
>>
>> I think a "use the attributes of X" would be a general enough tool.
>>
>> Something like:
>>
>>  void thisIsSoPolymorphic() attrOf(T.foo)
> [...]
>
> Hmm. Would functionAttributes and SetFunctionAttributes in std.traits
> help somehow? Not sure how you might actually use them to declare
> methods with desired attributes, though. But maybe there's a way.

Not sure, it looks like SetFunctionAttributes only can be used to  
construct types, not declare functions.

-Steve


More information about the Digitalmars-d mailing list