After playing around with Idris I've become a huge fan of using explicit types to strengthen the declarative-self-documentation of my F# programs by more often writing the types explicitly on my functions and by using type aliases.
Defining my functions with explicit types has made me (re-)discover that F# actually has two styles of explicit typing for functions. There is the standard let function style using :
and there is the lambda functions using ->
.
For example
// explicitly typed function using the classical let ':' style
let OK1 (content : string) (context : Context) : Async<Context option> =
{ context with Response = { Content = content; StatusCode = 200 } }
|> Some
|> async.Return
// explicitly typed function using the '->' style
let OK2 : string -> Context -> Async<Context option> = fun content context ->
{ context with Response = { Content = content; StatusCode = 200 } }
|> Some
|> async.Return
The nice thing about the ->
style is that I can define type aliases such as
// type alias defining a webpart
type WebPart = Context -> Async<Context option>
// using the type alias in the declaration of an explicitly typed function
let OK2 : string -> WebPart = fun content context ->
{ context with Response = { Content = content; StatusCode = 200 } }
|> Some
|> async.Return
I don't think it's possible to declare and use the same type alias with the :
style...?
I am puzzled why F# has two styles of declaring explicit types on functions. Is it some sort of .Net limitation? or does it have some special purpose? Why not just define all explicit types with ->
instead of :
?