Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 23, 2015 is expression with static if matches wrong type | ||||
---|---|---|---|---|
| ||||
[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? |
May 23, 2015 Re: is expression with static if matches wrong type | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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.
|
May 24, 2015 Re: is expression with static if matches wrong type | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On Saturday, 23 May 2015 at 04:40:28 UTC, tcak wrote: > [snip] Yup, you need to use == to match the exact type. Btw, you can use enum templates from std.traits, to accomplish the same with less code: public void setMarker(M)( size_t markerIndex, M markerValue ) if(isScalarType!M) { //... } http://dlang.org/phobos/std_traits.html#isScalarType |
May 24, 2015 Re: is expression with static if matches wrong type | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZombineDev | On Sunday, 24 May 2015 at 14:46:52 UTC, ZombineDev wrote:
> [snip]
Correction: not exactly the same, because isScalar also allows wchar, dchar and const and immutable versions of those 'scalar' types.
|
Copyright © 1999-2021 by the D Language Foundation