is expression with static if matches wrong type

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 22 21:40:27 PDT 2015


On Saturday, 23 May 2015 at 04:35:55 UTC, tcak wrote:
> [code]
> import std.stdio;
>
> public void setMarker(M)( size_t markerIndex, M markerValue )
> 	if(
> 		is(M: ulong) || is(M: long) ||
> 		is(M: uint) || is(M: int) ||
> 		is(M: ushort) || is(M: short) ||
> 		is(M: ubyte) || is(M: byte) || is(M: char) || is(bool) ||
> 		is(M: double) ||
> 		is(M: float)
> 	)
> {
> 	static if( is(M: ulong) || is(M: long) ){
> 		std.stdio.writeln("Here 1: ", typeid(M));
> 	}
>
> 	else static if( is(M: uint) || is(M: int) ){
> 		std.stdio.writeln("Here 2: ", typeid(M));
> 	}
>
> 	else static if( is(M: ushort) || is(M: short) ){
> 		std.stdio.writeln("Here 3: ", typeid(M));
> 	}
>
> 	else static if( is(M: ubyte) || is(M: byte) || is(M: char) || 
> is(bool) ){
> 		std.stdio.writeln("Here 4: ", typeid(M));
> 	}
> }
>
> void main() {
> 	setMarker( 0, cast(ubyte)5 );
> }
>
> [/code]
>
>
>
> Result:
> Here 1: ubyte
>
>
> Compiler and OS:
> DMD 2.067.1  on Ubuntu 14.04 64-bit
>
>
> Expected:
> Here 4: ubyte
>
>
> is(my expectation) wrong?

I have found my mistake. is(M: type) matches if M can be 
converted to type automatically. Thus, it matches to ulong. I 
changed it to is(M == type) and it works correctly now.


More information about the Digitalmars-d-learn mailing list