dart-lang / dart-lang/language

Allow more concise function literal argument

Open
#343 4 comments 3 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

A Dart function invocation requires parentheses, e.g., `f(42)`. Other languages can be more concise.

Functional languages like Haskell and SML use an "empty" operator (a space), e.g., `f 42`. With curried invocation this extends to several arguments (`f 42 true`), but curried function invocation would be a radical change for Dart, and simultaneous invocation with multiple arguments in functional languages still require parentheses, because we're passing one value which is a tuple (`f (42, true)`). This seems to indicate that Dart cannot easily use this approach.

However, some languages exploit special properties of the declaration (like being the last parameter) and the call site (like passing a function literal, rather than an arbitrary argument), and that allows the concise form using just a space.

For example, a Ruby function invocation can have an ['associated block'](http://rubylearning.com/satishtalim/ruby_blocks.html) which is passed using a mere space (example from [Ruby core Array doc](https://ruby-doc.org/core-2.6.3/Array.html)):

```ruby
arr = [1, 2, 3, 4, 5, 6]
arr.select {|a| a > 3}
```

In the callee, it's possible to test whether such a block was given (`block_given?`) and call it (`yield`), but here we are just interested in the fact that we a block is passed at the call site using a concise syntax.

Kotlin uses a similar syntax to pass a lambda as the last argument in a function invocation. Here is an example which is at the same time an example of a [type-safe builder](https://kotlinlang.org/docs/reference/lambdas.html?_ga=2.85660441.1815970936.1556091506-1559271053.1556091506#function-literals-with-receiver):

```kotlin
class HTML {
fun body() { ... }
}

fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}

// Invoke `html`, using a mere space to pass the argument `{...}`.
html {
body()
}
```

This issue is a request for a similar kind of conciseness in Dart. It may seem like a tiny thing, but it is probably going to be used in a large number of locations.

Contributor guide

Open the contributing guide

Research direction

The issue names no repository file, test, or entry point. Start by reviewing Dart's existing function-invocation and function-literal syntax in the language specification, then determine the grammar and semantics needed for the proposed concise form. Done would require an agreed language design and corresponding specification and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.