View mode: basic / threaded / horizontal-split · Log in · Help
July 13, 2012
Re: just an idea (!! operator)
I don't know much about C#, but in C#, isn't EVERYTHING a 
reference type? Meaning it always makes sense to check if 
"myobject is null".

I'm still no expert in D either, but what would(should) happen if 
you tried to call ?: or ?. on a value type? Writing "s is null" 
gives "Error: incompatible types for ((s) is (null)): 'S' and 
'typeof(null)'"

I'd guess that:
*"s ?: S(5)" would give a compile error, since it the call makes 
no sense?
*"s?.someFunction" would simply resolve as "s.somFunction"?
   - This could avoid problems in templates that want to use ?.

The operators *look* convenient, but isn't there a risk of 
ambiguity for D? But again, I'm not expert in either languages.
July 13, 2012
Re: just an idea (!! operator)
On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:
> I don't know much about C#, but in C#, isn't EVERYTHING a 
> reference type? Meaning it always makes sense to check if 
> "myobject is null".
>
> I'm still no expert in D either, but what would(should) happen 
> if you tried to call ?: or ?. on a value type? Writing "s is 
> null" gives "Error: incompatible types for ((s) is (null)): 'S' 
> and 'typeof(null)'"
>
> I'd guess that:
> *"s ?: S(5)" would give a compile error, since it the call 
> makes no sense?
> *"s?.someFunction" would simply resolve as "s.somFunction"?
>    - This could avoid problems in templates that want to use ?.
>
> The operators *look* convenient, but isn't there a risk of 
> ambiguity for D? But again, I'm not expert in either languages.

Regarding the ?: operator I agree with Andrei that is should be 
handled by a coalesce() function instead.

Can you identify any ambiguity with an ?. operator.

/Jonas
July 13, 2012
Re: just an idea (!! operator)
"Jonas Drewsen" , dans le message (digitalmars.D:172242), a écrit :
> Can you identify any ambiguity with an ?. operator.

? could be the begining of a ternary operator, and . the module scope 
indicator, or the beginning of a (badly) written float number.

Both case can be disambiguated by the presence of the ':' in the case 
of a ternary operator.

I don't think '?' has currently any other meaning in D.
July 13, 2012
Re: just an idea (!! operator)
On 13/07/2012 13:31, Jonas Drewsen wrote:
> On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:
>> I don't know much about C#, but in C#, isn't EVERYTHING a reference
>> type? Meaning it always makes sense to check if "myobject is null".
>>
>> I'm still no expert in D either, but what would(should) happen if you
>> tried to call ?: or ?. on a value type? Writing "s is null" gives
>> "Error: incompatible types for ((s) is (null)): 'S' and 'typeof(null)'"
>>
>> I'd guess that:
>> *"s ?: S(5)" would give a compile error, since it the call makes no
>> sense?
>> *"s?.someFunction" would simply resolve as "s.somFunction"?
>> - This could avoid problems in templates that want to use ?.
>>
>> The operators *look* convenient, but isn't there a risk of ambiguity
>> for D? But again, I'm not expert in either languages.
>
> Regarding the ?: operator I agree with Andrei that is should be handled
> by a coalesce() function instead.
>

Due to lazy being broken, this cannot be @safe, pure or nothrow.
July 13, 2012
Re: just an idea (!! operator)
On 2012-07-13 11:24, Jonas Drewsen wrote:

> I Agree. Looking at the Groovy link from Christophe I noticed their Safe
> Navigation Operator ?.
>
> Basicly it returns the final value in a dereference chain or null if any
> objects in the chain are null:
>
> // a is null if user or address or street is null. No exception thrown.
> auto a = user?.address?.street;

But what would the static type of "a" be? I tried this:

class Foo {}

void main ()
{
    auto a = true ? new Foo : new Object;
    static assert (is(typeof(a) == Object));
}

And it passes. You loose static typing.

How would the type of "a" be resolved if there's a field/method that 
returns a value type in the chain? Compile time error?

-- 
/Jacob Carlborg
July 13, 2012
Re: just an idea (!! operator)
On Friday, 13 July 2012 at 12:51:07 UTC, Jacob Carlborg wrote:
> On 2012-07-13 11:24, Jonas Drewsen wrote:
>> // a is null if user or address or street is null. No 
>> exception thrown.
>> auto a = user?.address?.street;
>
> But what would the static type of "a" be?

I don't think there is much doubt in this regard: the type that
you get when none of the »intermediates« evaluate to null, i.e.
typeof(street). Anything else doesn't make much sense in my
regard.

I guess that this operator is only really worth it in languages
where every type is nullable, though.

David
July 13, 2012
Re: just an idea (!! operator)
On Friday, 13 July 2012 at 13:46:10 UTC, David Nadlinger wrote:
> I guess that this operator is only really worth it in languages
> where every type is nullable, though.
>
> David

It might mean identity (return the argument unchanged) for value 
types.
July 13, 2012
Re: just an idea (!! operator)
"Roman D. Boiko" , dans le message (digitalmars.D:172259), a écrit :
> On Friday, 13 July 2012 at 13:46:10 UTC, David Nadlinger wrote:
>> I guess that this operator is only really worth it in languages
>> where every type is nullable, though.
>>
>> David
> 
> It might mean identity (return the argument unchanged) for value 
> types.

It might mean: give me the default I provide as an extra argument:

Example:
car?.driver?.name ?: "anonymous";

rewrites:
car? car.driver? car.driver.name? car.driver.name? car.driver.name
                                                :anonymous
                               :anonymous
              :anonymous
  :anonymous
July 14, 2012
Re: just an idea (!! operator)
On Friday, 13 July 2012 at 09:49:22 UTC, monarch_dodra wrote:
> I don't know much about C#, but in C#, isn't EVERYTHING a 
> reference type? Meaning it always makes sense to check if 
> "myobject is null".

No, C# has value types (enums, primitives, and user-defined
types) which are not nullable. The null coalescing operator (and
null?.dot, if it existed) is still useful for nullable types of
course; plus, any value type has a nullable counterpart (e.g.
int? = nullable int).
Next ›   Last »
1 2 3 4
Top | Discussion index | About this forum | D home