Skip to content

Add tasty reflect members for definitions #4714

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
Jun 25, 2018

Conversation

nicolasstucki
Copy link
Contributor

  1. Make the ADTs look like case classes by adding members as extension methods as abstract decorators

We can access members of

val vdef: ValDef = ...
val ValDef(name, _, _) = vdef
println(name)

With this addition we can also do

val vdef: ValDef = ...
println(vdef.name)
  1. Factor optionally for optional trees
  2. Rename arguments of decorators to give them more meaning

Copy link
Contributor

@liufengyun liufengyun 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

def TypeBoundsTreeDeco(bounds: TypeBoundsTree): TypeBoundsTreeAPI = new TypeBoundsTreeAPI {
def tpe(implicit ctx: Context): TypeBounds = bounds.tpe.asInstanceOf[Types.TypeBounds]
def low(implicit ctx: Context): TypeTree = bounds.asInstanceOf[tpd.TypeBoundsTree].lo
def hi(implicit ctx: Context): TypeTree = bounds.asInstanceOf[tpd.TypeBoundsTree].hi
Copy link
Contributor

Choose a reason for hiding this comment

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

asInstanceOf[tpd.TypeBoundsTree] is unnecessary. Same for the line before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeBoundsTree here is of type tpd.Tree but guaranteed to be tpd.TypeBoundsTree by the extractor, the cast is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, and this has all the usual dangers we've identified in our draft :-|... looking forward to a fix!

val selfVal = if (self.isEmpty) None else Some(self)
Some((x.name.toString, constr, parents, selfVal, temp.body))
val deco = ClassDefDeco(x)
Some((x.name.toString, deco.constructor, deco.parents, deco.self, deco.body))
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling ClassDefDeco seems to create unnecessary indirection here.

Another thought irrelevant to this PR but about the design: ClassDef and ClassDefDeco seem to have some duplication (that is why ClassDefDeco is called above). It seems possible to simplify the protocol between tasty API and concrete implementation such that extractors are implemented in the abstract API? Just a thought, no need to address in this PR.

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 was a simple way to reduce code duplication. I'm considering extending the ClassDefAPI to make it the result of the extractor (with name-based pattern matching). This way there will be only one instantiation and maybe the contents can be lazily extracted.

@nicolasstucki nicolasstucki merged commit f2f190d into scala:master Jun 25, 2018
@allanrenucci allanrenucci deleted the tasty-reflect-add-members branch June 25, 2018 20:07
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