Operator overloading with seq <'T>

In a project I'm experimenting with, I'm constantly multiplying the elements of the sequence. I wanted to create a statement like this:

let (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b

      

However, in FSI, it returns:

val ( .* ) : a:seq<int> -> b:seq<int> -> seq<int>

      

And the following:

  seq [1.0;2.0] .* seq [3.0;4.0];;
  -----^^^

stdin(16,6): error FS0001: This expression was expected to have type
    int    
but here has type
    float 

      

How can I get the operator to work with generic seq <'T> (assuming T supports multiplication)?

+3


source share


1 answer


You need to add a keyword inline

to the operator declaration so that correct overloading is resolved at compile time:

let inline (.*) (a:seq<'T>) (b:seq<'T>) = Seq.map2 (*) a b

      



val inline ( .* ) : a:seq< ^T> -> b:seq< ^T> -> seq< ^a> when  ^T :  (static member ( * ) :  ^T *  ^T ->  ^a)

      

+6


source







All Articles