Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 06, 2006 Conditional ? bug | ||||
---|---|---|---|---|
| ||||
class Test { int foo() { return 1; } } void main() { Test test = null; int i = test != null ? test.foo() : 0; } This throws access violation exception |
October 06, 2006 Re: Conditional ? bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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. |
October 06, 2006 Re: Conditional ? bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tydr Schnubbis | 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.
|
October 06, 2006 Re: Conditional ? bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tydr Schnubbis | 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
|
October 06, 2006 Re: Conditional ? bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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
|
October 06, 2006 Re: Conditional ? bug [OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | 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.
|
October 06, 2006 Re: Conditional ? bug [OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | 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
|
October 06, 2006 Re: Conditional ? bug [OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | 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
|
October 07, 2006 Re: Conditional ? bug [OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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
|
Copyright © 1999-2021 by the D Language Foundation