opCast'ing strings

2017-11-12 Thread helxi via Digitalmars-d-learn
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.stdi

Re: opCast'ing strings

2017-11-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 13 November 2017 at 01:03:17 UTC, helxi wrote: 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? to!string calls a function called `string toString() {}` on the struct, no

Re: opCast'ing strings

2017-11-13 Thread bauss via Digitalmars-d-learn
On Monday, 13 November 2017 at 01:12:59 UTC, Adam D. Ruppe wrote: On Monday, 13 November 2017 at 01:03:17 UTC, helxi wrote: 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? to!strin

Re: opCast'ing strings

2017-11-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, November 13, 2017 12:40:39 bauss via Digitalmars-d-learn wrote: > On Monday, 13 November 2017 at 01:12:59 UTC, Adam D. Ruppe wrote: > > On Monday, 13 November 2017 at 01:03:17 UTC, helxi wrote: > >> In this program, casting using to does not work as intended > >> (returning 23/11) on the