You can extend a binary operator using the ternary operator (?<-) and an intermediate type.

1
2
3
4
5
6
7
8
9
10
type VectorOperators = VectorOperators with 
    static member (?<-) (scalar: float,VectorOperators,vector: #Vector<float>) =
        DenseVector.init vector.Count (fun i -> scalar - vector.[i])
 
    static member (?<-) (vector: #Vector<float>,VectorOperators,scalar: float) =
        DenseVector.init vector.Count (fun i -> vector.[i] - scalar)

    static member inline (?<-) (a,VectorOperators,b) = a - b

let inline (-) a b = a ? (VectorOperators) <- b

I didn't test it. But it should work.

By on 12/6/2011 3:34 PM ()

Great idea!

By on 12/7/2011 1:33 AM ()
By on 11/26/2011 2:13 PM ()

Well, generally you should define operators as static members on the class (in this case, Vector<_>). This would solve all problems.

By on 11/19/2011 8:35 AM ()

The problem is that I'm trying to extend the Math.NET classes which are defined in C# with some more operators, and not defining my own classes. I can't use type extensions either, because they don't support operators (which is unexpected as they do support static members)

By on 11/19/2011 9:08 AM ()

Well, then I'd recommend defining two different operators (for example >- and -<). I see no other type-safe way.

By on 11/19/2011 10:50 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper