Does Scala have any equivalent to GCC's typeof
extension? (Or C++ decltype
?)
I'm generating code that references some external code (which may not be available yet), and I need a way to reference the type of that code in a method definition
For singleton objects, I could use Foo.type
, but if Foo
is an arbitrary expression, that doesn't work.
Update:
Here is a simplified example that shows the problem:
def f(x: typeof(Foo))(implicit M: Monoid[typeof(Foo)]) =
M.append(Foo, M.append(x, Foo))
The code I am working on doesn't know anything about Foo
other than that it is a string representation of a Scala expression. It is outputting the above code to a .scala
file which is to be later compiled as part of a separate project.
Of course the typeof(Foo)
bits don't work. Using Foo.type
would work only if Foo
is a singleton.
Basically, I want to know if there is something I could substitute in place of typeof(Foo)
that would work for arbitrary Scala expressions.