4 ms·
> Gleam's labelled arguments are AFAIK a unique feature OCaml also does this: # let foo ~bar:baz = baz + 42;; val foo : bar:int -> int = <fun> # f
by _flux 2y ago
> Gleam's labelled arguments are AFAIK a unique feature
OCaml also does this:
# let foo ~bar:baz = baz + 42;;
val foo : bar:int -> int = <fun>
# foo ~bar:12765;;
- : int = 12807
What I really enjoyed was its parameter forwarding, which is available in some languages for records:
# let bar = 42;;
val bar : int = 42
# foo ~bar;;
- : int = 84
Works similarly for optional values:
# let test ?(arg=5) () = arg;;
val test : ?arg:int -> unit -> int = <fun>
# let arg = None;;
val arg : 'a option = None
# test ?arg ();;
- : int = 5
And if you used ~arg, it would be a non-optional parameter:
# let arg = 5;;
val arg : int = 5
# test ~arg ();;
- : int = 5
Overall pretty spiffy. Partial application also works here nicely, but it does cause some trouble with optional arguments :/. For example, here "test" needs to have a final argument () so the optional parameter can be erased:
# test;;
- : ?arg:int -> unit -> int = <fun>
# test ();;
- : int = 5