digitalmars.D.learn - Typesafe variadic argument taking struct
- Satoshi (8/8) Oct 20 2016 Hello,
- rikki cattermole (2/10) Oct 20 2016 Because an int is not a Foo.
- Satoshi (4/18) Oct 20 2016 Oh, really? I didn't notice that... (sarcasm)
- Meta (6/8) Oct 20 2016 It's because for some weird reason, this type of varargs allows
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/10) Oct 20 2016 Being explicit about these things makes complex code more clear.
- Satoshi (10/24) Oct 20 2016 With class it works fine.
Hello, why ... cannot be used with structs? struct Foo { this(int a) { } } void bar(Foo foo...) { } bar(42);
Oct 20 2016
On 21/10/2016 1:48 AM, Satoshi wrote:Hello, why ... cannot be used with structs? struct Foo { this(int a) { } } void bar(Foo foo...) { } bar(42);Because an int is not a Foo.
Oct 20 2016
On Thursday, 20 October 2016 at 12:52:42 UTC, rikki cattermole wrote:On 21/10/2016 1:48 AM, Satoshi wrote:Oh, really? I didn't notice that... (sarcasm) If I change struct Foo to class Foo it works.Hello, why ... cannot be used with structs? struct Foo { this(int a) { } } void bar(Foo foo...) { } bar(42);Because an int is not a Foo.
Oct 20 2016
On Thursday, 20 October 2016 at 14:29:53 UTC, Satoshi wrote:Oh, really? I didn't notice that... (sarcasm) If I change struct Foo to class Foo it works.It's because for some weird reason, this type of varargs allows implicit construction of an object. I don't know why, it's just a feature that was added a long time ago and never removed. However as you can see, it doesn't work for structs and IMO you should never use it.
Oct 20 2016
On Thursday, 20 October 2016 at 12:48:34 UTC, Satoshi wrote:Hello, why ... cannot be used with structs? struct Foo { this(int a) { } } void bar(Foo foo...) { } bar(42);Being explicit about these things makes complex code more clear.
Oct 20 2016
On Thursday, 20 October 2016 at 16:04:00 UTC, Nordlöw wrote:On Thursday, 20 October 2016 at 12:48:34 UTC, Satoshi wrote:With class it works fine. https://dlang.org/spec/function.html#typesafe_variadic_functions I have struct Point(x, y) and I want to let the people call some methods with Point or just pass x and y argument. I can make Point as a class but it's not efficient. I think this void moveMouseTo(float x, float y); is same clear as void moveMouseTo(Point point);Hello, why ... cannot be used with structs? struct Foo { this(int a) { } } void bar(Foo foo...) { } bar(42);Being explicit about these things makes complex code more clear.
Oct 20 2016