string <-> null/bool implicit conversion

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 20 13:09:51 PDT 2015


On 08/20/2015 09:08 PM, Steven Schveighoffer wrote:
> On 8/20/15 2:49 PM, Timon Gehr wrote:
>> On 08/20/2015 08:05 PM, H. S. Teoh via Digitalmars-d wrote:
>>> On Thu, Aug 20, 2015 at 01:50:11PM -0400, Steven Schveighoffer via
>>> Digitalmars-d wrote:
>>> [...]
>>>> The main reason why it caused issues is this nice idiom:
>>>>
>>>> if(auto arr = someFunction())
>>>> {
>>>>     // use arr
>>>> }
>>>>
>>>> This would HAVE to be split out to two statements, and the arr variable
>>>> would be scoped outside of the if statement.
>>> [...]
>>>
>>> I wish the language would accept:
>>>
>>>     if ((auto arr = someFunction()) !is null)
>>>     {
>>>         ...
>>>     }
>>>
>>> But IIRC, auto cannot be used except at the top level expression.
>>> ...
>>
>> You want !=, not !is.
>>
>
> Current behavior of if(auto arr = someFunction()) is to compare both
> pointer and length to 0, i.e. !is null.
> ...

I know, and it's horrible.

> His point is that if we could get rid of if(arr), then there would at
> least be a way to update existing (correct) code that doesn't involve
> refactoring if the given syntax was accepted.
> ...

Fair enough.

> This also would be acceptable (since not much code ever creates arrays
> with null pointer but non-zero length):
>
> if((auto arr = someFunction()).ptr)
>...

It would certainly be handy, also for other usages.


More information about the Digitalmars-d mailing list