Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 1.05 KB

language_optional_labeled_argument.mdx

File metadata and controls

48 lines (37 loc) · 1.05 KB
id keywords name summary category
optional-labeled-argument
optional
labeled
argument
~arg=?
This is an `optional labeled argument`.
languageconstructs

Labeled arguments, i.e. arguments that are prefixed with ~, can be suffixed with =? to denote that they are optional. Thus, they can be omitted when calling the function.

Example

<CodeTab labels={["ReScript", "JS Output"]}>

let print = (text, ~logLevel=?) => {
  switch logLevel {
  | Some(#error) => Console.error(text)
  | _ => Console.log(text)
  }
}

print("An info")
print("An error", ~logLevel=#error)
function print(text, logLevel) {
  if (logLevel === "error") {
    console.error(text);
  } else {
    console.log(text);
  }
}

print("An info", undefined);

print("An error", "error");

References