Re: Converting a number to complex
On Saturday, 24 November 2012 at 07:27:18 UTC, Philippe Sigaud wrote: It's an is() expression (you cited my tutorial, there is an appendix on it). It recently became even more powerful, so the tutorial is not accurate any more. its time for another read through:) Template constraints might make for a good talk at the conf...
Re: Converting a number to complex
On 11/24/12 08:27, Philippe Sigaud wrote: What's even nicer with an is() expr is that the introspection info is accessible when used inside a static if. So with Artur's code, CT can be seen inside the true branch of the static if. Actually, static-ifs do not create a scope, so 'CT' leaks and is accessible after it's defined, not just inside the static-if branch: void main() { Complex!double c; static if (is(typeof(c) _ == Complex!CT, CT)) {} writeln(CT.stringof); } It has to work like that, as otherwise it would be impossible (well, harder) to conditionally compile /declarations/. Unfortunately the is-expressions don't handle aliases properly, at least with my old compiler here. So this does not work: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT)) enum isTempInst = true; else enum isTempInst = false; } artur
Re: Converting a number to complex
On Sat, Nov 24, 2012 at 11:30 AM, Artur Skawina art.08...@gmail.com wrote: On 11/24/12 08:27, Philippe Sigaud wrote: What's even nicer with an is() expr is that the introspection info is accessible when used inside a static if. So with Artur's code, CT can be seen inside the true branch of the static if. Actually, static-ifs do not create a scope, so 'CT' leaks and is accessible after it's defined, not just inside the static-if branch: You're right, I didn't think it through. Time to update my tutorial, then :) Unfortunately the is-expressions don't handle aliases properly, at least with my old compiler here. So this does not work: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT)) enum isTempInst = true; else enum isTempInst = false; } Works for me: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT...)) enum isTempInst = true; else enum isTempInst = false; } void main() { Tuple!(int,string) t = tuple(1, abc); writeln(typeof(t).stringof); writeln(isTempInst!(Tuple, typeof(t))); } (DMD 2.060)
Re: Converting a number to complex
On 11/24/12 12:30, Philippe Sigaud wrote: Unfortunately the is-expressions don't handle aliases properly, at least with my old compiler here. So this does not work: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT)) enum isTempInst = true; else enum isTempInst = false; } Works for me: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT...)) enum isTempInst = true; else enum isTempInst = false; } void main() { Tuple!(int,string) t = tuple(1, abc); writeln(typeof(t).stringof); writeln(isTempInst!(Tuple, typeof(t))); } (DMD 2.060) Then it's probably just my old compiler, gdc r748:ab99d67f04c2, using dmd 2.057, which thinks that that is-expression is always false. artur
Re: Converting a number to complex
On Thursday, 22 November 2012 at 16:09:46 UTC, Joshua Niehus wrote: On Thursday, 22 November 2012 at 15:47:11 UTC, Frederik Vagner wrote: I am trying to make a templated class to accept any numeric type: class example(Type) if (isNumeric(Type)) { Type k = to!Type(1); } however I always get a compiler erro stating I cannot make that conversion. How do I fix it? // phobos import std.stdio, std.conv, std.traits; class Example(T) if (isNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!double(); writeln(x.k); } Now do it for complex number please ^^
Re: Converting a number to complex
On Friday, 23 November 2012 at 12:39:59 UTC, Frederik Vagner wrote: Now do it for complex number please ^^ touche! import std.stdio, std.conv, std.traits, std.complex; template isComplexNumeric(T) { static if(is(NumericTypeOf!T)) { enum bool isComplexNumeric = is(NumericTypeOf!T); } else static if (is(T == Complex!double)) { enum bool isComplexNumeric = is(Complex!double); } // fillin real and float here... (e.g. is(Complex!real); etc... } class Example(T) if (isComplexNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!(Complex!double)(); writeln(x.k); auto y = new Example!double(); writeln(y.k); auto z = new Example!string(); // fail writeln(z.k); } A bit messy, but im sure there is some room for cleanup somewhere...
Re: Converting a number to complex
On Friday, 23 November 2012 at 16:11:25 UTC, Joshua Niehus wrote: A bit messy, but im sure there is some room for cleanup somewhere... Errata... (what i get for copy/pasting) import std.stdio, std.conv, std.traits, std.complex; template isComplexNumeric(T) { static if(isNumeric!T) { enum bool isComplexNumeric = true; } else static if (is(T == Complex!double)) { enum bool isComplexNumeric = true; } else { enum bool isComplexNumeric = false; } } class Example(T) if (isComplexNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!(Complex!double)(); writeln(x.k); auto y = new Example!double(); writeln(y.k); auto z = new Example!string(); writeln(z.k); } i did have to reference Philippe Sigaud's excellent book on Templates several times: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf
Re: Converting a number to complex
meh, couldn't resist: import std.stdio, std.conv, std.traits, std.complex; template isComplex(T) { static if (is(T == Complex!double)) { enum bool isComplex = true; } else static if (is(T == Complex!float)) { enum bool isComplex = true; } else static if (is(T == Complex!real)) { enum bool isComplex = true; } else { enum bool isComplex = false; } } template isComplexOrNumeric(T) { enum bool isComplexOrNumeric = (isComplex!T || isNumeric!T); } class Example(T) if (isComplexOrNumeric!T) { T k = to!T(1); }
Re: Converting a number to complex
On 11/23/12 18:06, Joshua Niehus wrote: meh, couldn't resist: import std.stdio, std.conv, std.traits, std.complex; template isComplex(T) { static if (is(T == Complex!double)) { enum bool isComplex = true; } else static if (is(T == Complex!float)) { enum bool isComplex = true; } else static if (is(T == Complex!real)) { enum bool isComplex = true; } else { enum bool isComplex = false; } } template isComplex(T) { static if (is(T _ == Complex!CT, CT)) enum isComplex = true; else enum isComplex = false; } artur
Re: Converting a number to complex
On Friday, 23 November 2012 at 18:45:53 UTC, Artur Skawina wrote: template isComplex(T) { static if (is(T _ == Complex!CT, CT)) enum isComplex = true; else enum isComplex = false; } artur oh wow didnt know u could do that. much nicer.
Re: Converting a number to complex
On Thursday, 22 November 2012 at 15:47:11 UTC, Frederik Vagner wrote: I am trying to make a templated class to accept any numeric type: class example(Type) if (isNumeric(Type)) { Type k = to!Type(1); } however I always get a compiler erro stating I cannot make that conversion. How do I fix it? // phobos import std.stdio, std.conv, std.traits; class Example(T) if (isNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!double(); writeln(x.k); }