I was thinking earlier today that it'd be nice if felix had strong 
typedefs, or typedefs that create a whole new type instead of just an 
alias. My canonical reasoning for this is trying to implement a vector 
math library. It makes sense to build on top of felix arrays, but some 
times you want operations that have the same name performing over the 
same fundamental structure, that do different things. For instance, 
multiplying vectors. Say we implement mul to equal the cross product:

typedef rowvec4[T] = array[T,4];
typedef colvec4[T] = array[T,4];

fun mul[T](x:rowvec4[T], y:colvec4[T]) => 
[|x.[0]*y.[0],x.[1]*y.[1],x.[2]*y.[2],x.[3]*y.[3]|];

Now, if we want to implement quaternions, we'd have:

typedef quat[T] = array[T,4];

fun mul[T])(x:quat[T], y:quat[T]) =>
  [|
    x.[3]*y.[0] + x.[0]*y.[3] + x.[1]*y.[2] - x.[2]*y.[1],
    x.[3]*y.[1] + x.[1]*y.[3] + x.[2]*y.[0] - x.[0]*x.[2],
    x.[3]*y.[2] + x.[2]*y.[3] + x.[0]*y.[1] - x.[1]*y.[0],
    x.[3]*y.[3] - x.[0]*y.[0] - x.[1]*y.[1] - x.[2]*y.[2],
  |]
;

But this will conflict with the first definition. The traditional answer 
would be to put both in their own respective modules, but this won't 
help the case when we want the modules opened up. Or, if we had strong 
typedefs, this could be handled transparently, as these two functions 
would be operating over two separate types and thus are overloadable. If 
you ever wanted to use a regular array, you could just cast it in using 
the "x:rowvec4" casting.

Of course, having type aliases is also quite useful as well. Since the 
word "type" is already a keyword, and "type foo = int" doesn't currently 
work, what if we used that for this purpose? Then we'd have

typedef quat[T] = array[T,4];

Be the type that requires explicit constructors. The quaternion 
multiplication could then use the _ctor and be used like this:

fun mul[T])(x:quat[T], y:quat[T]) =>
  quat[T](
    x.[3]*y.[0] + x.[0]*y.[3] + x.[1]*y.[2] - x.[2]*y.[1],
    x.[3]*y.[1] + x.[1]*y.[3] + x.[2]*y.[0] - x.[0]*x.[2],
    x.[3]*y.[2] + x.[2]*y.[3] + x.[0]*y.[1] - x.[1]*y.[0],
    x.[3]*y.[3] - x.[0]*y.[0] - x.[1]*y.[1] - x.[2]*y.[2],
  )
;


What do you all think?

-e


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to