-
I have
which works as expected:
But then, when I tried adding another argument with a default value, I got the following:
The most surprising bit was when I tried going around that issue by using a
And I get the following error:
Of course, it works when I pass
I was expecting the same behavior we have in properties, explained in the language reference: Type Annotations - Default values
Advanced Topics - Amending Null Values
For the time being, I've worked around this by crafting an
I guess Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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
where I'd write
In your last suggestion, you reach for a class, to aggregate the function parameters and give them default values. I would take one further step, and eliminate the function itself:
There is a general pattern for using classes to define functions. You can even do this in multiple directions;
(Notice the difference in the use of We are still considering how to make functions more expressive, but the power of laziness, and using objects as functions is actually enough for so many scenarios, so it's not a priority. I think I only ever user functions when an instance of my (module) class needs extra information that I can't provide at definition time. |
Beta Was this translation helpful? Give feedback.
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
where I'd write
In your last suggestion, …