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

Language suggestion: shorter function declaration with only one parameter and one line expression #36941

Closed
xareelee opened this issue May 12, 2019 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report

Comments

@xareelee
Copy link

xareelee commented May 12, 2019

It's just a suggestion for Dart lang, not a bug report. I did not find a way to publish my suggestion, so I file this here.

Arrow functions are good, but it can be shorter when there is only one parameter by using underscore _.

function greeting(name) {
  print("Hello ${name}! How old are you, ${name}?");
}

// It can be shorter using a shorthand syntax.
var foo = (name) => print("Hello ${name}! How old are you, ${name}?");

greeting('John');   // Hello John! How old are you, John?

It can be shorter using underscore _ without (_) => syntax.

var foo = print("Hello ${_}! How old are you, ${_}?");
// same as 
var foo = (_) => print("Hello ${_}! How old are you, ${_}?");

When an expression using underscore _, it returns a function which will take only one parameter and pass it to the express. You can use the same parameter in the expression as many times as you want. It might be useful for functional programming.

[1, 2, 3, 4].map((_ + 1) * _);   // [2, 6, 12, 20]
// same as 
[1, 2, 3, 4].map((_) => (_ + 10) * _);  
@xareelee xareelee changed the title Language suggestion: Language suggestion: shorter function declaration with only one parameter May 12, 2019
@xareelee xareelee changed the title Language suggestion: shorter function declaration with only one parameter Language suggestion: shorter function declaration with only one parameter and one line expression May 12, 2019
@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels May 13, 2019
@lrhn
Copy link
Member

lrhn commented May 13, 2019

This suggestion is reminiscent of dart-lang/language#265 (and a number of other language requests in the language repository). So, it is something we are aware of.

One issue with the proposed syntax is that it's not clear where to insert the implicit (_) =>.
In [1, 2, 3, 4].map((_ + 1) * _), it is possible to insert it in many places, including:

  • [1, 2, 3, 4].map((_) => (_ + 1) * _)
  • [1, 2, 3, 4].map(((_) => _ + 1) * (_) => _)
  • [1, 2, 3, 4].map((((_) => _) + 1) * (_) => _)
  • (_) => [1, 2, 3, 4].map((_ + 1) * _)

Most of these can be rejected from the type, but it's dangerous to have a syntax where you can't parse until you have typed the syntax, because typing usually depends on the parsing.
We need to insert an implicit delimiter, and without any guidance, there is a lot of places where it can be inserted. That's why it's a good idea to require the user to tell us where it goes.

@lrhn lrhn closed this as completed May 13, 2019
@xareelee
Copy link
Author

@lrhn, Thanks for your reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

2 participants