digitalmars.D.learn - Simple template constraint question
- WhatMeWorry (27/27) Sep 19 2015 Does D provide complete template constraint granularity?
- Daniel Kozak (22/56) Sep 19 2015 http://dlang.org/expression.html#IsExpression
- WhatMeWorry (4/27) Sep 19 2015 Thanks! I keep going straight to std.traits and forget about the
- anonymous (9/35) Sep 19 2015 If the whole goal is to get how many members T has:
Does D provide complete template constraint granularity? In other words, I want to only accept structs in the template below. I've find the isAggregateType which is close but no cigar. Am I missing some other filters? And a more open ended question. Is there a more elegant solution for the below function? Maybe a one-liner? I have a knack for making simple solutions complex :) // Calculate the number of components for openGL generic // vertex attribute. Must be 1, 2, 3, 4. // isAggregateType constraint accepts class, union, struct, and interface int getNumberComponents(T)(T someStruct) if (isAggregateType!(T)) { int members = 0; foreach (i, member; someStruct.tupleof) { //writefln("Member %s:", i); //writefln(" type : %s", typeof(member).stringof); //writefln(" value: %s", member); members++; } return members; }
Sep 19 2015
WhatMeWorry p=C3=AD=C5=A1e v So 19. 09. 2015 v 17:09 +0000:Does D provide complete template constraint granularity? =20 In other words, I want to only accept structs in the template=20 below. I've find the isAggregateType which is close but no cigar. Am I=20 missing some other filters? =20 And a more open ended question. Is there a more elegant solution=20 for the below function? Maybe a one-liner? I have a knack for making=20 simple solutions complex :) =20 =20 =20 // Calculate the number of components for openGL generic // vertex attribute. Must be 1, 2, 3, 4. =20 // isAggregateType constraint accepts class, union, struct, and=20 interface =20 int getNumberComponents(T)(T someStruct) if (isAggregateType!(T)) { int members =3D 0; foreach (i, member; someStruct.tupleof) { //writefln("Member %s:", i); //writefln(" type : %s", typeof(member).stringof); //writefln(" value: %s", member); members++; } return members; }http://dlang.org/expression.html#IsExpression 3. is ( Type =3D=3D TypeSpecialization ) import std.stdio; struct S { } class C { } void f(T)(T someStruct) if (is (T =3D=3D struct)) { writeln("struct"); } void f(T)(T notStruct) if (!is(T =3D=3D struct)) { writeln("not a struct"); } void main() { auto c =3D new C(); auto s =3D S(); f(c); // not a struct f(s); // struct }
Sep 19 2015
On Saturday, 19 September 2015 at 17:18:23 UTC, Daniel Kozak wrote:WhatMeWorry píše v So 19. 09. 2015 v 17:09 +0000:Thanks! I keep going straight to std.traits and forget about the is(Type)[...]http://dlang.org/expression.html#IsExpression 3. is ( Type == TypeSpecialization ) import std.stdio; struct S { } class C { } void f(T)(T someStruct) if (is (T == struct)) { writeln("struct"); } void f(T)(T notStruct) if (!is(T == struct)) { writeln("not a struct"); } void main() { auto c = new C(); auto s = S(); f(c); // not a struct f(s); // struct }
Sep 19 2015
On Saturday 19 September 2015 19:09, WhatMeWorry wrote:And a more open ended question. Is there a more elegant solution for the below function? Maybe a one-liner? I have a knack for making simple solutions complex :) // Calculate the number of components for openGL generic // vertex attribute. Must be 1, 2, 3, 4. // isAggregateType constraint accepts class, union, struct, and interface int getNumberComponents(T)(T someStruct) if (isAggregateType!(T)) { int members = 0; foreach (i, member; someStruct.tupleof) { //writefln("Member %s:", i); //writefln(" type : %s", typeof(member).stringof); //writefln(" value: %s", member); members++; } return members; }If the whole goal is to get how many members T has: ---- template getNumberComponents(T) if (is(T == struct)) { enum getNumberComponents = T.tupleof.length; } ---- Or you can write `T.tupleof.length` directly, of course.
Sep 19 2015