Reputation: 404
I'm studying F# and I don't understand the purpose of flexible types, or better, I can't understand the difference between writing this:
set TextOfControl (c : Control) s = c.Text <- s
and writing this:
set TextOfControl (c : 'T when 'T :> Control) s = c.Text <- s
where Control
is the System.Windows.Forms.Control
class.
Upvotes: 12
Views: 1397
Reputation: 41290
There is no difference in your example. If return types are constrained, you start seeing the difference:
let setText (c: Control) s = c.Text <- s; c
let setTextGeneric (c: #Control) s = c.Text <- s; c
let c = setText (TreeView()) "" // return a Control object
let tv = setTextGeneric (TreeView()) "" // return a TreeView object
Note that #Control
is a shortcut of 'T when 'T :> Control
.
Type constraints are important to create generic functions for subtypes.
For example,
let create (f: _ -> Control) = f()
let c = create (fun () -> Control()) // works
let tv = create (fun () -> TreeView()) // fails
vs.
let create (f: _ -> #Control) = f()
let c = create (fun () -> Control()) // works
let tv = create (fun () -> TreeView()) // works
Upvotes: 21
Reputation: 243051
When passing a value directly as an argument to an F# function, the compiler autoamtically upcasts the value (so if the function takes Control
, you can give it TextBox
value). So, if you use a flexible type as a type of parameter, there is not a big difference.
However, there is a difference if the function takes, for example a list '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!
Upvotes: 8