static check for float type or existence of a function overload

Bill Baxter wbaxter at gmail.com
Tue Oct 24 14:30:05 PDT 2006


I'd like to do something like this:
     static if (is(Type.epsilon)) {
         // Type is fp type
         alias Type RType;
         static final bool isfloat = true;
     } else {
         // type is not fp type
         alias real RType;
         static final bool isfloat = false;
     }

I thought the is(Type.epsilon) would fail only if Type didn't have an 
epsilon, but it seems to fail no matter what.

Is there another way to achieve this other than enumeration of the float 
types, i.e.
  static if (is(Type==float) || is(Type==double) || is(Type==real))...

I'd rather use duck typing here, i.e. if it has some property that makes 
it float-like, then do one thing, otherwise do another.

Actually what I *really* want to do is just special case types for which 
a particular funtion is not defined.  E.g. sqrt().  If sqrt() doesn't 
exist for the type then I'd like to do something else (because it won't 
compile with the call to sqrt in there...)

Is there a way to achieve that other than the enumeration of the things 
I know have a sqrt?  Problem with that is a user can easily provide a 
sqrt for their type.  So I have no way of knowing in advance what types 
have sqrt functions and which don't.

--bb



More information about the Digitalmars-d-learn mailing list