carbon-lang
carbon-lang copied to clipboard
Design idea: terse function single-expression function body syntax
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:
- Is this sufficiently motivated? How widely used is the corresponding C# feature? How does this affect readability of Carbon code?
- Should an explicit return type be permitted?
- What syntax should be used?
=>seems nice but might be too similar to->. Also,=>in acaseis followed by a block not an expression, creating some amount of inconsistency. - Whatever syntax is chosen should presumably also be available in lambdas; how well does the chosen syntax fit there?
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 }
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.