www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - conv.to for enums

http://d.puremagic.com/issues/show_bug.cgi?id=8143

This is easily doable for most types but there's one problem area:

    enum EF : float { C = 4.9 }
    float f = 4.9;
    EF en2 = to!EF(f);

Floating-point comparisons are.. unreliable (for most people unaware
of how floating-point really works - me included). I was thinking we
should either disallow calling to!() on enums with floating point base
types or provide an overload of to!() which has an additional
parameter which would be a comparison function that's set by the user.

It would work like this:

  static bool equal(float a, float b) { return feqrel(a, b) >= 24; }
  enum EF : float { C = 4.9 }
  float f = 4.9;
  EF enf1 = to!(EF, equal)(f);  // compile-time alias
  EF enf2 = to!EF(f, &equal);  // alternative syntax (runtime)

Thoughts?
Oct 24 2012