There is no way to extend existing types with overloaded operators, they have to be put on the type when it is created. Your best bet is to define a new pointwise addition operator, along the lines of:

1
let (+.) x y = Array.map2 (+) x y

If you want the operator to work on arrays of any type exposing its own (+) operator, you can inline the definition:

1
let inline (+.) x y = Array.map2 (+) x y
By on 12/9/2009 5:33 PM ()

Thanks for your quick reply.

Two questions,

1) Stemming from my lack of knowledge of the language, I am not sure how the inline keyword would affect the operation of the new (+.) operator. Could you please explain further?

and 2) This may be answered by question 1, but is there any way to restrict the type of arrays that can use that operator? So the operator would only work on numeric arrays (int[], double[], etc...)

By on 12/9/2009 7:33 PM ()

1 and 2) Without 'inline' you would get (+.): int[] -> int[] -> int[]. With inline it will be (+.): ^a[] -> ^a[] -> ^a[] where ^a must be known at compile time to have operator+ defined (op_Addition which is used in C#), so it can be used with any type which supports +

By on 12/10/2009 12:52 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