Skip to content

Instantly share code, notes, and snippets.

@hoichi
Created November 8, 2018 17:07
Show Gist options
  • Save hoichi/5ee5a9a7b4f9ed526f0180419d0d9ae4 to your computer and use it in GitHub Desktop.
Save hoichi/5ee5a9a7b4f9ed526f0180419d0d9ae4 to your computer and use it in GitHub Desktop.
Experiments with optional parameters in ReasonML
/**
* Trying to figure out types of the optional arguments.
*/
let mandatory = (~s: string) => s;
// let mandatory: (~s: string) => string = <fun>;
let optional = (~s: string=?) => s;
/* Error: This pattern matches values of type string
but a pattern was expected which matches values of type option('a)
Which is a pity, because the following is a bit of a mouthful.
*/
let optional = (~s: option(string)=?) => s;
/*
Characters 17-18:
Warning 16: this optional argument cannot be erased.
let optional: (~s: string=?) => option(string) = <fun>;
*/
optional(~s="lkj");
// - : option(string) = Some("lkj")
plainOption = (~s: option(string)) => s;
// let plainOption: (~s: option(string)) => option(string) = <fun>;
let plainWrapper = (~sOut=?, ()) => plainOption(~s=sOut);
/* let plainWrapper: (~sOut: string=?, unit) => option(string) = <fun>;
No need for explicitly passed optional because sOut is already an option, and ~s is an option (not optional) too.
Moreover, if you try the expicit passing...
*/
let plainWrapper = (~sOut=?, ()) => plainOption(~s=?sOut);
/*
Characters 52-56:
Warning 43: the label s is not optional.
let plainWrapper: (~sOut: string=?, unit) => option(string) = <fun>;
It works though:
*/
plainWrapper(~sOut="kj", ());
// - : option(string) = Some("kj")
// No warning with optional:
let optionalWrapper = (~sOut=?, ()) => optional(~s=?sOut);
// let optionalWrapper: (~sOut: string=?, unit) => option(string) = <fun>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment