Skip to content

New phase: FunctionalInterfaces. Rewires closures to implement more specific types of Function SAMs. #488

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

Merged
merged 8 commits into from
Apr 23, 2015

Conversation

DarkDimius
Copy link
Contributor

No description provided.

 Rewires closures to implement more specific types of Function SAMs.
It seems that loading a class from class path does not define
the class in previous phases. Are InfoTransformers applied?
It seems to violate a lot of assumptions of what types&symbols are after erasure,
as unerased types could leak into trees.
Function0, Function1, Function2 are all specialized for different type params.
FunctionalInterface filters out classes that are known to not exists
and relies on getClassIfDefined for a precise check.
@DarkDimius
Copy link
Contributor Author

@AlexSikia, you could be interested to reuse this.

@DarkDimius
Copy link
Contributor Author

@odersky please review.

@smarter
Copy link
Member

smarter commented Apr 20, 2015

So, currently something like:

val x: Function1[Int,Int] = elem => elem + 1

Is rewritten as:

val x: Function1[Int, Int] = {
  def $anonfun(elem: Int): Int = elem.+(1)
  closure($anonfun) // Pretty-print of Closure(Nil, $anonfun, EmptyTree)
}

But if instead it was rewritten as:

val x: Function1[Int, Int] = {
  def $anonfun(elem: Int): Int = elem.+(1)
  closure($anonfun:Function1[Int,Int]) // Pretty-print of Closure(Nil, $anonfun, Function1[Int, Int])
}

Then this phase would not be needed: you could just replace Function1[Int, Int] by the corresponding specialized version in the Specialize phase (which does not exist yet), no?

@DarkDimius
Copy link
Contributor Author

But if instead it was rewritten as:

EmptyTree as tpt in Closure means that this is FunctionX. I'm not getting the difference between first version and rewritten one, in both you have the very same closure.

I believe that this commit is entirely different from what specialization is about. Specialization knows nothing about SAM's and their particularities.
What specialization does is it generates specialized methods\classes, makes generic bridges for them and dispatches to specialized methods where possible.
This change is neither about defining new methods nor about dispatching to them. And SAM's cannot have new bridges. I'm not aware how specialization can be reused to implement this.

@smarter
Copy link
Member

smarter commented Apr 20, 2015

What your phase does:

Closure(Nil, $anonfun, EmptyTree) ===>  Closure(Nil, $anonfun, Function$mcXX...)

One of the thing that specialization does:

new Foo[Int] ===> new Foo$mcXX...

It seems like it wouldn't be very hard to extend specialization to do:

Closure(Nil, $anonfun, Function1[Int]) ===>  Closure(Nil, $anonfun, Function1$mcXX...)

(As a bonus, it would also work for SAM types which are not FunctionN.)

@DarkDimius
Copy link
Contributor Author

Closure(Nil, $anonfun, EmptyTree) ===> Closure(Nil, $anonfun, Function$mcXX...)

No it's not what it does! It does
Closure(Nil, $anonfun, EmptyTree) ===> Closure(Nil, $anonfun, JFunction$mcXX...)
Where JFunction is java-defined.
At some point we can extend specialization to able to do it. But I'd better extend this phase to allow to implement specialization-generated SAM's, not to make specialization itself bigger.


def phaseName: String = "functionalInterfaces"

var allowedReturnTypes: Set[Symbol] = _ // moved here to make it explicit what specializations are generated
Copy link
Contributor

Choose a reason for hiding this comment

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

Make private?

@odersky
Copy link
Contributor

odersky commented Apr 22, 2015

Otherwise LGTM

DarkDimius added a commit that referenced this pull request Apr 23, 2015
New phase: FunctionalInterfaces.      Rewires closures to implement more specific types of Function SAMs.
@DarkDimius DarkDimius merged commit f1c191a into scala:master Apr 23, 2015
@allanrenucci allanrenucci deleted the functionalInterfaces branch December 14, 2017 19:23
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.

3 participants