On Sunday, 24 August 2025 at 03:13:38 UTC, David T. Oxygen wrote:
The code above has simplified. It defines some simple classes.
And now, I have a function:
```d
import std.algorithm.searching;
MyReal is_int_or_float(string inputs){
if(inputs.canFind('.'))
return new MyFloat(inputs);
else
return new MyInt(inputs);
}
```
This would be a great use-case for `std.sumtype.SumType`:
```d
import std.sumtype: SumType, match;
alias Number = SumType!(double, long);
Number isIntOrFloat(string input){
import std.algorithm.searching: canFind;
import std.conv: to;
if(input.canFind('.'))
return Number(input.to!double());
else
return Number(input.to!long());
}
void main(){
string input = "3.14";
Number result = isIntOrFloat(input);
import std.stdio: writeln;
result.match!(
(long x){ writeln("integral: ",x); },
(double x){ writeln("floating: ",x); },
);
}
```