Implicit type conversion depending on assignment
apz28
home at home.com
Thu Mar 23 21:21:36 UTC 2023
On Thursday, 23 March 2023 at 14:36:11 UTC, Alexander Zhirov
wrote:
> On Thursday, 23 March 2023 at 14:19:31 UTC, user1234 wrote:
>> omg, let's rewrite this...
>
Or abuse opBinary
struct MyVal
{
string value;
T opBinary(string op, T)(T rhs)
if (op == "+")
{
import std.conv : convto = to;
static if (is(T==int))
return convto!T(value) + rhs;
else static if (is(T==float))
return convto!T(value) + rhs;
else
static assert(0);
}
alias opBinary this;
}
void main()
{
auto a = MyVal("100");
auto b = MyVal("11.2");
int MyInt = a + 0;
float myFloat = b + 0.0f;
}
More information about the Digitalmars-d-learn
mailing list