Jump to page: 1 2
Thread overview
Error with constraints on a templated fuction
Aug 25, 2014
Jeremy DeHaan
Aug 25, 2014
bearophile
Aug 25, 2014
Jeremy DeHaan
Aug 25, 2014
ketmar
Aug 25, 2014
Jeremy DeHaan
Aug 25, 2014
ketmar
Aug 25, 2014
Jeremy DeHaan
Aug 25, 2014
Jonathan M Davis
Aug 25, 2014
H. S. Teoh
Aug 25, 2014
Marc Schütz
Aug 25, 2014
H. S. Teoh
Aug 25, 2014
Artur Skawina
August 25, 2014
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?
August 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
August 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 thoughts?
"is" should be used as function. here. i.e.: `if (is(T == dchar))`


August 25, 2014
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!
August 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:
>
>> 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.
August 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 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.


August 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:
>
>> 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.
August 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
August 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 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)
August 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:
>> 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.
« First   ‹ Prev
1 2