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

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

Open
zygoloid opened this issue Aug 31, 2022 · 2 comments
Open

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

zygoloid opened this issue Aug 31, 2022 · 2 comments
Labels
design idea An issue recording a specific language design idea that folks can potentially pick up. long term Issues expected to take over 90 days to resolve.

Comments

@zygoloid
Copy link
Contributor

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 a case is 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?
@zygoloid zygoloid added the design idea An issue recording a specific language design idea that folks can potentially pick up. label Aug 31, 2022
@josh11b
Copy link
Contributor

josh11b commented Sep 6, 2022

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

This comment was marked as resolved.

@github-actions github-actions bot added the inactive Issues and PRs which have been inactive for at least 90 days. label Dec 6, 2022
@josh11b josh11b added long term Issues expected to take over 90 days to resolve. and removed inactive Issues and PRs which have been inactive for at least 90 days. labels Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design idea An issue recording a specific language design idea that folks can potentially pick up. long term Issues expected to take over 90 days to resolve.
Projects
None yet
Development

No branches or pull requests

2 participants