Eponymous Aggregates

JS js.mdnq at gmail.com
Sat Aug 10 13:10:53 PDT 2013


On Saturday, 10 August 2013 at 18:28:41 UTC, Simen Kjaeraas wrote:
> On 2013-08-10, 14:58, JS wrote:
>
>> e.g.,
>>
>> interface A
>> {
>>     static T A(T)() { ... }
>> }
>>
>> can be used as A!T instead of A.A!T. Same for classes and 
>> structs. If you want a use case I'm not going to stop you from 
>> coming up with one... so feel free.
>
> Tried with DMD 2.063.2, and I'm unable to make A!T compile. Are 
> you sure
> you've written the code you intended to?
>
> For reference, this is the code I tried:
>
> interface A {
>     static T A(T)() {
>         return T.init;
>     }
> }
>
> void main() {
>     A!int a;        // Error: template instance A!(int) A is 
> not a template declaration, it is a interface
>     auto b = A!int; // Error: template instance A!(int) A is 
> not a template declaration, it is a interface
> }
>
> This code, of course, works:
>
> template A(T) {
>     interface A {
>     }
> }
>
> void main() {
>     A!int a;
> }

No, the code does not compile as it is a proposal.

The issue is simple to allow interfaces to have Eponymous static 
members. This makes it easier to use in some cases. Your template 
example just creates a template... but does nothing as far as 
inheritance.


interface iFactory(A)
{
     static A iFactory(Args...)(Args args) { return new A; }
     // ....
}

class Q : iFactory!Q
{
    // ....
}

auto q = iFactory!Q();

instead of

auto q = iFactory!Q.iFactory();

or

auto q = Q.iFactory()

The last case being only acceptable if Q inherits from 
iFactory(which it may not).

This is simply generalizing eponymous templates to interfaces, 
classes, structs, etc.



More information about the Digitalmars-d mailing list