In addition to the other answers, here are some more “almost solutions”. They are not strictly what you wanted, but you should still know.
Using a list (or array) and pattern matching:
let f = function
| [a, b] -> ...
| [a] -> ...
| [] -> failwith "too few arguments"
| _ -> failwith "too many arguments"
f ["a"]
f ["a" ; "b"]
Problems: parameters are not named, it is not clear from the function signature how many parameters are required.
Using a record to pass all optional parameters:
type FParams = { a : string; b : string }
let fdefault = { a = "a" ; b = "b" }
let f (pars: FParams) = ...
f { fdefault with b = "c" }
Problem: a is also optional that you do not need. May be helpful though.
source
share