www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Conditional ? bug

reply Max Samuha <maxter i.com.ua> writes:
class Test
{
	int foo()
	{
		return 1;
	}
}

void main()
{
	Test test = null;
	int i = test != null ? test.foo() : 0;
}

This throws access violation exception
Oct 06 2006
parent reply Tydr Schnubbis <fake address.dude> writes:
Max Samuha wrote:
 class Test
 {
 	int foo()
 	{
 		return 1;
 	}
 }
 
 void main()
 {
 	Test test = null;
 	int i = test != null ? test.foo() : 0;
 }
 
 This throws access violation exception

From http://www.digitalmars.com/d/expression.html#EqualExpression : "For class and struct objects, the expression (a == b) is rewritten as a.opEquals(b), and (a != b) is rewritten as !a.opEquals(b)." So you have to use "test != null ? test.foo() : 0" for that kind of thing. Just "test ? test.foo() : 0" works too.
Oct 06 2006
next sibling parent Tydr Schnubbis <fake address.dude> writes:
Tydr Schnubbis wrote:
 So you have to use "test != null ? test.foo() : 0" for that kind of 
 thing.  Just "test ? test.foo() : 0" works too.

Oops, I meant "test !is null ? test.foo() : 0". "is" and "!is" compares the references, it doesn't turn into a call to opEquals.
Oct 06 2006
prev sibling parent reply Max Samuha <maxter i.com.ua> writes:
On Fri, 06 Oct 2006 13:51:21 +0200, Tydr Schnubbis <fake address.dude>
wrote:

Max Samuha wrote:
 class Test
 {
 	int foo()
 	{
 		return 1;
 	}
 }
 
 void main()
 {
 	Test test = null;
 	int i = test != null ? test.foo() : 0;
 }
 
 This throws access violation exception

From http://www.digitalmars.com/d/expression.html#EqualExpression : "For class and struct objects, the expression (a == b) is rewritten as a.opEquals(b), and (a != b) is rewritten as !a.opEquals(b)." So you have to use "test != null ? test.foo() : 0" for that kind of thing. Just "test ? test.foo() : 0" works too.

Sorry, my fault. I should have used !is or simply test. Thanks
Oct 06 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Max Samuha wrote:

So you have to use "test != null ? test.foo() : 0" for that kind of 
thing.  Just "test ? test.foo() : 0" works too.

Sorry, my fault. I should have used !is or simply test. Thanks

Not entirelly your fault, "== null" and "!= null" is common enough to have the D compiler detect them at compile time... Or it could just be defined to "false", like it is in Java. (i.e. "For any non-null reference x, x == null equals false") --anders
Oct 06 2006
prev sibling parent reply Serg Kovrov <kovrov no.spam> writes:
I like PHP5 syntax for that matter: use == / '!=' to compare values of 
objects, and === / !== (they call it 'identity operators') to compare 
references.

This 'identity operators' could be used to distinct, say, true (bool) 
from 1 (int), etc.. Nice stuff.

-- 
serg.
Oct 06 2006
next sibling parent xs0 <xs0 xs0.com> writes:
Serg Kovrov wrote:
 I like PHP5 syntax for that matter: use == / '!=' to compare values of 
 objects, and === / !== (they call it 'identity operators') to compare 
 references.
 
 This 'identity operators' could be used to distinct, say, true (bool) 
 from 1 (int), etc.. Nice stuff.

=== and !== were in D as well, but supposedly == can't be distinguished from === in some fonts, so they were changed to is and !is.. xs0
Oct 06 2006
prev sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Serg Kovrov wrote:

 I like PHP5 syntax for that matter: use == / '!=' to compare values of 
 objects, and === / !== (they call it 'identity operators') to compare 
 references.

We had those in D too... They got renamed to 'is"'and '!is' in 0.126. --anders
Oct 06 2006
parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Well, close, but what he's also saying is that PHP (since 4 actually) 
also dropped implicit conversions for them - for example:

bool foo = true;
int bar = 1;

if (foo is bar)
	writefln("This happens in D but not in PHP.");

This gave you a much faster compare, as well.  Still, I think it makes 
sense the way it works now in D, personally.

-[Unknown]


 Serg Kovrov wrote:
 
 I like PHP5 syntax for that matter: use == / '!=' to compare values of 
 objects, and === / !== (they call it 'identity operators') to compare 
 references.

We had those in D too... They got renamed to 'is"'and '!is' in 0.126. --anders

Oct 06 2006