carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)
http://docs.carbon-lang.dev/
Other
32.28k stars 1.48k forks source link

Design idea: terse function single-expression function body syntax #2131

Open zygoloid opened 2 years ago

zygoloid commented 2 years ago

Early Carbon language design ideas included having a terser way of writing a function definition in the case where the body is a single expression. Instead of:

fn F() -> auto {
  return expr;
}

... we could allow something like ...

fn F() => expr;

In such cases, an omitted return type would be treated as -> auto. Some other languages such as C# have similar functionality.

Particular concerns that a proposal should consider:

josh11b commented 2 years ago

Food for thought, from Wren:

The nice thing about [Wren's] syntax as opposed to something like => is that the end of the block has an explicit delimiter. That helps when chaining:

numbers.map {|n| n * 2 }.where {|n| n < 100 }
github-actions[bot] commented 1 year ago

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please comment or remove the inactive label. The long term label can also be added for issues which are expected to take time. This issue is labeled inactive because the last activity was over 90 days ago.