www.digitalmars.com         C & C++   DMDScript  

D - null casts

reply "Pavel Minayev" <evilone omen.ru> writes:
I've raised this topic once, still...

I don't understand why null should be casted to whatever
type you want other than void*? It's so annoying! Just
look around, no other language has such a restriction:

calling WinAPI functions, since many arguments are
usually nulls. Just let null be compatible with any pointer
type. Or, even better, make void* compatible with all
pointers, like it was in C.
Dec 21 2001
next sibling parent "Walter" <walter digitalmars.com> writes:
You're probably right. -Walter

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a0044r$1m2p$1 digitaldaemon.com...
 I've raised this topic once, still...

 I don't understand why null should be casted to whatever
 type you want other than void*? It's so annoying! Just
 look around, no other language has such a restriction:

 calling WinAPI functions, since many arguments are
 usually nulls. Just let null be compatible with any pointer
 type. Or, even better, make void* compatible with all
 pointers, like it was in C.
Dec 21 2001
prev sibling next sibling parent reply "Walter" <walter digitalmars.com> writes:
I don't understand. The following works:

object test()
{
    return null;
}

works, as well as object*, int*, etc. Can you please post an example where
it doesn't? Thanks!

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a0044r$1m2p$1 digitaldaemon.com...
 I've raised this topic once, still...

 I don't understand why null should be casted to whatever
 type you want other than void*? It's so annoying! Just
 look around, no other language has such a restriction:

 calling WinAPI functions, since many arguments are
 usually nulls. Just let null be compatible with any pointer
 type. Or, even better, make void* compatible with all
 pointers, like it was in C.
Dec 21 2001
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a00pf9$24ri$1 digitaldaemon.com...
 I don't understand. The following works:

 object test()
 {
     return null;
 }

 works, as well as object*, int*, etc. Can you please post an example where
 it doesn't? Thanks!
Try this: import windows; int main(char[][] args) { HANDLE x = null; } HANDLE is a typedef for void*, and thus a pointer type, so it should be compatible with null, right? Still it isn't. As the result, when working with API heavily, you have to cast null to HANDLE all the time...
Dec 22 2001
parent "Walter" <walter digitalmars.com> writes:
Ok, I see the problem. It'll get fixed. -Walter

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a01g85$2ma7$1 digitaldaemon.com...
 Try this:

     import windows;

     int main(char[][] args)
     {
      HANDLE x = null;
     }

 HANDLE is a typedef for void*, and thus a pointer type, so it
 should be compatible with null, right? Still it isn't. As the
 result, when working with API heavily, you have to cast null
 to HANDLE all the time...
Dec 22 2001
prev sibling parent reply "Sean L. Palmer" <spalmer iname.com> writes:
I agree about NULL being compatible with other pointer types, but disagree
about void* being implicitly convertable to other pointer types.  That
leaves a gaping hole in the typechecking safety net that will end up causing
bugs.  That is why C++ "fixed" that problem.

Sean

"Pavel Minayev" <evilone omen.ru> wrote in message
news:a0044r$1m2p$1 digitaldaemon.com...
 I've raised this topic once, still...

 I don't understand why null should be casted to whatever
 type you want other than void*? It's so annoying! Just
 look around, no other language has such a restriction:

 calling WinAPI functions, since many arguments are
 usually nulls. Just let null be compatible with any pointer
 type. Or, even better, make void* compatible with all
 pointers, like it was in C.
Dec 22 2001
parent "Walter" <walter digitalmars.com> writes:
I fixed D so that null is compatible with other pointer and reference types,
but other void*'s will require an explicit cast. -Walter

"Sean L. Palmer" <spalmer iname.com> wrote in message
news:a02rnb$iij$1 digitaldaemon.com...
 I agree about NULL being compatible with other pointer types, but disagree
 about void* being implicitly convertable to other pointer types.  That
 leaves a gaping hole in the typechecking safety net that will end up
causing
 bugs.  That is why C++ "fixed" that problem.

 Sean

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:a0044r$1m2p$1 digitaldaemon.com...
 I've raised this topic once, still...

 I don't understand why null should be casted to whatever
 type you want other than void*? It's so annoying! Just
 look around, no other language has such a restriction:

 calling WinAPI functions, since many arguments are
 usually nulls. Just let null be compatible with any pointer
 type. Or, even better, make void* compatible with all
 pointers, like it was in C.
Dec 22 2001