Table of Contents
Optional and named arguments
The OCaml syntax doesn't work with F#. Optional and named arguments can't be used with let functions, but only with members (both static and instance members) and class constructors.
Named arguments
type complex =
{r: float; i: float}
member c.Add(i, r) = {r = c.r + r; i = c.i + i}
c.Add(3., 4.)
c.Add(i = 3., r = 4.)
c.Add(r = 4., i = 3.)
Optional arguments
type complex =
{r: float; i: float}
member c.Add(?i, ?r) =
let i = defaultArg i 0.
let r = defaultArg r 0.
{r = c.r + r; i = c.i + i}
let c = {i = 1.; r = 1.}
> c.Add();;
val it : complex = 1.00+1.00i
> c.Add(1.);;
val it : complex = 1.00+2.00i
> c.Add(1., 2.);;
val it : complex = 3.00+2.00i
> c.Add(i = 1., r = 2.);;
val it : complex = 3.00+2.00i
> c.Add(r = 1., i = 2.);;
val it : complex = 2.00+3.00i
Class constructors
When used on a class constructor, named arguments are compiled into property setters:
new MenuItem(text="Hello")
new Form(Width=300)
See also
- Cours 11 - Méthodes (fr) (Laurent Le Brun - some samples come from this page)