equivariant functions

Christopher Wright dhasenan at gmail.com
Sun Oct 12 13:08:45 PDT 2008


Andrei Alexandrescu wrote:
> class A { A clone(); }
> class B : A { B clone(); }

...

> Here are some examples:
> 
> a) Simple equivariance
> 
> typeof(s) stripl(const(char)[] s);
> 
> b) Parameterized equivariance
> 
> typeof(s) stripl(S)(S s) if (isSomeString!S);
> 
> c) Equivariance of field:
> 
> typeof(s.ptr) getpointer(const(char)[] s);
> 
> d) Equivariance inside a class/struct declaration:
> 
> class S
> {
>     typeof(this) clone();
>     typeof(this.field) getfield();
>     int field;
> }
> 
> What do you think? I'm almost afraid to post this.
> 
> 
> Andrei

All your examples use typeof(something). Is the intent to make this 
example work?
 > class A { A clone(); }
 > class B : A { B clone(); }

I take it that this would also extend to delegates?

class A {}
class B : A {}

void foo (A delegate (A) dg) { }
B bar (B b) { }
foo (&bar);

If you require "typeof(something)", I'd never use this feature. 
Otherwise, I'd use it sometimes.



More information about the Digitalmars-d mailing list