How to use overloaded static statements

I am writing a tiny matrix library (mostly shell methods) in F # and have the problem of overloading static operator methods, with F # choosing the overload that I did not intend for it.

I have a module where I determined the correct matrix multiplication with a vector:

[<AutoOpen>]
module MatrixOps =

let (*) (matrix : IMatrix) (vector : IVector) =
    (...)

This allows me to write things like A * v, for example, where A is IMatrix and v is IVector. However, now I am adding the following line below let-binding:

let z = 1.0 * 2.0

and then the F # compiler identifies this as an error. Hover over "1.0", I get: "Type" float "is incompatible with type" IMatrix ", and similarly, hovering over" 2.0 ", I get:" Type "float" is incompatible with type "IVector". What happens here It seems that the F # compiler does not apply the multiplication operator for float and instead applies the operator for IMatrix and IVector. If I write instead

let z = (1.0 : float) * (2.0 : float)

the problem persists, so adding explicit type annotations does not help. How can I guarantee that F # selects the floating multiplication operator instead of the IMatrix / IVector operator defined above?

+4
source share
1

F # , .

F # .NET , :

type Matrix =
  static member (*) (matrix : Matrix) (vector : IVector) = ..

. . , , .

, . asMatrix, .

+8

Source: https://habr.com/ru/post/1662268/


All Articles