digitalmars.D.learn - Error with constraints on a templated fuction
- Jeremy DeHaan (26/26) Aug 25 2014 I've done things like this before with traits and I figured that
- bearophile (5/7) Aug 25 2014 Try:
- Jeremy DeHaan (4/11) Aug 25 2014 That one compiles, and I'm assuming it works. I didn't know you
- ketmar via Digitalmars-d-learn (4/6) Aug 25 2014 On Mon, 25 Aug 2014 15:48:10 +0000
- Jeremy DeHaan (4/11) Aug 25 2014 Is its ability to be used as a function like this documented
- ketmar via Digitalmars-d-learn (6/8) Aug 25 2014 On Mon, 25 Aug 2014 16:11:27 +0000
- Jeremy DeHaan (3/12) Aug 25 2014 Awesome! Thanks so much.
- Jonathan M Davis via Digitalmars-d-learn (19/45) Aug 25 2014 On Mon, 25 Aug 2014 15:48:10 +0000
- H. S. Teoh via Digitalmars-d-learn (11/17) Aug 25 2014 The correct syntax is:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/15) Aug 25 2014 Almost... T is already a type; typeof(T) doesn't compile.
- H. S. Teoh via Digitalmars-d-learn (6/22) Aug 25 2014 Ah, right, it should be simply: if (is(T == dchar))
- Artur Skawina via Digitalmars-d-learn (8/10) Aug 25 2014 D is not quite that simple. ;)
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?
Aug 25 2014
Jeremy DeHaan:It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts?Try: if (is(T == dchar)) Bye, bearophile
Aug 25 2014
On Monday, 25 August 2014 at 15:52:20 UTC, bearophile wrote:Jeremy DeHaan: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!It compiles if I remove the 'if(typeof(T) is dchar)' section. Any thoughts?Try: if (is(T == dchar)) Bye, bearophile
Aug 25 2014
On Mon, 25 Aug 2014 15:48:10 +0000 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=20 thoughts?"is" should be used as function. here. i.e.: `if (is(T =3D=3D dchar))`
Aug 25 2014
On Monday, 25 August 2014 at 15:59:38 UTC, ketmar via Digitalmars-d-learn wrote:On Mon, 25 Aug 2014 15:48:10 +0000 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.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))`
Aug 25 2014
On Mon, 25 Aug 2014 16:11:27 +0000 Jeremy DeHaan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Is its ability to be used as a function like this documented=20 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.
Aug 25 2014
On Monday, 25 August 2014 at 16:20:24 UTC, ketmar via Digitalmars-d-learn wrote:On Mon, 25 Aug 2014 16:11:27 +0000 Jeremy DeHaan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Awesome! Thanks so much.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.
Aug 25 2014
On Mon, 25 Aug 2014 15:48:10 +0000 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
Aug 25 2014
On Mon, Aug 25, 2014 at 03:48:10PM +0000, 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 isThe 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)
Aug 25 2014
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 +0000, Jeremy DeHaan via Digitalmars-d-learn wrote:Almost... T is already a type; typeof(T) doesn't compile.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 isThe correct syntax is: if (is(typeof(T) == dchar))
Aug 25 2014
On Mon, Aug 25, 2014 at 05:10:18PM +0000, via Digitalmars-d-learn wrote:On Monday, 25 August 2014 at 17:05:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: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.On Mon, Aug 25, 2014 at 03:48:10PM +0000, Jeremy DeHaan via Digitalmars-d-learn wrote:Almost... T is already a type; typeof(T) doesn't compile.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 isThe correct syntax is: if (is(typeof(T) == dchar))
Aug 25 2014
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, whereasD 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
Aug 25 2014