View mode: basic / threaded / horizontal-split · Log in · Help
September 11, 2012
Re: auto limitation?
On Tuesday, 11 September 2012 at 19:07:52 UTC, Simen Kjaeraas 
wrote:
> On Tue, 11 Sep 2012 20:57:07 +0200, Maxim Fomin 
> <maxim@maxim-fomin.ru> wrote:
>
>> I think it is UB rather than a bug. The spec says that return 
>> types must match exactly. AFAIK auto is a feature to infer 
>> return type, not to magically adjust to multiple incompatible 
>> types.
>
> I'd be surprised if this were not a bug. My expectation would 
> be that the
> types be combined as with the ?: operator, just like 
> std.traits.CommonType.
>
> If this is not a bug, it's certainly worth filing as an 
> enhancement request.

It can do, but in compile time. At runtime it cannot know what 
type is returned (I suppose, but I am not expert in such things). 
Variant works because it does it in compile time, but buggy 
examples published so far do it at runtime.
September 11, 2012
Re: auto limitation?
With multiple alias this this should work.

int getInt() const { return this.ivalue; }
alias getInt this;
float getFloat() const { return this.fvalue; }
alias getFloat this;

Or with a class method for implicit conversion.
September 11, 2012
Re: auto limitation?
On 09/11/2012 08:57 PM, Maxim Fomin wrote:
>
> I think it is UB rather than a bug.

No, this is a bug.

> The spec says that return types must match exactly.

Exactly. If it was UB, the spec would say the behaviour is undefined if 
they don't match exactly.


> AFAIK auto is a feature to infer return type, not to
> magically adjust to multiple incompatible types.
>
September 11, 2012
Re: auto limitation?
On Tuesday, 11 September 2012 at 19:27:40 UTC, Timon Gehr wrote:
> On 09/11/2012 08:57 PM, Maxim Fomin wrote:
>>
>> I think it is UB rather than a bug.
>
> No, this is a bug.
>
>> The spec says that return types must match exactly.
>
> Exactly. If it was UB, the spec would say the behaviour is 
> undefined if they don't match exactly.
>
>
>> AFAIK auto is a feature to infer return type, not to
>> magically adjust to multiple incompatible types.

So what? Should my code work or not?
September 11, 2012
Re: auto limitation?
On 09/11/2012 09:29 PM, Namespace wrote:
> On Tuesday, 11 September 2012 at 19:27:40 UTC, Timon Gehr wrote:
>> On 09/11/2012 08:57 PM, Maxim Fomin wrote:
>>>
>>> I think it is UB rather than a bug.
>>
>> No, this is a bug.
>>
>>> The spec says that return types must match exactly.
>>
>> Exactly. If it was UB, the spec would say the behaviour is undefined
>> if they don't match exactly.
>>
>>
>>> AFAIK auto is a feature to infer return type, not to
>>> magically adjust to multiple incompatible types.
>
> So what? Should my code work or not?

According to the spec it should fail compilation.
September 11, 2012
Re: auto limitation?
Ah ok. That's sad.
September 11, 2012
Re: auto limitation?
On Tuesday, 11 September 2012 at 19:35:58 UTC, Timon Gehr wrote:
> According to the spec it should fail compilation.

After rethinking it I agree that bug is in faulty compilation 
(assuming spec is correct), not in returning zero at runtime, 
which was thought previously.
September 11, 2012
Re: auto limitation?
My final implementation:
http://dpaste.dzfl.pl/4d2a045a

Any suggestions?
September 11, 2012
Re: auto limitation?
Namespace:

> I have this code, but it works not as expected:
> http://dpaste.dzfl.pl/6ce5b4dd

I suggest to file a bug:


auto foo(bool b) {
    final switch (b) {
        case true:
            return 10;
        case false:
            return 20.0;
    }
}
void main() {
    import std.stdio: writeln;
    writeln(foo(true));
    writeln(foo(false));
}


The acceptable results are a compile-time error for type 
mismatch, or a conversion of both literals to double. Probably 
the second is better. But a silent bit-level cast is not 
acceptable.

Bye,
bearophile
September 11, 2012
Re: auto limitation?
On Tuesday, 11 September 2012 at 21:13:02 UTC, bearophile wrote:
> Namespace:
>
>> I have this code, but it works not as expected:
>> http://dpaste.dzfl.pl/6ce5b4dd
>
> I suggest to file a bug:
>
>
> auto foo(bool b) {
>     final switch (b) {
>         case true:
>             return 10;
>         case false:
>             return 20.0;
>     }
> }
> void main() {
>     import std.stdio: writeln;
>     writeln(foo(true));
>     writeln(foo(false));
> }
>
>
> The acceptable results are a compile-time error for type 
> mismatch, or a conversion of both literals to double. Probably 
> the second is better. But a silent bit-level cast is not 
> acceptable.
>
> Bye,
> bearophile

Sure that is the first i will do tomorrow.
But so far no suggestions?
1 2 3
Top | Discussion index | About this forum | D home