What is a Trees#Tree type?


In the scala synthax tree the Trees#Tree type appears quite often for a block of code. What is it exactly and how is it different from the Tree type? Is there a way to convert between the two?

Why did you think it’s different? In what specific case do you need to convert something?

If you are inside something extending Trees you can refer to Trees#Tree as just Tree.

In macros there is context.universe.Tree, in runtime reflection there is scala.reflect.runtime.universe.Tree.

Generally maybe the question is about the difference between type projection A#B and path-dependent type a.B.

1 Like

I need to pass in a Tree to a function and it won’t take Trees#Tree.
This is inside a traverser, where I want to call super.traverse on the body of a function.

But actually I think it was just the IDE complaining and it does just work.

I found a place where this is relevent and why I was confused. There are two libraries which have Trees and there is a type mismatch between those.

error: type mismatch; found : scala.reflect.api.Trees#Tree required: MyComponent.this.global.Tree

There’s only one library here. As Dmytro hinted, many types in the compiler are “path-dependent”; the compiler uses the “cake pattern” heavily internally. Trees#Tree is the general type (that’s a “type projection”), MyComponent.this.global.Tree is the path-dependent type specific to the particular instance of MyComponent. The latter is a subtype of the former.

It’s good that you quoted the exact error message — that helps us give relevant advice.