[ACCEPTED]-OCaml: Default values for function arguments?-default-value

Accepted answer
Score: 34

OCaml doesn't have optional positional parameters, because, since 10 OCaml supports currying, if you leave out 9 some arguments it just looks like a partial 8 application. However, for named parameters, there 7 are optional named parameters.

Normal named 6 parameters are declared like this:

let foo ~arg1 = arg1 + 5;;

Optional 5 named parameters are declared like this:

let odp ?(ftw = "OMG!!") () = print_endline ftw;;

(* and can be used like this *)
odp ~ftw:"hi mom" ();;
odp ();;

Note 4 that any optional named parameters must 3 be followed by at least one non-optional 2 parameter, because otherwise e.g "odp" above 1 would just look like a partial application.

More Related questions