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

Add extension instances #8318

Open
wants to merge 7 commits into
base: master
from

Conversation

@odersky
Copy link
Contributor

odersky commented Feb 14, 2020

Add a new extension { ... } syntax for extension instances without a collective parameter, and
tweak the translation rules for collective extensions.

Motivation

Collective extensions and normal extension methods are conceptually closely related, but look far apart. The expansion of collective extensions into given instances that implement AnyRef is a cute technical trick, but few would want to write that.

It is generally encouraged that extension methods should be put into given instances since that way they can be often be found in the implicit scope of the left-hand side argument, and do not require an import. Furthermore, it's better to import an implicit extension object instead of all extension methods since that leads to less namespace pollution.

But so far, the only pleasant way to do this was to use collective extensions. These are great where they fit, but they do not fit everything. In particular, one sometimes wants to define a bunch of extension methods that do not share a common left parameter. Then one would have to define a separate collective extension for each extension method, which is also awkward.

Summary

This PR adds extension instances, to address the above mentioned shortcomings. Extension instances are simply extensions without the on part. Example:

extension ops {
  def (xs: Seq[String]).longestStrings: Seq[String] = {
    val maxLength = xs.map(_.length).max
    xs.filter(_.length == maxLength)
  }
  def [T](xs: List[T]).second: T = xs.tail.head
}

This extension groups the two extension methods longestStrings and second into a single given instance ops.

Collective extensions are mapped to extension instances by repeating the common parameter and extension instances are mapped in turn to given instances.

New doc page: https://github.com/lampepfl/dotty/blob/d36f43bc9ae1bc28fd5ade8d1fcfd7d14365301c/docs/docs/reference/contextual/extension-methods.md

odersky added 3 commits Feb 14, 2020
Don't postulate a synthetic import when expanding collective extensions.
Such an import would not typecheck. Postulate a direct substitution of
co-defined method identifiers instead.
@odersky odersky force-pushed the dotty-staging:add-extension-instance branch 2 times, most recently from 071b0b4 to d36f43b Feb 14, 2020
@odersky odersky changed the title Trial: extension instances Add extension instances Feb 15, 2020
@odersky odersky force-pushed the dotty-staging:add-extension-instance branch from d36f43b to ee3a2ac Feb 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
You can’t perform that action at this time.