The F # statement does not resolve correctly

let inline (=~) a b = abs (single a - single b) <= 0.001f

type Vector =
    { x : single; y : single; z : single }

    static member (=~) (v1, v2) = (v1.x =~ v2.x) && (v1.y =~ v2.y) && (v1.z =~ v2.z)

let v1, v2 =
    { x = 0.1f; y = single Math.PI; z = 0.f },
    { x = 0.1f; y = 3.14159f; z = 0.0001f }

v1 =~ v2

The compiler complains: The type 'Vector' does not support a conversion to the type 'single'

I do not understand. Obviously, a particular type of operator does not take precedence over a generic operator, limiting my intuition. What is the trick for this?

+4
source share
2 answers

When you define a custom statement with let, it will always take precedence over type-specific statements. An easy way to handle this is to avoid name clashes in local and global operator names or to limit the scope of the let-bound statements. For example, you can put the global operator =~in a separate module:

module VectorImplementation = 
  let inline (=~) a b = abs (single a - single b) <= 0.001f

module Vectors = 
  open VectorImplementation
  type Vector =
      { x : single; y : single; z : single }
      static member (=~) (v1, v2) = 
        (v1.x =~ v2.x) && (v1.y =~ v2.y) && (v1.z =~ v2.z)

open System
open Vectors

let v1, v2 =
    { x = 0.1f; y = single Math.PI; z = 0.f },
    { x = 0.1f; y = 3.14159f; z = 0.0001f }

v1 =~ v2

, let, . , - , , , .

+4

97 F # 4.0 , :

, (§14.1) , , (§0), . , , F #, , , , .

, , =~, , "" =~ Vector. . :

let inline (==~) a b = abs (single a - single b) <= 0.001f
type Vector =
    { x : single; y : single; z : single }
    static member (=~) (v1, v2) = 
        (v1.x ==~ v2.x) && (v1.y ==~ v2.y) && (v1.z ==~ v2.z)

, , . ; .

+2

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


All Articles