Giter Club home page Giter Club logo

Comments (9)

jashkenas avatar jashkenas commented on June 24, 2024

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.

weepy avatar weepy commented on June 24, 2024

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.

maxterry avatar maxterry commented on June 24, 2024
mymeth: a | "abc", b | "def" => a + b

from coffeescript.

noonat avatar noonat commented on June 24, 2024

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.

maxterry avatar maxterry commented on June 24, 2024

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.

jashkenas avatar jashkenas commented on June 24, 2024

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.

weepy avatar weepy commented on June 24, 2024

I agree that the addition of the ||= operator means this is not really a necessary feature.

from coffeescript.

jashkenas avatar jashkenas commented on June 24, 2024

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.

StanAngeloff avatar StanAngeloff commented on June 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.