id | keywords | name | summary | category | ||
---|---|---|---|---|---|---|
labeled-argument |
| ~arg | This is a `labeled argument`. | languageconstructs |
When declaring a function, arguments can be prefixed with ~
which means that they can and need to be called by their name, not the argument position. This is especially useful to differentiate them more easily if they are of the same type.
<CodeTab labels={["ReScript", "JS Output"]}>
letcalculateDistance= (~x1, ~y1, ~x2, ~y2) => { Math.sqrt((x1-.x2) **2. +. (y1-.y2) **2.) } calculateDistance(~x1=6., ~y1=8., ~x2=3., ~y2=4.)
functioncalculateDistance(x1,y1,x2,y2){returnMath.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));}calculateDistance(6,8,3,4);
Labeled arguments can be provided in any order:
<CodeTab labels={["ReScript", "JS Output"]}>
calculateDistance(~x1=6., ~x2=3., ~y1=8., ~y2=4.)
calculateDistance(6,8,3,4);
This also works together with partial application:
<CodeTab labels={["ReScript", "JS Output"]}>
letcalcY=calculateDistance(~x1=6., ~x2=3., ...) calcY(~y1=8., ~y2=4.)
functioncalcY(none,extra){returncalculateDistance(6,none,3,extra);}calcY(8,4);