F#関数の引数として値を直接渡すと、コンパイラは自動的に値をアップキャストします(関数がControl
の場合は、TextBox
の値を与えることができます)。したがって、パラメータの型として柔軟な型を使用すると、大きな違いはありません。
機能は、例えば、リスト'T list
を取る場合は、違いがあります:
// Takes a list of any subtype of object (using flexible type)
let test1<'T when 'T :> obj> (items:'T list) =
items |> List.iter (printfn "%A")
// Takse a list, which has to be _exactly_ a list of objects
let test2 (items:obj list) =
items |> List.iter (printfn "%A")
// Create a list of System.Random values (System.Random list)
let l = [new System.Random()]
test1 l // This works because System.Random is subtype of obj
test2 l // This does not work, because the argument has wrong type!
+1の#文を記述しています。 –