Unfortunately, it is not easy to write truly generic numeric code on .NET, because you cannot express operator constraints easily (e.g. 'T or Matrix<'T> must be a type that implements operator *). In case of your simple example however, you can use static member constraints:

1
2
3
4
let inline f m x = m * x
// inferred type signature: ^a ->  ^b ->  ^c

//       when ( ^a or  ^b) : (static member ( * ) :  ^a *  ^b ->  ^c)


As I understand it, the effect of this is, that a call to f is replaced by a type-specialized version of the right hand side (at compile-time).

By on 6/26/2011 6:40 AM ()

Thanks very much for the reply. I tried it out. The following definition with type constraints compiles and dispatches to the correct function:

1
2
let inline f2 (m:^a) (x:^b) : ^c when (^a or ^b) : (static member (*) : ^a * ^b -> ^c) =
      m * x
By on 6/26/2011 12:28 PM ()
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