dmd command line scripting experiments and observations
Witold Baryluk
witold.baryluk at gmail.com
Mon Dec 25 12:16:46 UTC 2023
On Monday, 25 December 2023 at 11:59:35 UTC, Witold Baryluk wrote:
...
>
> Inability to opt-in to allow implicit opCast casting are making
> it not possible to develop fully dynamic and easy to use
> solution.
>
Was thinking a bit after posting, and maybe there is some hope:
So, one of the possibly limited hacks would be to force `double`
return type on `opBinary` when used with arithmetic operators
Instead of
```d
auto opBinary(string op)(const ref DT other) const {
if (numeric() && other.numeric()) {
const n = number();
const m = other.number();
return DT(to!string(mixin("n " ~ op ~ " m")));
}
throw new Exception("cannot perform " ~ op ~ " on string");
}
```
we do
```d
double opBinary(string op)(const ref DT other) const if (op ==
"+" || op == "-" || op == "*" | op == "/" || op == "^^" || op ==
"|" || op == "&" || op == "^"){
if (numeric() && other.numeric()) {
const n = number();
const m = other.number();
return mixin("n " ~ op ~ " m");
}
throw new Exception("cannot perform " ~ op ~ " on string");
}
string opBinary(string op)(const ref DT other) const if (op ==
"~") {
if (!numeric() && !other.numeric()) {
return mixin("x_ " ~ op ~ " other.x_");
}
throw new Exception("cannot perform " ~ op ~ " on
non-string");
}
...
// more overloads
// ...
```
This is quite limit tho in general. What if I want to also
support things more types than just double, and do it efficiently
(cdouble, BigInt, other custom types).
More information about the Digitalmars-d
mailing list