Thread overview
Allow implicit template properties for templates with more than one member
Dec 23, 2006
Bill Baxter
Dec 23, 2006
Frits van Bommel
Dec 23, 2006
Daniel Keep
Dec 24, 2006
Thomas Kuehne
December 23, 2006
You can do this:

template Foo()
{
    const uint Foo = 5;
}

...

uint x = Foo!();

And it automatically looks up Foo!().Foo because it has one member named the same as the template.

What I'm suggesting is that the "have one member" part be changed.  I've written and seen a lot of template code that does stuff like this:

template SomethingImplementation(T, U)
{
    alias Blah!(T) myBlah;
    alias Boo!(U) myBoo;

    ...

    const uint result = someNumber;
}

template Something(T, U)
{
    alias SomethingImplementation!(T, U).result Something;
}

That is, an "implementation" template to do all the work, which has several members, and an "interface" template to make it easier to call the implementation template.  Pretty kludgy.

Would it really break anything if the rule were changed so that it would always just look for a member of the same name of the template, no matter how many members the template has?


December 23, 2006
Jarrett Billingsley wrote:
> You can do this:
> 
> template Foo()
> {
>     const uint Foo = 5;
> }
> 
> ...
> 
> uint x = Foo!();
> 
> And it automatically looks up Foo!().Foo because it has one member named the same as the template.
> 
> What I'm suggesting is that the "have one member" part be changed.  I've written and seen a lot of template code that does stuff like this:

Yes, this would be very nice to have.

I find myself wishing frequently I could do something like that when dealing with non-trivial return/parameter types.

template something(T) {
   alias [...] Ret;
   alias [...] A;
   alias [...] B;
   Ret something(A a, B b) { ... }
}

Where the [...]'s are things like typeof(T.member) or other such statically determined type based on T.

--bb
December 23, 2006
Jarrett Billingsley wrote:
> You can do this:
> 
> template Foo()
> {
>     const uint Foo = 5;
> }
> 
> ...
> 
> uint x = Foo!();
> 
> And it automatically looks up Foo!().Foo because it has one member named the same as the template.
> 
> What I'm suggesting is that the "have one member" part be changed.  I've written and seen a lot of template code that does stuff like this:

This has been proposed before, but (IIRC) with the added qualification that all other members should be private for it to work. Still hasn't happened though :(.
December 23, 2006
Frits van Bommel wrote:
> Jarrett Billingsley wrote:
> 
>> You can do this:
>>
>> template Foo()
>> {
>>     const uint Foo = 5;
>> }
>>
>> ...
>>
>> uint x = Foo!();
>>
>> And it automatically looks up Foo!().Foo because it has one member named the same as the template.
>>
>> What I'm suggesting is that the "have one member" part be changed.  I've written and seen a lot of template code that does stuff like this:
> 
> 
> This has been proposed before, but (IIRC) with the added qualification that all other members should be private for it to work. Still hasn't happened though :(.

Mmm; I agree with the extra restriction: the implicit property works iff the template has exactly one public member with the same name as the template itself.

	-- Daniel
December 24, 2006
Daniel Keep schrieb am 2006-12-23:
> Frits van Bommel wrote:
>> Jarrett Billingsley wrote:
>> 
>>> You can do this:
>>>
>>> template Foo()
>>> {
>>>     const uint Foo = 5;
>>> }
>>>
>>> ...
>>>
>>> uint x = Foo!();
>>>
>>> And it automatically looks up Foo!().Foo because it has one member named the same as the template.
>>>
>>> What I'm suggesting is that the "have one member" part be changed. I've written and seen a lot of template code that does stuff like this:
>> 
>> 
>> This has been proposed before, but (IIRC) with the added qualification that all other members should be private for it to work. Still hasn't happened though :(.
>
> Mmm; I agree with the extra restriction: the implicit property works iff the template has exactly one public member with the same name as the template itself.

The public-restriction is too weak.

#
# struct Foo{ int i = 1; }
#
# template Bar(){
#     private int i = 2;
#     public Foo Bar;
# }
#
# [...]
#
# int x = Bar.i; // 1 or 2?

If implemented, it should be:

The implicit property works if the template has exactly one accessible member and this member has the same name as the template.

Thomas