Error with constraints on a templated fuction
I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. immutable(T)[] toString(T)(const(T)* str) if(typeof(T) is dchar)//this is where the error is { return str[0..strlen(str)].idup; //I have strlen defined for each *string } I was going to add some || sections for the other string types, but this one won't even compile. src/dsfml/system/string.d(34): Error: found ')' when expecting '.' following dchar src/dsfml/system/string.d(35): Error: found '{' when expecting identifier following 'dchar.' src/dsfml/system/string.d(36): Error: found 'return' when expecting ')' src/dsfml/system/string.d(36): Error: semicolon expected following function declaration src/dsfml/system/string.d(36): Error: no identifier for declarator str[0 .. strlen(str)] src/dsfml/system/string.d(36): Error: no identifier for declarator .idup src/dsfml/system/string.d(37): Error: unrecognized declaration It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts?
Re: Error with constraints on a templated fuction
Jeremy DeHaan: It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts? Try: if (is(T == dchar)) Bye, bearophile
Re: Error with constraints on a templated fuction
On Mon, 25 Aug 2014 15:48:10 + Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts? is should be used as function. here. i.e.: `if (is(T == dchar))` signature.asc Description: PGP signature
Re: Error with constraints on a templated fuction
On Monday, 25 August 2014 at 15:59:38 UTC, ketmar via Digitalmars-d-learn wrote: On Mon, 25 Aug 2014 15:48:10 + Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts? is should be used as function. here. i.e.: `if (is(T == dchar))` Is its ability to be used as a function like this documented anywhere? I looked and could not find it.
Re: Error with constraints on a templated fuction
On Monday, 25 August 2014 at 15:52:20 UTC, bearophile wrote: Jeremy DeHaan: It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts? Try: if (is(T == dchar)) Bye, bearophile That one compiles, and I'm assuming it works. I didn't know you could use is this way. I've only seen it as an 'obj1 is obj2' sort of way. Thanks much!
Re: Error with constraints on a templated fuction
On Mon, 25 Aug 2014 16:11:27 + Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Is its ability to be used as a function like this documented anywhere? I looked and could not find it. http://dlang.org/concepts.html template constraints is a special case. is not a real function here, it's more like special predicate syntax. signature.asc Description: PGP signature
Re: Error with constraints on a templated fuction
On Monday, 25 August 2014 at 16:20:24 UTC, ketmar via Digitalmars-d-learn wrote: On Mon, 25 Aug 2014 16:11:27 + Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Is its ability to be used as a function like this documented anywhere? I looked and could not find it. http://dlang.org/concepts.html template constraints is a special case. is not a real function here, it's more like special predicate syntax. Awesome! Thanks so much.
Re: Error with constraints on a templated fuction
On Mon, 25 Aug 2014 15:48:10 + Jeremy DeHaan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. immutable(T)[] toString(T)(const(T)* str) if(typeof(T) is dchar)//this is where the error is { return str[0..strlen(str)].idup; //I have strlen defined for each *string } I was going to add some || sections for the other string types, but this one won't even compile. src/dsfml/system/string.d(34): Error: found ')' when expecting '.' following dchar src/dsfml/system/string.d(35): Error: found '{' when expecting identifier following 'dchar.' src/dsfml/system/string.d(36): Error: found 'return' when expecting ')' src/dsfml/system/string.d(36): Error: semicolon expected following function declaration src/dsfml/system/string.d(36): Error: no identifier for declarator str[0 .. strlen(str)] src/dsfml/system/string.d(36): Error: no identifier for declarator .idup src/dsfml/system/string.d(37): Error: unrecognized declaration It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts? As the others have pointed out, you need to do is(T == dchar). The way you used is the is operator and it checks for bitwise equality (most frequently used for comparing pointers), and it's a runtime operation, whereas the is that you need to use in a template constraint is an is expression, which is a compile time operation: http://dlang.org/expression.html#IsExpression is expressions actually get pretty complicated in their various forms, but the most basic two are probably is(T == dchar), which checks that the two types ar the same, and is(T : dchar), which checks that T implictly converts to dchar. Another commonly used one is is(typeof(foo)). typeof(foo) gets the type of foo and will result in void if foo doesn't exist, and is(void) is false, whereas is(someOtherType) is true, so it's frequently used to check whether something is valid. The Phobos source code is littered with examples (especially in std.algorithm, std.range, and std.traits), since is expressions are frequently used in template constraints. - Jonathan M Davis
Re: Error with constraints on a templated fuction
On Mon, Aug 25, 2014 at 03:48:10PM +, Jeremy DeHaan via Digitalmars-d-learn wrote: I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. immutable(T)[] toString(T)(const(T)* str) if(typeof(T) is dchar)//this is where the error is The correct syntax is: if (is(typeof(T) == dchar)) When comparing two values for equality (i.e., are these two values equal to each other), use if (a == b). When comparing two variables for identity (i.e., do these two references point to the same data), use if (a is b). When comparing two types, use is(A == B). T -- Verbing weirds language. -- Calvin ( Hobbes)
Re: Error with constraints on a templated fuction
On Monday, 25 August 2014 at 17:05:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Aug 25, 2014 at 03:48:10PM +, Jeremy DeHaan via Digitalmars-d-learn wrote: I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. immutable(T)[] toString(T)(const(T)* str) if(typeof(T) is dchar)//this is where the error is The correct syntax is: if (is(typeof(T) == dchar)) Almost... T is already a type; typeof(T) doesn't compile.
Re: Error with constraints on a templated fuction
On Mon, Aug 25, 2014 at 05:10:18PM +, via Digitalmars-d-learn wrote: On Monday, 25 August 2014 at 17:05:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Aug 25, 2014 at 03:48:10PM +, Jeremy DeHaan via Digitalmars-d-learn wrote: I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. immutable(T)[] toString(T)(const(T)* str) if(typeof(T) is dchar)//this is where the error is The correct syntax is: if (is(typeof(T) == dchar)) Almost... T is already a type; typeof(T) doesn't compile. Ah, right, it should be simply: if (is(T == dchar)) Should've read more carefully before replying. :-P T -- It only takes one twig to burn down a forest.
Re: Error with constraints on a templated fuction
On 08/25/14 18:52, Jonathan M Davis via Digitalmars-d-learn wrote: Another commonly used one is is(typeof(foo)). typeof(foo) gets the type of foo and will result in void if foo doesn't exist, and is(void) is false, whereas D is not quite that simple. ;) static assert(is(void)==true); (a) `typeof(invalid)` is an error; (b) the `is(...)` expression swallows errors; hence (a)+(b) - static assert(is(typeof(invalid))==false); artur