Talmage
Talmage

Reputation: 343

Operator Overloading with seq<'T>

In a project I'm experimenting with, I'm constantly multiplying sequence elements. I wanted to create an operator 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 fails:

  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 make the operator work on a generic seq<'T> (provided 'T supports multiplication)?

Upvotes: 3

Views: 49

Answers (1)

TheInnerLight
TheInnerLight

Reputation: 12184

You need to add the inline keyword to your operator declaration so that the correct overload can be 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)

Upvotes: 6

Related Questions