Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default function parameter ordering

Tags:

coffeescript

Reading through this, I came to the bit on default values for function arguments:

fill = (container, liquid = "coffee") ->   "Filling the #{container} with #{liquid}..." 

That's neat, but then I tried this:

fill = (container="mug", liquid = "coffee") ->   "Filling the #{container} with #{liquid}..."  alert fill(liquid="juice") 

and got the unexpected alert with "Filling the juice with coffee...". So then I tried this:

fill = (container="mug", liquid = "coffee") ->   "Filling the #{container} with #{liquid}..."  alert fill(null, "juice") 

and it worked. It's not pretty though. Is there a better way, or is this the idiomatic way to do this?

like image 778
nmichaels Avatar asked Mar 08 '11 21:03

nmichaels


People also ask

What is the order of default argument?

The order of the default argument will be random.

Does the order of parameters in a function matter?

terminology - Name of Property: The Order Of Parameters in a Function Does Not Matter - Mathematics Stack Exchange.

What is the default parameter of a function?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Does the order of parameters matter in Python?

It doesn't matter what order they are given in the parameter so long as the arguments in the call expression match the correct variable. Either way won't matter to the output string.


2 Answers

fill = ({container, liquid} = {}) ->      container ?= "mug"      liquid ?= "coffee"       "Filling the #{container} with #{liquid}..."  alert fill(liquid: "juice", container: "glass") alert fill() 
fill = (quantity="500 mL", {container, liquid} = {}) ->      container ?= "mug"      liquid ?= "coffee"       "Filling the #{container} with #{quantity} of #{liquid}..."  alert fill("1L", liquid: "juice", container: "glass") alert fill() alert fill "1L" alert fill "1L", liquid: "water" 
like image 58
Jeremy Avatar answered Sep 27 '22 22:09

Jeremy


Amir and Jeremy already have this. As they point out, container="mug" in a function's argument list is really just shorthand for container ?= "mug" in the function body.

Let me just add that when calling functions,

fill(liquid="juice") 

means the same thing as in JavaScript: First, assign the value "juice" to the liquid variable; then pass liquid along to fill. CoffeeScript doesn't do anything special here, and liquid has the same scope in that situation as it would outside of the function call.

By the way, I've suggested that the default argument syntax should be made more powerful by allowing arguments to be skipped (e.g. (first, middle ?= null, last) -> would assign values to first and last if only two arguments were passed), and that the ?= syntax should be used rather than =. You might want to express support for that proposal here: issue 1091.

like image 43
Trevor Burnham Avatar answered Sep 27 '22 20:09

Trevor Burnham