Skip to content

Factor out logic for scala functions. #1962

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 1 commit into from
Feb 16, 2017

Conversation

nicolasstucki
Copy link
Contributor

This PR factors out code from #1408 aimed to make an abstraction on top of all different kinds of functions. Implemented Function0 to Function22, synthetic FunctionN with N > 22, ImplicitFunctionN and erased FunctionXXL. #1408 extends it with PhantomFunctionM and ImplicitPhantomFunctionM.

Copy link
Contributor

@OlivierBlanvillain OlivierBlanvillain left a comment

Choose a reason for hiding this comment

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

LGTM!

denot.info =
ClassInfo(ScalaPackageClass.thisType, cls, ObjectType :: parentTraits, decls)

decls.enter(newMethod(cls, nme.apply, methodType(tParamTpes.init, tParamTpes.last), Deferred))
Copy link
Contributor

Choose a reason for hiding this comment

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

With a .init and .last here I'm not actually sure I prefer tParamTpes to argParams & resParam

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I agree, the change was done on an intermediate version where it saved a considerable amount of code. This is clearly not the case anymore. I will change it back.

@@ -688,26 +684,14 @@ class Definitions {
lazy val AbstractFunctionType = mkArityArray("scala.runtime.AbstractFunction", MaxImplementedFunctionArity, 0)
val AbstractFunctionClassPerRun = new PerRun[Array[Symbol]](implicit ctx => AbstractFunctionType.map(_.symbol.asClass))
def AbstractFunctionClass(n: Int)(implicit ctx: Context) = AbstractFunctionClassPerRun()(ctx)(n)
private lazy val ImplementedFunctionType = mkArityArray("scala.Function", MaxImplementedFunctionArity, 0)
lazy val ImplementedFunctionType = mkArityArray("scala.Function", MaxImplementedFunctionArity, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about keeping this private and adding:?

lazy val Function0Type = ImplementedFunctionType(0)
lazy val Function1Type = ImplementedFunctionType(1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would work nicely. Thought will define them as def as ImplementedFunctionType is an array.

@@ -780,7 +760,8 @@ class Definitions {
* trait gets screwed up. Therefore, it is mandatory that FunctionXXL
* is treated as a NoInit trait.
*/
lazy val NoInitClasses = PhantomClasses + FunctionXXLClass
def isNoInitClass(cls: Symbol) =
cls.is(NoInitsTrait) || PhantomClasses(cls) || ScalaFunction(cls).exists
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this PhantomClasses.contains(cls)?

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I am actually a bit dubious about whether this is an improvement. The refactoring almost doubles the loc dealing with functions but I don't see a clear benefit. It also adds a new concept (in the form of a type) named ScalaFunction. What is it? At the very least this needs to be documented thoroughly.

I would be very reluctant to add new concepts like this, because they increase the cognitive load. So I think this should only be done if we clearly eliminate code duplication by doing it.

@nicolasstucki
Copy link
Contributor Author

Yes, in this PR the advantages does not really exist. It is only when adding the functions with phantom parameters that this starts to help. I will redesign it to to be closer to the original code to reduce the cognitive load while making it easy to add the new types of functions.

@nicolasstucki nicolasstucki force-pushed the centralize-function-logic branch 3 times, most recently from b2472ea to 09eb2d3 Compare February 13, 2017 15:36
* Add `isSyntheticFunction` checks for synthetic functions such as FuntionN
for N > 22 and ImplicitFunctionN for N >= 0.
* Add `erasedFunctionClass` to get the erased verion of synthetic functions.
* Change the semantics of `isFunctionClass` to return  true if it is any kind of
FunctionN or ImplicitFunctionN.
@nicolasstucki nicolasstucki force-pushed the centralize-function-logic branch from 09eb2d3 to 93a2d06 Compare February 13, 2017 17:29
@nicolasstucki
Copy link
Contributor Author

In this PR we:

  • Add isSyntheticFunction checks for synthetic functions such as FunctionN for N > 22 and ImplicitFunctionN for N >= 0.
  • Add erasedFunctionClass to get the erased version of synthetic functions.
  • Change the semantics of isFunctionClass to return true if it is any kind of FunctionN or ImplicitFunctionN.

@odersky
Copy link
Contributor

odersky commented Feb 16, 2017

LGTM! Great cleanup. 👍

@odersky odersky merged commit 6df672c into scala:master Feb 16, 2017
@allanrenucci allanrenucci deleted the centralize-function-logic branch December 14, 2017 16:58
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