Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Braceless object return #1263

Closed
zdzolton opened this issue Apr 4, 2011 · 15 comments
Closed

Braceless object return #1263

zdzolton opened this issue Apr 4, 2011 · 15 comments

Comments

@zdzolton
Copy link

zdzolton commented Apr 4, 2011

I'd like to be able to return an object literal from a function without typing any braces:

makeAwesome = (what, ever) ->
  stuff = doStuff what, ever
  return
    foo: what
    bar: doMore stuff
    baz: stuff

Right now, you have to do this:

makeAwesome = (what, ever) ->
  stuff = doStuff what, ever
  return {
    foo: what
    bar: doMore stuff
    baz: stuff
  }

Not terrible, but come on... We don't wanna type no stinking braces! LOL

Actually, this is legal but I think it looks confusing:

makeAwesome = (what, ever) ->
  stuff = doStuff what, ever
  foo: what
  bar: doMore stuff
  baz: stuff
@OnesimusUnbound
Copy link

How about separating the object to be returned by one line? I think it's readable.

makeAwesome = (what, ever) ->
  stuff = doStuff what, ever

  foo: what
  bar: doMore stuff
  baz: stuff

@erisdev
Copy link

erisdev commented Apr 5, 2011

The way it is now is inconsistent with the rest of the language—function calls, assignment, nested objects—so yeah, this should probably be fixed.

The implicit return separated by an empty line is readable, but not as clear as it could be. Additionally, while it is a passable workaround for the usual case, it can't help you when you need to put an explicit return somewhere.

@zdzolton
Copy link
Author

zdzolton commented Apr 5, 2011

Also, given CoffeeScript's lack of surrounding braces or keywords for
a function block, I prefer not to put blank lines in the middle of my
functions. Having them visually together makes my mind consider it as
one unit, which helps me to understand the code's organization.

On Mon, Apr 4, 2011 at 11:28 PM, erisdiscord
[email protected]
wrote:

The way it is now is inconsistent with the rest of the language—function calls, assignment, nested objects—so yeah, this should probably be fixed.

The implicit return separated by an empty line is readable, but not as clear as it could be. Additionally, while it is a passable workaround for the usual case, it can't help you when you need to put an explicit return somewhere.

Reply to this email directly or view it on GitHub:
#1263 (comment)

@satyr
Copy link
Collaborator

satyr commented Apr 5, 2011

@zdzolton
Copy link
Author

zdzolton commented Apr 5, 2011

I'm sorry @satyr, I must have been unclear in my original post—I
wouldn't waste everyone's time by asking something that could be
answered by a Google search! (^_^)

I'm asking for comments on potentially amending CoffeeScript's
return syntax to allow for returning a multiline object literal from
a function without having to use braces (or parens or one-lining the
whole object).

Given that you can already pass an object literal to a function call like this:

someFunc
  foo: 42
  bar: true
  baz: 'ok'

It feels natural to be able to do the same thing with returning an
object from a function, as in my original post.

On Tue, Apr 5, 2011 at 4:25 PM, satyr
[email protected]
wrote:

http://stackoverflow.com/questions/4907625/how-to-return-object-explicitly-in-coffeescript

Reply to this email directly or view it on GitHub:
#1263 (comment)

@satyr
Copy link
Collaborator

satyr commented Apr 5, 2011

someFunc
  foo: 42

Note that this syntax is an inconsitency in the language (there are lingering issues with it). Adding another similar specialcase may worsen the situation. (What about throw?)

@michaelficarra
Copy link
Collaborator

See #1113, a function call with an implicit object is a special case. @satyr's suggestions (from stackoverflow) seem fine to me.

@jashkenas
Copy link
Owner

Yep -- agreed -- I don't think we really need to be adding an additional special case for this.

@joeytwiddle
Copy link

If you really need this style, you can do:

  return \
    foo: what
    bar: doMore stuff
    baz: stuff

@jashkenas jashkenas reopened this Dec 16, 2011
@michaelficarra
Copy link
Collaborator

@jashkenas: why the re-open?

@jashkenas
Copy link
Owner

Because we're thinking about fixing this in that other ticket.

Although we can certainly close one of them as a dupe, if you like.

@satyr
Copy link
Collaborator

satyr commented Dec 17, 2011

If you really need this style, you can do:

  return \
    foo: what
    bar: doMore stuff
    baz: stuff

Not really.

$ coffee -cs
  return \
    foo: what
    bar: doMore stuff
    baz: stuff

(function() {

  return {
    foo: what
  };

  ({
    bar: doMore(stuff),
    baz: stuff
  });

}).call(this);

@erisdev
Copy link

erisdev commented Dec 17, 2011

@satyr haha, subtle, wow.

@markjaquith
Copy link

I've taken to using this for explicit early returns:

return {} =
  here: 'is'
  my:   'object'

...and this for implicit object returns:

{} =
  here: 'is'
  my:   'object'

(that second one not being mentioned on the StackOverflow thread).

@GeoffreyBooth
Copy link
Collaborator

GeoffreyBooth commented Apr 30, 2017

I’ve noticed that this bug is the root of several similar issues. I’m trying to trace it down but not having much luck so far. Parsing/lexing experts, can anyone tell me why this:

throw
  a: 1
  b: 2

compiles as you’d expect, but if you replace throw with return or export default, the latter two throw error: unexpected indentation?

EDIT: Looks like it’s in lexer.coffee, lineToken and unfinished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants