www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UFCS for types?

reply simendsjo <simendsjo gmail.com> writes:
Is is possible to use UFCS for types to simulate static members?

struct S {
     static void f();
}

void ext(S)() {
     S.f();
}

void main() {
     ext!S(); // ok
     S.ext(); // Error: no property 'ext' for type 'S'
}
Mar 31 2012
parent James Miller <james aatch.net> writes:
On 31 March 2012 21:37, simendsjo <simendsjo gmail.com> wrote:
 Is is possible to use UFCS for types to simulate static members?

 struct S {
 =C2=A0 =C2=A0static void f();
 }

 void ext(S)() {
 =C2=A0 =C2=A0S.f();
 }

 void main() {
 =C2=A0 =C2=A0ext!S(); // ok
 =C2=A0 =C2=A0S.ext(); // Error: no property 'ext' for type 'S'
 }
As far as I know UFCS just rewrites `a.f(b,c)` to `f(a,b,c)`, so probably not, since `S.ext()` would be re-written to `ext(S)`, which obviously makes no sense. Its pretty much just syntatic sugar with a few disambiguation rules. Basically you're asking for UFCS for template parameters, but that would introduce too many ambiguities, especially considering things like alias parameters: ext(alias T, U)(U a); int foo =3D 1; foo.ext() //does the alias T get the value, or the U a? If you just remember that UFCS is just for functions, not templates, or rather its Uniform Function-Call Syntax, not Uniform Template-Instantiation Syntax (UTIS?). Because even ext(T)(); is just sugar for: template ext(T) { void ext(); } -- James Miller
Apr 01 2012