Optional Arguments(OCaml)
Run the following code at sketch.sh
Optional arguments
let concat ?sep x y =
let sep = match sep with None -> "" | Some s -> s in
x ^ sep ^ y ;;
concat ~sep:"," "Hello" "World";;
Need to add "()" if optional argument is the last
let concat x y ?sep () =
let sep = match sep with None -> "" | Some s -> s in
x ^ sep ^ y ;;
concat ~sep:"," "hello" "world" () ;;
With function type signature and paramater type annotation
(* val concat: string -> string -> ?sep:string -> unit -> string *)
let concat (x: string) (y: string) ?(sep:string option) () =
let sep = match sep with None -> "" | Some s -> s in
x ^ sep ^ y ;;
concat ~sep:"," "hello" "world" () ;;
Passing value of option type to optional paramater
let concat ?sep x y =
let sep = match sep with None -> "" | Some s -> s in
x ^ sep ^ y ;;
let separator = Some("/") in
concat ?sep:separator "hello" "world" ;;
let sep = None in
concat ?sep "hello" "world" ;;
Optional paramater with default value
let bump ?(step=1) x = x + step ;;
bump 10;;
Partial application
let test ?x ?y () ?z () = (x, y, z) ;;
test () () ~x:1 ~y:2 ~z:3 ;;
test ()() ;;
test ~x:1 ~y:2 () () ;;