-
Notifications
You must be signed in to change notification settings - Fork 17
Scala AST reference
Vivek Srikumar edited this page Jan 2, 2014
·
52 revisions
-
All the types (say
X
) should be prefixed byglobal.X
when used in a case statement. -
Type names (like Int, Map, etc) either belong to a class called
TypeName_S
orTypeName_R
. But these are private members of the classNames
so we cannot pattern match against them. However, any AST node that is a type name has the flagisTypeName
set totrue
. This could be used to identify type names. Below, all type names are represented by the stringnewTypeName("<some-name>")
.
Here are some examples.
Scala type | AST case class |
---|---|
Int |
Ident(newTypeName("Int")) |
String |
Ident(newTypeName("String")) |
Foo |
Ident(newTypeName("Foo")) |
java.util.Random |
Select(Select(Ident(newTermName("java")), newTermName("util")), newTypeName("Random")) |
scala.util.Random |
Select(Select(Ident(newTermName("scala")), newTermName("util")), newTypeName("Random")) |
Set[String] |
AppliedTypeTree(Ident(newTypeName("Set")),List(Ident(newTypeName("String")))) |
Map[String, Int] |
AppliedTypeTree(Ident(newTypeName("Map")), List(Ident(newTypeName("String")), Ident(newTypeName("Int")))) |
(String, Int) |
AppliedTypeTree(Select(Ident(scala), newTypeName("Tuple2")), List(Ident(newTypeName("String")), Ident(newTypeName("Int")))) |
(String, Int, Double) |
AppliedTypeTree(Select(Ident(scala), newTypeName("Tuple3")), List(Ident(newTypeName("String")), Ident(newTypeName("Int")), Ident(newTypeName("Double")))) |
String => Double |
AppliedTypeTree(Select(Select(Ident(nme.ROOTPKG), scala), newTypeName("Function1")), List(Ident(newTypeName("String")), Ident(newTypeName("Double")))) |
(String, Int) => Double |
AppliedTypeTree(Select(Select(Ident(nme.ROOTPKG), scala), newTypeName("Function2")), List(Ident(newTypeName("String")), Ident(newTypeName("Int")), Ident(newTypeName("Double"))) |
(String, Int, Boolean) => Double |
AppliedTypeTree(Select(Select(Ident(nme.ROOTPKG), scala), newTypeName("Function3")), List(Ident(newTypeName("String")), Ident(newTypeName("Int")), Ident(newTypeName("Boolean")), Ident(newTypeName("Double")))) |
String => Int => Double |
AppliedTypeTree(Select(Select(Ident(nme.ROOTPKG), scala), newTypeName("Function1")), List(Ident(newTypeName("String")), AppliedTypeTree(Select(Select(Ident(nme.ROOTPKG), scala), newTypeName("Function1")), List(Ident(newTypeName("Int")), Ident(newTypeName("Double")))))) |
Notes
- In general,
AppliedTypeTree
takes two arguments. The first argument tells us about the type and the second argument is a list that gives information about the types of parameters used to construct the types. -
Select
is used to pick a member of a type or an object. We will see more of this below too. - Some
Ident
s take the word scala as a parameter. Most likely, we won't be using this. So, for now, let's ignore them. Ditto fornme.ROOTPKG
.
-
type Person = <some-type>
becomesTypeDef(name =TypeName_R("Person"), rhs = <a tree that represents the right hand side>)
The rhs tree above should be a Type tree as described in the section Types.