Skip to content

Hate this implicit return statement. Proposal to add returnless function arrow #4210

@yivo

Description

@yivo

Hello guys! Thank you for creating and supporting CoffeeScript!

Often when I want to extract loop to separate method I write like this:

performEach = ->
  el.perform() for el in collection

But always getting loop which collects returned data from el.perform():

var performEach;

performEach = function() {
  var el, i, len, results;
  results = [];
  for (i = 0, len = collection.length; i < len; i++) {
    el = collection[i];
    results.push(el.perform());
  }
  return results;
};

Then I add explicit return statement:

performEach = ->
  el.perform() for el in collection; return

And getting what I want:

var performEach;

performEach = function() {
  var el, i, len;
  for (i = 0, len = collection.length; i < len; i++) {
    el = collection[i];
    el.perform();
  }
};

Proposal! What do you think about returnless arrow? Something like this:

performEach = |> # Note new syntax: "|>". Fat arrow syntax: "||>" 
  el.perform() for el in collection

Compiles to:

var performEach;

performEach = function() {
  var el, i, len;
  for (i = 0, len = collection.length; i < len; i++) {
    el = collection[i];
    el.perform();
  }
};

For many APIs is it important what value was returned (for example it is event handlers in jQuery).
Also there is article about it http://programmaticallyspeaking.com/why-i-hate-implicit-return-in-coffeescript.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions