digitalmars.D.bugs - [Issue 6573] New: Add isOneOf to std.traits
- d-bugmail puremagic.com (31/31) Aug 29 2011 http://d.puremagic.com/issues/show_bug.cgi?id=6573
- d-bugmail puremagic.com (28/28) Aug 29 2011 http://d.puremagic.com/issues/show_bug.cgi?id=6573
- d-bugmail puremagic.com (24/24) Aug 30 2011 http://d.puremagic.com/issues/show_bug.cgi?id=6573
- d-bugmail puremagic.com (20/20) Aug 30 2011 http://d.puremagic.com/issues/show_bug.cgi?id=6573
http://d.puremagic.com/issues/show_bug.cgi?id=6573 Summary: Add isOneOf to std.traits Product: D Version: D2 Platform: Other OS/Version: Windows Status: NEW Severity: enhancement Priority: P2 Component: Phobos AssignedTo: nobody puremagic.com ReportedBy: andrej.mitrovich gmail.com 19:15:23 PDT --- I think Steven Schveighoffer came up with this originally: template isOneOf(X, T...) { static if (!T.length) enum bool isOneOf = false; else static if (is (X == T[0])) enum bool isOneOf = true; else enum bool isOneOf = isOneOf!(X, T[1..$]); } It's very useful as a replacement for multiple checks on a single type parameter, e.g.: void foo(T)(T t) if (is(T == Type1) || is(T == Type2) || is(T == Type3)) turns into: void foo(T)(T t) if (isOneOf!(T, Type1, Type2, Type3)) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 29 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6573 20:16:51 PDT --- However the code will have to be slightly improved. If you accidentally pass a tuple followed by a single type things can get weird. E.g.: template isOneOf(X, T...) { static if (!T.length) enum bool isOneOf = false; else static if (is (X == T[0])) enum bool isOneOf = true; else enum bool isOneOf = isOneOf!(X, T[1..$]); } void test(T...)(T t) { static assert (isOneOf!(T, double)); // passes by mistake static assert (isOneOf!(double, T)); // ok, fails properly } void main() { test(1, 2, 3); } So that's something to improve. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 29 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6573 bearophile_hugs eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs eml.cc Something like this? (A different recursion base): template SameTypes(X, T...) { static if (T.length) enum bool SameTypes = is(X == T[0]) && SameTypes!(X, T[1 .. $]); else enum bool SameTypes = true; } void main() { static assert (!SameTypes!(int, int, int, double)); static assert (!SameTypes!(double, int, int, int)); static assert (SameTypes!(double)); static assert (SameTypes!(int)); static assert (SameTypes!(int, typeof(1))); static assert (SameTypes!(int, typeof(1), typeof(2))); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 30 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6573 kennytm gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |kennytm gmail.com I think this should be in std.typetuple, and also can be implemented using anySatisfy. template isOneOf(X, T...) { enum isOneOf = anySatisfy!(staticEqualsTo!X, T); } private template staticEqualsTo(X) { template staticEqualsTo(Y) { enum staticEqualsTo = isSame!(X, Y); // ^ isSame is a private template in std.typetuple. } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 30 2011