www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error with constraints on a templated fuction

reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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!
Aug 25 2014
prev sibling next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
parent reply "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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:

 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.
Aug 25 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
parent "Jeremy DeHaan" <dehaan.jeremiah gmail.com> writes:
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:

 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.
Aug 25 2014
prev sibling next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
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
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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 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)
Aug 25 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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:
 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.
Aug 25 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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:
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 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.
Aug 25 2014
prev sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Aug 25 2014