user defined literals

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Fri Mar 25 09:05:15 PDT 2011


On Fri, 25 Mar 2011 11:46:08 -0400, Steven Schveighoffer wrote:
> On Fri, 25 Mar 2011 11:14:26 -0400, Trass3r <un at known.com> wrote:
>> Steven Schveighoffer Wrote:
>>> Also, complex is the clear and obvious beneficiary
>> Yep, though I think complex literals would still better be handled by
>> the compiler specifically so that 5 + 3i is transformed to Complex(5,3)
>> rather than 5 + Complex(0, 3)
> 
> We have quite powerful mixin abilities such that I think something like:
> 
> complex!("5 + 3i")
> 
> could be rewritten as Complex!double(5, 3) without compiler help.  And
> like you said, the feature as proposed does not do the ideal thing.

I believe the plan is to keep complex literals even after the built-in 
complex types are gone, but I don't think this is necessary at all.  The 
following works just fine:

  enum I = Complex!float(0, 1);

  auto z = 5.0 + 3*I;

The compiler should be able to fold the sum into a single constant at 
compile time.

-Lars


More information about the Digitalmars-d mailing list