by edwcross · 6mo ago · 0 comments
createUser(user, ~isAdmin:true, ~sendWelcomeEmail:false)
Even though in OCaml's functional style it is actually like this: createUser user ~isAdmin:true ~sendWelcomeEmail:false
Using the fact that a variable named exactly like a labeled argument is automatically assigned to it, we can make the call more concise (especially if reusing existing variables): let isAdmin = true in
let sendWelcomeEmail = false in
createUser user ~isAdmin ~sendWelcomeEmail fn foo(namedParam internalName: bool) { // use internalName here }
fn foo(unnamedParam: bool)As author points out: "So I’ll usually just make it explicit:
createAdminUser(user);
createRegularUser(user);
Now there’s not much left to interpret. To be fair, this isn’t always bad. Sometimes this is completely fine:
toggleMenu(true);
That’s clear enough. This tends to work when:
- the meaning is obvious
- the function is small and local
- there’s only one flag "
> function foo({a, b, c}) {
... return {x: a, y: b, z: c};
... }
undefined
> foo({a: 1, b: 2, c: 3})
{ x: 1, y: 2, z: 3 }
> const a = 'A', b = 'B', c = 'C';
undefined
> foo({a, b, c})
{ x: 'A', y: 'B', z: 'C' }
> pub fn pad_end(
string: String,
to desired_length: Int,
with pad_string: String,
) -> String
And a call to this function may look like: pad_end("123", to: 5, with: ".")