Re: auto limitation?

2012-09-12 Thread Namespace
Done, thank you. :)

Re: auto limitation?

2012-09-11 Thread Ali Çehreli
On 09/11/2012 11:10 AM, Namespace wrote: I have this code, but it works not as expected: http://dpaste.dzfl.pl/6ce5b4dd I get 0 instead of 42 if my type is Int. My value is correct (as you can see) but writeln prints still 0 instead of 42. I think auto compiles first to float and cannot

Re: auto limitation?

2012-09-11 Thread Ali Çehreli
On 09/11/2012 11:40 AM, Namespace wrote: Here is a reduced code: import std.stdio; enum Type { Int, Float } auto foo(Type t) { final switch (t) { case Type.Int: return 42; case Type.Float: return 1.5; } } void main() { writeln(foo(Type.Int)); writeln(foo(Type.Float)); } The

Re: auto limitation?

2012-09-11 Thread Namespace
That's not what i want. I would avoid casting if it's possible. I hope it is a bug and will fixed in 2.061.

Re: auto limitation?

2012-09-11 Thread Maxim Fomin
On Tuesday, 11 September 2012 at 18:32:47 UTC, Ali Çehreli wrote: Here is a reduced code: I guess it may be reduced to: auto foo(bool val) { if (val) return 42; else return 1.5; } void main() { assert(foo(true) == 42); // assertion failure assert(foo(false) == 1.5);

Re: auto limitation?

2012-09-11 Thread Simen Kjaeraas
On Tue, 11 Sep 2012 20:48:25 +0200, Ali Çehreli acehr...@yahoo.com wrote: Or you can write or find a template that produces the largest type among the members of a union. std.traits.CommonType. -- Simen

Re: auto limitation?

2012-09-11 Thread Namespace
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. But i thought Variant is one. ;)

Re: auto limitation?

2012-09-11 Thread Maxim Fomin
On Tuesday, 11 September 2012 at 19:03:56 UTC, Namespace 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. But i thought Variant is one. ;) I

Re: auto limitation?

2012-09-11 Thread Namespace
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.

Re: auto limitation?

2012-09-11 Thread Maxim Fomin
On Tuesday, 11 September 2012 at 19:07:52 UTC, Simen Kjaeraas wrote: On Tue, 11 Sep 2012 20:57:07 +0200, Maxim Fomin ma...@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

Re: auto limitation?

2012-09-11 Thread Timon Gehr
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

Re: auto limitation?

2012-09-11 Thread Timon Gehr
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

Re: auto limitation?

2012-09-11 Thread Namespace
Ah ok. That's sad.

Re: auto limitation?

2012-09-11 Thread Maxim Fomin
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.

Re: auto limitation?

2012-09-11 Thread Namespace
My final implementation: http://dpaste.dzfl.pl/4d2a045a Any suggestions?

Re: auto limitation?

2012-09-11 Thread bearophile
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:

Re: auto limitation?

2012-09-11 Thread Namespace
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:

Re: auto limitation?

2012-09-11 Thread Denis Shelomovskij
11.09.2012 22:48, Ali Çehreli пишет: return to!(ReturnType!(typeof(foo)))(42); typeof(return) See http://dlang.org/declaration.html#typeof Anyway don't use it as a type of the first return. You will get: --- Error: cannot use typeof(return) inside function foo with inferred return

Re: auto limitation?

2012-09-11 Thread Jonathan M Davis
On Tuesday, September 11, 2012 23:31:43 Namespace wrote: Sure that is the first i will do tomorrow. But so far no suggestions? Use a cast. auto _cannot_ be different types depending on what you return. It must always resolve to the same type. So, in this case, either it needs to resolve to

Re: auto limitation?

2012-09-11 Thread bearophile
Namespace: But so far no suggestions? This seems to work, but solutions that use cast() are sometimes fragile (and dangerous): auto foo(bool b) { final switch (b) { case false: return 20.0; case true: return cast(typeof(return))10; } } void

Re: auto limitation?

2012-09-11 Thread Jonathan M Davis
On Tuesday, September 11, 2012 23:55:40 bearophile wrote: Namespace: But so far no suggestions? This seems to work, but solutions that use cast() are sometimes fragile (and dangerous): auto foo(bool b) { final switch (b) { case false: return 20.0;

Re: auto limitation?

2012-09-11 Thread Jonathan M Davis
On Tuesday, September 11, 2012 15:14:43 Jonathan M Davis wrote: If you're going to use a cast, then use one where you give the type explicitly. Using typeof(return) is just asking for it when the compiler is clearly already having issues with the return type. And actually, in all honesty, I

Re: auto limitation?

2012-09-11 Thread Namespace
You mean so? ref Variant get() { return this._num; // with Variant _num; } alias get this; and then: int i = zahl.get!int? No. Then I use my solution as you can see on DPaste. If Variant works only this way I can also set float to the return type an cast if I need to int. And with my

Re: auto limitation?

2012-09-11 Thread Jonathan M Davis
On Wednesday, September 12, 2012 00:29:32 Namespace wrote: You mean so? ref Variant get() { return this._num; // with Variant _num; } alias get this; and then: int i = zahl.get!int? No. Then I use my solution as you can see on DPaste. If Variant works only this way I can also set

Re: auto limitation?

2012-09-11 Thread Graham Fawcett
On Tuesday, 11 September 2012 at 21:31:17 UTC, Namespace wrote: 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) {