Template args to UDA's

Steven Schveighoffer schveiguy at yahoo.com
Tue May 28 18:43:00 PDT 2013


On Tue, 28 May 2013 21:08:42 -0400, Manu <turkeyman at gmail.com> wrote:

> On 29 May 2013 10:46, Diggory <diggsey at googlemail.com> wrote:
>
>> Can you not move the template outside the attribute like this?:
>>
>> template func(string T) {
>>     @attribute("target", T) void func(...);
>> }
>>
>
> Yes, that's Kenji's initial suggestion, which I can use for now, but it
> feels like a workaround.
> It feels pretty lame to have to wrap select functions in an arbitrary  
> outer
> scope, and it also has some subtle implications; how does
> auto-complete/intellisense see it? As a template, or as whatever it
> resolves to?

It's not a workaround, nor arbitrary.

In fact:

func(string T)(...)

is EXACTLY equivalent to:

template func(string T)
{
    func(...)
}

The compiler implicitly converts the former to the latter.

In fact, the formal name of func!"foo"() is func!"foo".func() in both  
cases.

> The latter would imply some pretty powerful semantic analysis, and if
> there's static logic inside the template? The IDE basically won't work
> properly for any of these functions.

Any conditional compilation breaks IFTI.  This is required, since the  
"magic" of IFTI means it must partially instantiate the template to see  
whether the function parameters match.  It could very easily get stuck in  
the weeds through some arbitrary conditional compilation.

> Either way, the attribute certainly looks like it's part of the
> declaration, I think any reasoning programmer would assume that it is.  
> It's
> only a DMD implementation detail that says otherwise, and it's not
> particularly intuitive to a programmer.

On this, I agree.  But it is important to remember that a function  
template is first a template and then a function.  When you apply  
attributes to it, you are applying it to the outer-most piece -- the  
template.  And IATI (implicit attribute template instantiation) isn't  
available yet :)

I think it's awesome that IFTI handles this messy detail, and it would be  
nice if uda's were part of that messy detail.

-Steve


More information about the Digitalmars-d mailing list