opCast'ing strings
helxi
brucewayneshit at gmail.com
Mon Nov 13 01:03:17 UTC 2017
struct Fraction
{
private:
int numerator = 1, denominator = 1;
public:
string opCast(T : string)() const
{
import std.conv : to;
return numerator.to!string() ~ "/" ~ denominator.to!string();
}
}
void main()
{
import std.stdio, std.conv;
Fraction n = Fraction(23, 11);
writeln(n.to!string(), " ", n.opCast!string);
}
In this program, casting using to does not work as intended
(returning 23/11) on the struct. However, calling opCast directly
seems to do the job. Why is that?
More information about the Digitalmars-d-learn
mailing list