Recently there have been a couple questions regarding static type constraints and inline:
- http://stackoverflow.com/questions/3754862/use-of-inline-in-f
- http://stackoverflow.com/questions/3757126/how-does-f-compile-functions-that-can-take-multiple-different-parameter-types-in
Two statements in particular have struck me:
"There is no way to encode this type constraint in a first class way in compiled code on .NET. However, the F# compiler can enforce this constraint at the site where it inlines the function, so that all operator uses are resolved at compile time."
"...but F# can also inline between compiled assemblies because inline is conveyed via .NET metadata."
The later almost seems to contradict the former. I wonder why inlining needs to be related to the static type constraints feature at all. Couldn't the F# compiler work the way (I believe) C++ templates and .NET generics do and generate type specific, reusable functions as needed on the fly at compile time?
For example:
Polymorphic Definition:
let add (lhs:^a) (rhs:^a) = lhs + rhs
Usage:
let a = add 2 3
let b = add 2. 3.
let b = add 4. 5.
Compiler Generated add Functions:
let add_int (lhs:int) (rhs:int) = lhs + rhs
let add_float (lhs:float) (rhs:float) = lhs + rhs
Compiler Rewritten Usage:
let a = add_int 2 3
let b = add_float 2. 3.
let c = add_float 4. 5.
I am not a compiler writer nor language designer, though the topics do interest me greatly, so please point out my naivety.