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

Change to (using x: T) => E for context functions #8305

Merged
merged 3 commits into from Feb 13, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Feb 12, 2020

Change syntax of context functions to (using x: T) => E. The previous
syntax (x: T) ?=> E is still supported, but the docs have been changed
to using.

Change syntax of context functions to `(using x: T) => E`. The previous
syntax `(x: T) ?=> E` is still supported, but the docs have been changed
to `using`.
@odersky
Copy link
Contributor Author

odersky commented Feb 12, 2020

As a trial, implement a suggestion @Kaishh on Scala contributors to align context parameter syntax of methods and function values.

@odersky
Copy link
Contributor Author

odersky commented Feb 12, 2020

My takeaway from the conversion:

  • Unlike the other contextual abstractions, context function literals are quite rare.
  • The rewrites do improve readability (to some degree) and regularity (significantly).

@arturopala
Copy link
Contributor

context (implicit) function literals are the most common way to define actions in PlayFramework, are they? i.e. {implicit request => ... }

@odersky
Copy link
Contributor Author

odersky commented Feb 12, 2020

context (implicit) function literals are the most common way to define actions in PlayFramework, are they? i.e. {implicit request => ... }

I was not aware of that! We should get PlayFramework into the community build, that would give us better insights. But U=I fear it will be a while until that can happen.

@mdedetrich
Copy link

Unlike the other contextual abstractions, context function literals are quite rare.

I would be cautious of making such conclusions. Just because something isn't in the community build doesn't mean its rare.

These should also be converted to `using` once Tasty printers have been updated.
@odersky
Copy link
Contributor Author

odersky commented Feb 13, 2020

@mdedetrich Understood. But there is another argument why context closures will be rare in Scala 3: They are automatically inferred if your target type is a context function type. So it's good style not to write them explicitly. For instance, a new version of the Play framework would probably just use {response} instead of {implicit request => response} since the expected type of such expressions would be Request ?=> Response.

@odersky odersky marked this pull request as ready for review February 13, 2020 08:12
@nicolasstucki
Copy link
Contributor

One advantage of this notation is that it composes naturally with erased function types

(using x: T) => E
(erased x: T) => E
(using erased x: T) => E

vs

(x: T) ?=> E
(erased x: T) => E
(erased x: T) ?=> E

compiler/src/dotty/tools/dotc/parsing/Parsers.scala Outdated Show resolved Hide resolved
```
instead of `(implicit x: A) => E` or `(given x: A) => E`.

## Context Functions Types

Implicit function types `implicit A => B` have been replaced with context function types, which are written `A ?=> B`. The syntax `(given A) => B` that was used in earlier Dotty versions is no longer supported.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have (using A) => B?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, let's have one way todo things.

Co-Authored-By: Nicolas Stucki <nicolas.stucki@gmail.com>
@odersky odersky merged commit 558a3ef into scala:master Feb 13, 2020
@odersky odersky deleted the change-using-closure branch February 13, 2020 12:34
@anatoliykmetyuk anatoliykmetyuk added this to the 0.23.0-RC1 milestone Mar 12, 2020
@jdegoes
Copy link

jdegoes commented Mar 19, 2020

Wonderful to see increased regularity during the polishing phase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants