www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - comparing typedefed type to null

reply Rick Mann <rmann-d-lang latencyzero.com> writes:
In writing code against the Mac OS X Carbon API, I often have a need to do this:

struct __Foo {};
typedef Foo*   FooRef;
typedef FooRef     BarRef;

{
  BarRef br = ...;
  FooRef fr = ...;
  if (br == null)  {} // complain
  if (fr == null) {} ok

GDC 0.22/DMD 1.004 complains about the comparison to br: "Error: incompatible
types for ((br) == (null)): 'BarRef' and 'void*'

I can avoid it by aliasing BarRef instead of typedefing it, but I have the need
to allow a BarRef to be passed to FooRef parameters, but not the other way
around. The typedef gives me that.
Mar 01 2007
next sibling parent reply mike <vertex gmx.at> writes:
Am 02.03.2007, 01:40 Uhr, schrieb Rick Mann <rmann-d-lang latencyzero.co=
m>:

 In writing code against the Mac OS X Carbon API, I often have a need t=
o =
 do this:

 struct __Foo {};
 typedef Foo*   FooRef;
 typedef FooRef     BarRef;

 {
   BarRef br =3D ...;
   FooRef fr =3D ...;
   if (br =3D=3D null)  {} // complain
   if (fr =3D=3D null) {} ok

 GDC 0.22/DMD 1.004 complains about the comparison to br: "Error:  =
 incompatible types for ((br) =3D=3D (null)): 'BarRef' and 'void*'

 I can avoid it by aliasing BarRef instead of typedefing it, but I have=
=
 the need to allow a BarRef to be passed to FooRef parameters, but not =
=
 the other way around. The typedef gives me that.
You should always use the "is" operator to check for null: ' if (br is null) { ... } -Mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Mar 02 2007
parent =?ISO-8859-15?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
mike wrote:

 You should always use the "is" operator to check for null:
 
 ' if (br is null) { ... }
Using == is perfectly valid for pointers (such as those). --anders
Mar 02 2007
prev sibling next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Rick Mann" <rmann-d-lang latencyzero.com> wrote in message 
news:es7rp3$153q$1 digitalmars.com...
 In writing code against the Mac OS X Carbon API, I often have a need to do 
 this:

 struct __Foo {};
 typedef Foo*   FooRef;
 typedef FooRef     BarRef;

 {
  BarRef br = ...;
  FooRef fr = ...;
  if (br == null)  {} // complain
  if (fr == null) {} ok

 GDC 0.22/DMD 1.004 complains about the comparison to br: "Error: 
 incompatible types for ((br) == (null)): 'BarRef' and 'void*'

 I can avoid it by aliasing BarRef instead of typedefing it, but I have the 
 need to allow a BarRef to be passed to FooRef parameters, but not the 
 other way around. The typedef gives me that.
template realType(T) { static if(is(T U == typedef)) alias realType!(U) realType; else alias T realType; } bool isNull(T)(T val) { return (cast(realType!(T))val is null); } void main() { BarRef br; FooRef fr; if (isNull(br)) {} } :S There's got to be a better way..
Mar 02 2007
prev sibling parent Rick Mann <rmann-d-lang latencyzero.com> writes:
mike Wrote:

 You should always use the "is" operator to check for null:
 
 ' if (br is null) { ... }
This behaves exactly the same as '=='. :-(
Mar 03 2007