www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Default value of class

reply "David B. Held" <dheld codelogicconsulting.com> writes:
Can anyone tell me the expected runtime behavior of this program?

class Foo
{ }

void main()
{
     Foo foo;
     assert(foo == null);
}

It's certainly not what I expected, and I want to know if I should file 
a bug report or not.

Dave
May 19 2007
parent reply Dave <Dave_member pathlink.com> writes:
David B. Held wrote:
 Can anyone tell me the expected runtime behavior of this program?
 
 class Foo
 { }
 
 void main()
 {
     Foo foo;
     assert(foo == null);
 }
 
 It's certainly not what I expected, and I want to know if I should file 
 a bug report or not.
 
 Dave

Try 'assert(foo is null);'
May 19 2007
parent reply davidb <ta-nospam-zz gmx.at> writes:
Dave schrieb:
 David B. Held wrote:
 Can anyone tell me the expected runtime behavior of this program?

 class Foo
 { }

 void main()
 {
     Foo foo;
     assert(foo == null);
 }

 It's certainly not what I expected, and I want to know if I should 
 file a bug report or not.

 Dave

Try 'assert(foo is null);'

See http://www.digitalmars.com/d/operatoroverloading.html
 Note: Comparing a reference to a class object against null  should be 
 done as:
 if (a is null)

 if (a == null)

 if (a.opEquals(null))


which will fail if a is null because it can't use it to reference to opEquals() david
May 20 2007
parent reply "David B. Held" <dheld codelogicconsulting.com> writes:
davidb wrote:
 Dave schrieb:
 David B. Held wrote:
 Can anyone tell me the expected runtime behavior of this program?

 class Foo
 { }

 void main()
 {
     Foo foo;
     assert(foo == null);
 }

 It's certainly not what I expected, and I want to know if I should 
 file a bug report or not.

 Dave

Try 'assert(foo is null);'

See http://www.digitalmars.com/d/operatoroverloading.html > Note: Comparing a reference to a class object against null should be > done as: >> if (a is null) > and not as: >> if (a == null) > The latter is converted to: >> if (a.opEquals(null)) which will fail if a is null because it can't use it to reference to opEquals()

Aha...thanks guys. However, I am disturbed by two things: 1) that this looks more like VB than C++ ;), and 2) that everyone in this thread is named "David". Hmm... Dave
May 20 2007
next sibling parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
David B. Held wrote:
 davidb wrote:
 Dave schrieb:
 David B. Held wrote:
 Can anyone tell me the expected runtime behavior of this program?

 class Foo
 { }

 void main()
 {
     Foo foo;
     assert(foo == null);
 }

 It's certainly not what I expected, and I want to know if I should 
 file a bug report or not.

 Dave

Try 'assert(foo is null);'

See http://www.digitalmars.com/d/operatoroverloading.html > Note: Comparing a reference to a class object against null should be > done as: >> if (a is null) > and not as: >> if (a == null) > The latter is converted to: >> if (a.opEquals(null)) which will fail if a is null because it can't use it to reference to opEquals()

Aha...thanks guys. However, I am disturbed by two things: 1) that this looks more like VB than C++ ;), and 2) that everyone in this thread is named "David". Hmm... Dave

o_O It only makes sense though, for a language named "D". We have our army of Davids, and the (Ch|K)ris's... at least there's just one Walter. Could you imagine... -- Chris (!) Nicholson-Sauls
May 20 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"David B. Held" <dheld codelogicconsulting.com> wrote in message 
news:f2ptar$2022$1 digitalmars.com...
 this looks more like VB than C++

Well, maybe VB got some things right ;)
May 20 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Jarrett Billingsley wrote:
 "David B. Held" <dheld codelogicconsulting.com> wrote in message 
 news:f2ptar$2022$1 digitalmars.com...
 this looks more like VB than C++

Well, maybe VB got some things right ;)

It's still the only language I've seen that lets me pass temporaries and literals as reference arguments... :) -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 20 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
news:f2qf8m$2v85$1 digitalmars.com...
 Jarrett Billingsley wrote:
 "David B. Held" <dheld codelogicconsulting.com> wrote in message
 news:f2ptar$2022$1 digitalmars.com...
 this looks more like VB than C++

Well, maybe VB got some things right ;)

It's still the only language I've seen that lets me pass temporaries and literals as reference arguments... :)

Doesn't C++ have those ... && reference arguments? Like int&& ? You can pass temps to those. Well I guess those are in C++09, so they're not part of the language yet.
May 20 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:f2qgao$3176$1 digitalmars.com...
 Doesn't C++ have those ... && reference arguments?  Like int&& ?  You can 
 pass temps to those.  Well I guess those are in C++09, so they're not part 
 of the language yet.

Though D can do this: struct S { int x; static S opCall(int x) { S s; s.x = x; return s; } } void foo(ref S s) { writefln(s.x); } void main() { foo(S(4)); } Which I guess is pretty close.
May 20 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Jarrett Billingsley wrote:
 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
 news:f2qgao$3176$1 digitalmars.com...
 Doesn't C++ have those ... && reference arguments?  Like int&& ?  You can 
 pass temps to those.  Well I guess those are in C++09, so they're not part 
 of the language yet.

Though D can do this: struct S { int x; static S opCall(int x) { S s; s.x = x; return s; } } void foo(ref S s) { writefln(s.x); } void main() { foo(S(4)); } Which I guess is pretty close.

That's interesting; could that be NVRO? That said, this still doesn't work...
 import std.stdio;

 void foo(ref int a)
 {
     writefln("a: %s", a);
 }

 void main()
 {
     foo(42);
 }

H:\WINDOWS\system32\cmd.exe /c bud -clean -exec refargs refargs.d(11): Error: constant 42 is not an lvalue :( This is a pain because I discovered that using ref arguments for my vector library can net me a *big* speed increase, even more so once I put in SSE optimisations. The problem is that if I can't pass literals or temporaries, then it'll render the library much harder to use. Then again, maybe since the vectors are structs, the above will work... need to give that a shot when I'm not so busy. -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 20 2007