Skip to content

Default values for function arguments? #487

Answered by holzensp
jjmaestro asked this question in Q&A
Discussion options

You must be logged in to vote

You're so close to how I would write this.

Taking a step back; first a mini-rant about functions. People grab for them too often. Very many applications of functions are much more cleanly expressed using laziness / late-binding. The fact that one value derives from other, given values is a reason to reach for functions in many languages, but not lazy, value-centric ones. Here's how I'd write your initial example (I understand this is heavily reduced from a real-world case, so I may be shooting at the wrong target); you wrote

const function foo1(arg1: Int): String =
  "arg1: \(arg1)"

_foo1 = foo1(42)

where I'd write

hidden arg1: Int
fixed foo1 = "arg1: \(arg1)"

In your last suggestion, …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jjmaestro
Comment options

Answer selected by jjmaestro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants