Skip to content

Instantly share code, notes, and snippets.

@chenglou
Last active September 7, 2017 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenglou/a8078054d3e22524b30ff9e7a58a230f to your computer and use it in GitHub Desktop.
Save chenglou/a8078054d3e22524b30ff9e7a58a230f to your computer and use it in GitHub Desktop.
Revised syntax labelled function argument. **The only difference is between ~ and :**
/* == definition == */
/* two labelled arguments, one unlabelled */
let display = (~message, ~person, time) => 1;
/* with default value */
let display = (~message="hello", ~person="Reason", time) => 1;
/* with type annotation */
let display = (~message: string, ~person: string, time: float) => 1;
/* type annoation and with default */
let display = (~message: string="hello", ~person: string="Reason", time: float) => 1;
/* == call == */
/* normal */
display(~message="hey", ~person="Reason", 2.0);
/* call with punning, aka ~message=message */
display(~message, ~person, 2.0);
/* == type definition == */
/* normal */
type d = (~message: string, ~person: string, float) => int;
/* with default */
type d = (~message: string=?, ~person: string=?, float) => int;
/* == definition == */
/* two labelled arguments, one unlabelled */
let display = (:message, :person, time) => 1;
/* with default value */
let display = (:message="hello", :person="Reason", time) => 1;
/* with type annotation */
let display = (:message: string, :person: string, time: float) => 1;
/* type annoation and with default */
let display = (:message: string="hello", :person: string="Reason", time: float) => 1;
/* == call == */
/* normal */
display(:message="hey", :person="Reason", 2.0);
/* call with punning, aka :message=message */
display(:message, :person, 2.0);
/* == type definition == */
/* normal */
type d = (:message: string, :person: string, float) => int;
/* with default */
type d = (:message: string=?, :person: string=?, float) => int;
@kliron
Copy link

kliron commented Sep 7, 2017

Tilde please. Like others have said, I think overloading the meaning of symbols is not a wise thing to do. The tilde is unique and while it might seem weird the first 5 times a newcomer looks at it, it is easy to digest. Thowing : everywhere is awfully confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment