If I have the following type
Defn.Var(mods, pats, decltpe, rhs)
in scalameta it might happen that decltype is set to None
for a variable like this:
var x = 10
I still want to know the exact type of the variable x
which Scala has inferred without checking the type of the assignment expression myself.
I know that I can just get the information that 10 is an Int literal but for more complex expressions it might be helpful to have some helper function for the type.
Is there any function in scalameta which gives you the inferred type?
Scalameta
mostly works with source code (parses it to trees, transforms trees) before its compilation i.e. when there can't be any information about symbols and types. To get information about symbols and types one should start compiler (one of).
This is exactly what SemanticDB is for. If you switch on
semanticdb-scalac
compiler pluginthen upon compilation it will generate
.semanticdb
files near to.class
files. Information about symbols and types will be there. These files can be parsed withsemanticdb
For example if you have
App1.scala
then
produces
You can pretty-print this file with
Metap
Output
And
produces
Scheme is here.
Also you can try Scalafix
If you write in
input/src/main/scala/fix/Scalafixdemo.scala
and in
rules/src/main/scala/fix/Scalafixdemo.scala
then it will print
: Int
https://github.com/DmytroMitin/scalafix-codegen