Comments (9)
This has come up a couple of times. Here's my pasted answer from an earlier email:
I fiddled with default arguments for a while, but there are a number
of problems. It significantly complicates the grammar to allow arbitrary
expressions within parameter lists. Function literals look really
confusing with objects and assignment in the parameter lists, because
the "=>" gets lost in all the noise. And most of all, default arguments
aren't so much of an issue in JavaScript in the first place. You need to
use them in Ruby in order to create a method that's able to take
variable numbers of arguments, but JavaScript has no such problem
-- any method can be called with any number of named arguments. Providing
the default can be done quite easily with "||=". If CoffeeScript has both
variable argument length and "||=", putting the defaults up top in the
parameter list just makes things harder to read, without providing any
functionality that couldn't be otherwise accomplished.
But that's kind of just a rationalization for something that looks pretty ugly when shoehorned into CoffeeScript's syntax. I like using them in Ruby too. So if anyone comes up with a gorgeous syntax that wouldn't be too hard to parse, I wouldn't be opposed.
from coffeescript.
how about switching =>
to -->
, then it won't get confused so with other =
's
mymeth: a="abc", b="def" --> a + b
I kind of like it as well as its slightly bigger (and no harder to type).
from coffeescript.
mymeth: a | "abc", b | "def" => a + b
from coffeescript.
Well, = is kind of an opt-in assignment operator anyway, isn't it?
It's not that bad when you use the proper Coffee colon:
mymeth: a:"abc", b:"def" => a + b
from coffeescript.
I agree with @noonat, actually.
mymeth: a:"abc", b:"def" => a + b
seems much nicer than
mymeth: a, b => (a or "abc") + (b or "abc")
and any other syntax would just add unnecessary complexity.
from coffeescript.
My original objection was just that it looks too confusing with all the assignment, especially when the method signature is longer. And, that while in Ruby it serves a real purpose (calling the function with more or fewer named arguments), in JavaScript you can always already do that. Do y'all really prefer this:
any: obj, iterator: _.identity, context: undefined =>
return obj.some().....
To this:
any: obj, iterator, context =>
iterator ||= _.identity
return obj.some().....
If I wasn't already familiar with default arguments, I'd have a hard time understanding what was going on in the first one.
from coffeescript.
I agree that the addition of the ||= operator means this is not really a necessary feature.
from coffeescript.
Ok. Closing the ticket. If anyone finds a really readable syntax for this for methods with long signatures, feel free to reopen it.
from coffeescript.
I have implemented this in my default_args branch. Syntax is pretty simple:
fn: (param1, param2: 'default value', param3: param1 + param2) ->
# param1 can be null or undefined
# param2 is defaulted to 'default value' is omitted or null
# param3 is the value of param1 and param2 after param2 has been defaulted
from coffeescript.
Related Issues (20)
- Bug?: Cannot Base64 encode value: 0 (Legacy browser/WSH) HOT 2
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.