Please explain the magic behind the drawShape function. 1) Why does this work at all - I mean, what he calls the Draw member, 2) why should it be inline ?
type Triangle() = member x.Draw() = printfn "Drawing triangle" type Rectangle() = member x.Draw() = printfn "Drawing rectangle" let inline drawShape (shape : ^a) = (^a : (member Draw : unit->unit) shape) let triangle = Triangle() let rect = Rectangle() drawShape triangle drawShape rect
And the next problem - is it possible to write a drawShape function using parameter type annotation as shown below? I found that it has exactly the same signature as the first, but I cannot complete the body.
let inline drawShape2 (shape : ^a when ^a : (member Draw : unit->unit)) = ...
Thanks in advance.
f #
Endrju May 18 '15 at 1:07 pm 2015-05-18 13:07
source share