これまでのところ、F#での型推論には非常に感銘を受けていますが、実際には得られなかったものを見つけました。
//First up a simple Vect3 type
type Vect3 = { x:float; y:float; z:float } with
static member (/) (v1 : Vect3, s : float) = //divide by scalar, note that float
{x=v1.x / s; y= v1.y /s; z = v1.z /s}
static member (-) (v1 : Vect3, v2 : Vect3) = //subtract two Vect3s
{x=v1.x - v2.x; y= v1.y - v2.y; z=v1.z - v2.z}
//... other operators...
//this works fine
let floatDiff h (f: float -> float) x = //returns float
((f (x + h)) - (f (x - h)))/(h * 2.0)
//as does this
let vectDiff h (f: float -> Vect3) x = //returns Vect3
((f (x + h)) - (f (x - h)))/(h * 2.0)
//I'm writing the same code twice so I try and make a generic function:
let genericDiff h (f: float -> 'a) x : 'a = //'a is constrained to a float
((f (x + h)) - (f (x - h)))/(h * 2.0)
この最後の関数を作成しようとすると、除算記号の下に青い波線が表示され、「この構成により、コードは型アノテーションで示されるよりも一般的ではなくなります。型変数'aは次のように制約されています。 'float'"と入力します。Vect3に/
関数に適した演算子を提供します。なぜ私に警告するのですか?