digitalmars.D.learn - Is it possible to override the behavior of a type when used in a
- pineapple (2/2) Sep 10 2016 I've got a struct and it would be very convenient if I could
- ag0aep6g (14/16) Sep 10 2016 `if (value)` implies a cast to bool. Define opCast!bool and it gets call...
- pineapple (3/21) Sep 10 2016 Huh, I could've sworn that some time ago I tried that and it
- ag0aep6g (2/3) Sep 10 2016 I don't think so.
- Jonathan M Davis via Digitalmars-d-learn (6/33) Sep 10 2016 No. That's how it's been for years now - certainly well before TDPL was
- Steven Schveighoffer (7/10) Sep 12 2016 It's not that way in D1. I think it was added around the time operator
I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible?
Sep 10 2016
On 09/10/2016 04:10 PM, pineapple wrote:I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible?`if (value)` implies a cast to bool. Define opCast!bool and it gets called: ---- struct S { bool opCast(T : bool)() { return true; } } void main() { S value; import std.stdio: writeln; if (value) writeln("yup"); } ----
Sep 10 2016
On Saturday, 10 September 2016 at 14:24:23 UTC, ag0aep6g wrote:On 09/10/2016 04:10 PM, pineapple wrote:Huh, I could've sworn that some time ago I tried that and it didn't work. Was this a recent addition to the language?I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible?`if (value)` implies a cast to bool. Define opCast!bool and it gets called: ---- struct S { bool opCast(T : bool)() { return true; } } void main() { S value; import std.stdio: writeln; if (value) writeln("yup"); } ----
Sep 10 2016
On 09/10/2016 04:29 PM, pineapple wrote:Was this a recent addition to the language?I don't think so.
Sep 10 2016
On Saturday, September 10, 2016 14:29:33 pineapple via Digitalmars-d-learn wrote:On Saturday, 10 September 2016 at 14:24:23 UTC, ag0aep6g wrote:No. That's how it's been for years now - certainly well before TDPL was released - and I suspect that it was that way in D1 (though I've never really used D1, so I'm not sure). - Jonathan M DavisOn 09/10/2016 04:10 PM, pineapple wrote:Huh, I could've sworn that some time ago I tried that and it didn't work. Was this a recent addition to the language?I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible?`if (value)` implies a cast to bool. Define opCast!bool and it gets called: ---- struct S { bool opCast(T : bool)() { return true; } } void main() { S value; import std.stdio: writeln; if (value) writeln("yup"); } ----
Sep 10 2016
On 9/10/16 6:47 PM, Jonathan M Davis via Digitalmars-d-learn wrote:No. That's how it's been for years now - certainly well before TDPL was released - and I suspect that it was that way in D1 (though I've never really used D1, so I'm not sure).It's not that way in D1. I think it was added around the time operator overloads were changed to templates. Combined with changelog search and this hint, I think this is correct: https://github.com/dlang/dlang.org/commit/b60bf3881ccad5e09cfb6e773c3927637ecca70c#diff-87ae512b433ac9f86b715f03fa17cb0e Version 2.041 added it. -Steve
Sep 12 2016