jashkenas / jashkenas/coffeescript
Proposal: Optional (pre-last) function arguments
- Dominant language
- CoffeeScript
- Stars
- 16.6k
- Forks
- 2k
- PR merge metrics
- No merged PRs in 30d
Description
It has been submitted before #1091, #4148 but it has been a while since last discussed so I'd like bring it forward again. I've been an avid coffeescript user since the early days and I missing this feature all the time.
instead of:
```coffee
fn = (optional_arg1, optional_arg2, cb) ->
unless cb?
unless optional_arg2?
cb = optional_arg1
else
cb = optional_arg2
````
wouldn't it make it sense to have it as:
```coffee
fn = (optional_arg1?, optional_arg2?, cb) ->
```
or better yet, decorating optional arguments with default values:
```coffee
fn = (optional_arg1 ?= true, optional_arg2 ?= false, cb) ->
```
for example:
```coffee
retriveRecords = (perPage ?= 20, page ?= 1, cb) ->
```
above function can be used neatly as:
```coffee
retriveRecords page, cb for page in [1...20]
```
or as:
```coffee
retriveRecords -> #callback result with arguments page=20 & page=1
```
my workaround at the moment is like this:
```coffee
retriveRecords = ({perPage, page}, cb) ->
perPage ?= 20
page ?= 1
retriveRecords perPage: 50, ->
retriveRecords page: 5, ->
retriveRecords perPage: 50, page: 5, ->
retriveRecords {}, ->
```
Contributor guide
Research direction
No implementation files or tests are named. Start by reading the prior discussions in #1091 and #4148, then identify how CoffeeScript parses and compiles function parameters. Done means agreeing on the optional pre-last argument syntax and semantics, supporting the demonstrated calls, and adding coverage for those examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- coffeescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100