www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is() and const

reply Andrea Fontana <nospam example.com> writes:
Run this code:

class PP {}

void what(T)(T val)
{
	static if (is(T =3D=3D int)) writeln ("T =3D=3D int");
	static if (is(T =3D=3D const(int))) writeln ("T =3D=3D const(int)");
	static if (is(T : int)) writeln ("T : int");
	static if (is(T =3D=3D PP)) writeln ("T =3D=3D PP");
	static if (is(T =3D=3D const(PP))) writeln ("T =3D=3D const(PP)");
	static if (is(T : PP)) writeln ("T : PP");
}

void main(string[] args)
{

	const int aa =3D 10;
	int ab;
=09
	const PP ba =3D new PP;
	PP bb =3D new PP;
=09
	writeln("- Testing const(int)");
	what(aa);
	writeln();
=09
	writeln("- Testing int");
	what(ab);
	writeln();
=09
	writeln("- Testing const(PP)");
	what(ba);
	writeln();
=09
	writeln("- Testing PP");
	what(bb);
	writeln();
=09
	return;
}

It says:

- Testing const(int)
T =3D=3D const(int)
T : int

- Testing int
T =3D=3D int
T : int

- Testing const(PP)
T =3D=3D const(PP)

- Testing PP
T =3D=3D PP
T : PP


So:
const(int) : int  <-- true
const(PP) : PP  <-- false

Is this behaviour correct?

And how can I check if T is of a certain class ignoring consts (and
avoiding double checks)?
Jul 18 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Andrea Fontana:

 const(int) : int  <-- true
 const(PP) : PP  <-- false

 Is this behaviour correct?
I think it's correct, and it's caused by the difference between value types and reference types.
 And how can I check if T is of a certain class ignoring consts 
 (and avoiding double checks)?
There are some different ways to do it, one of them is to use something like (untested): is(Unqual!typeof(x) == PP) Where Unqual is in Phobos, std.traits.Unqual. Bye, bearophile
Jul 18 2012
parent Andrea Fontana <nospam example.com> writes:
It seems to works (but i use Unqual!T directly)

Thank you :)

Il giorno mer, 18/07/2012 alle 11.13 +0200, bearophile ha scritto:

 Andrea Fontana:
=20
 const(int) : int  <-- true
 const(PP) : PP  <-- false

 Is this behaviour correct?
=20 I think it's correct, and it's caused by the difference between=20 value types and reference types. =20 =20
 And how can I check if T is of a certain class ignoring consts=20
 (and avoiding double checks)?
=20 There are some different ways to do it, one of them is to use=20 something like (untested): =20 is(Unqual!typeof(x) =3D=3D PP) =20 Where Unqual is in Phobos, std.traits.Unqual. =20 Bye, bearophile
Jul 18 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, July 18, 2012 10:43:23 Andrea Fontana wrote:
 So:
 const(int) : int  <-- true
const int is implicitly convertible to int, because it's a value type, and assigning a const int to an int (or vice versa) makes a copy.
 const(PP) : PP  <-- false
const PP is not implicitly convertible to PP, because it's a reference type. So, assigning a PP to a PP doesn't make a copy. It just copies the reference. And a const PP can't be converted to a mutable PP, because that would be dropping the const, invalidating const's guarantees. If PP were a struct with no pointers, arrays, or reference types (or it had a postblit constructor), then it would be a value type, and const PP _would_ implicitly convert to PP.
 Is this behaviour correct?
Yes.
 And how can I check if T is of a certain class ignoring consts (and
 avoiding double checks)?
is(Unqual!T == T) Unqual is in std.traits. - Jonathan M Davis
Jul 18 2012