Skip to content

Fix #3866: Add support for typetags #3867

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 12 commits into from
Jan 27, 2018
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 18, 2018

Based on feedback with the early implementation, we found out that we need type lifting
in the framework. The first commit explains what needs to be done in the meta programming doc.

The other commits are a first implementation. I am still not quite happy with it because we are lacking a way to do quoting and splicing directly on types. So for now we generate a type tree, but this cannot express all possible positions of typetags. For instance, we could not splice
a type tag in the position of a type parameter of a refined method since refined methods are
represented as types, not as type trees.

So maybe we need to take a step back and allow that. But that would upset quite a few core structures of the compiler.

@odersky odersky requested a review from nicolasstucki January 18, 2018 17:14
@odersky
Copy link
Contributor Author

odersky commented Jan 18, 2018

test performance please

@dottybot
Copy link
Member

performance test scheduled: 4 job(s) in queue, 1 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/3867/ to see the changes.

Benchmarks is based on merging with master (79281da)

- title: Symmetric Meta Programming
url: docs/reference/symmetric-meta-programming.html
- title: Meta Programming
url: docs/reference/principled-meta-programming.html
Copy link
Contributor

Choose a reason for hiding this comment

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

The file name needs to be changed to principled-meta-programming.md as well.

@odersky
Copy link
Contributor Author

odersky commented Jan 19, 2018

I think we found our performance regression culprit causing #3721: isQuote and isSplice did a member lookup on every call because they were implemented in a sub-optimal way in Definitions.
Fixed by e6664ed

}
def reflect[T, U](f: Expr[T] => Expr[U])(implicit t: Type[T]): Expr[T => U] =
’{ (x: ~t) => ~f(’(x))

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing } at the end

@@ -9,6 +9,8 @@ abstract class Type[T] extends Quoted {
/** Some basic type tags, currently incomplete */
object Type {

implicit def apply[T]: Type[T] = ???
Copy link
Contributor

Choose a reason for hiding this comment

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

Are calls to this methods going to be replaced by the tags below when possible and replaced '[T] if the type is statically known?

def unary_~: T // splice operation
}
class Type[T] {
sealed abstract class Type[T] {
Copy link
Contributor

Choose a reason for hiding this comment

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

@odersky I created #3883 to make these classes sealed, no need to implement it on this PR.

@odersky odersky changed the title [WIP] Add type lifting Fix #3866: Add support for typetags Jan 22, 2018
@odersky odersky assigned nicolasstucki and unassigned odersky Jan 22, 2018
Copy link
Contributor

@nicolasstucki nicolasstucki left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM

reference to a type `T` to a type-splice, by rewriting `T` to `~implicitly[Type[T]]`.
For instance, the user-level definition of `reflect`

def reflect[T: Type, U](f: Expr[T] => Expr[U]) Expr[T => U] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing : before result type.


would be rewritten to

def reflect[T: Type, U](f: Expr[T] => Expr[U]) Expr[T => U] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing : before result type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

}

val tag = toTree(tpe)
if (ok) ref(defn.typeQuoteMethod).appliedToTypeTrees(tag :: Nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future, we should optimize primitive types to return quoted.Type.XYZTag. But we should do it in general for any '[XYZ] that has a quoted.Type.XYZTag. Probably as part of reification.

RefinedTypeTree(toTree(tpe), refinements, refineCls)
}

def toTree(tpe: Type): Tree = tpe.stripTypeVar match {
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed, we should try using the same trick to no need all these trees.

@nicolasstucki nicolasstucki self-requested a review January 23, 2018 13:49
Copy link
Contributor

@nicolasstucki nicolasstucki left a comment

Choose a reason for hiding this comment

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

LGTM, just missing a couple of details in the documentation.

reference to a type `T` to a type-splice, by rewriting `T` to `~implicitly[Type[T]]`.
For instance, the user-level definition of `reflect`:

def reflect[T: Type, U](f: Expr[T] => Expr[U]) Expr[T => U] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Still missing a :


would be rewritten to

def reflect[T: Type, U](f: Expr[T] => Expr[U]) Expr[T => U] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Based on feedback with the early implementation, we found out that we need type lifting
in the framework. This commit explains what needs to be done in the meta programming doc.
It's not so easy to see what is symmetric about it. "Principled" is bolder, but why not, since we do introduce a new principle?
This is intended as a stub. Calls to this method will be replaced
by implicit type tag searches.

Also, rework definition of quote and splice methods. This could also
have contributed in the slowdown we were seeing in scala#3721.
We need to come back to the earlier scheme that a lifted type
needs to refer to an implicit type tag.
Not sure this is the right way. It feels a bit roundabout to take the detour
from types to type trees. But I am not sure we can correctly quote types without
trees.
Generate a type with embedded quotes instead of a tree.
@odersky odersky merged commit 31a81fb into scala:master Jan 27, 2018
@allanrenucci allanrenucci deleted the fix-typetags branch January 27, 2018 15:41
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.

4 participants