views:

100

answers:

1

Hi,

I have a class called Vector which implements a number of operators such as + and properties such as Count. Vector is also subtyped by classes such as DenseVector, SparseVector which inherit from Vector. Now in F# when I write the following

let foo (v : #Vector) (w : #Vector) = v + w
let bar (v : #Vector) (w : #Vector) = v.Count + w.Count

For "foo" I get the warning: "This construct causes code to be less generic than indicated by its type annotations. The type variable implied by the use of a '#', '_' or other type annotation at or near ... has been constrained to be type 'Vector'." while "bar" works just fine.

I don't understand why the flexible type constraint works fine for properties but for operator resolution it's having trouble. Any explanations/ideas/workarounds ?

EDIT My Vector class is abstract and has an operator with the following signature:

public static Vector operator +(Vector leftSide, Vector rightSide)
+2  A: 

I think the reason is quite simple: it doesn't work for operators because they are not inherited. For a workaround, this should work:

let foo (v : #Vector) (w : #Vector) = (v :> Vector) + (w :> Vector)

However, I am not sure this is really what you need. Is Vector an abstract class? How is + implemented?

Alexey Romanov
I've been looking around for an explanation about subtypes not inheriting operators; do you have a pointer to this fact?
Jurgen
Operators are defined in .NET as static methods, basically; and static members are never inherited in .NET.
Alexey Romanov