Discover if a class has been declared inside a particular module type - scala

I am writing a DSL which describes a structure. The DSL uses Types to reference classes which will later be instantiated. I would like to enforce that a particular Type has been declared within a particular module. This can be a runtime check after the DSL has been compiled.
In essence I need to access the outer element starting from the inner class and check it is of the correct type and get a reference to it.
If I get the child type I can get its symbol using reflection by calling to typeSymbol and in the resulting symbol I see that I can call owner to get the outer type symbol. However if I try to reflect the parent as a Module (even when the parent it is a module) it throws an exception.
Lets put an example:
trait TheMixin
object TheParent extends TheMixin {
case class TheChild()
}
object TestDiscoverParent extends App {
import scala.reflect.runtime.{currentMirror => cm, universe => ru}
val theChildType = ru.typeOf[TheParent.TheChild]
val theChildOwner = theChildType.typeSymbol.owner
println(theChildOwner)
val theParentModuleSymbol = theChildOwner.asModule
val theParentRef = cm.reflectModule(theParentModuleSymbol).instance
println(theParentRef.isInstanceOf[TheMixin])
}
In this example, the line println(theChildOwner) will print
object TheParent
But the call to theChildOwner.asModule throws an exception:
Exception in thread "main" scala.ScalaReflectionException: object TheParent is not a module
How can I get a reference to the outer object wrapper?

There seems to one more indirection, the owner is a "module class". I'm not exactly sure what that means, probably just that technically behind every singleton object there is also a class that is instantiated once.
So the following seems to work:
object TestDiscoverParent extends App {
import scala.reflect.runtime.{currentMirror => cm, universe => ru}
val theChildType = ru.typeOf[TheParent.TheChild]
val theChildOwner = theChildType.typeSymbol.owner
println(theChildOwner)
require(theChildOwner.isModuleClass)
val theParentModuleClass = theChildOwner.asClass
val theParentModuleSymbol = theParentModuleClass.module.asModule
val theParentRef = cm.reflectModule(theParentModuleSymbol).instance
println(theParentRef.isInstanceOf[TheMixin])
}

Related

Scala companion objects are not singleton

I have following two classes.
class A (name: String) {
}
object A {
}
According to definition of Singleton, we can have only one object of that type. However I am able to create two different objects of type A using following piece of code.
object B {
def main(args: Array[String]): Unit = {
val a = new A("Vinod")
println(a)
val b = new A("XYZ")
println(b)
}
}
can someone please explain me, where my understanding is not correct?
An object by itself is a singleton. It has its own class and no other instance of the same class exist at runtime.
However, the pattern you describe here is different: object A is not an instance of class A unless you make it so using object A extends A. You could make it the only instance of class A by making class A a sealed class, but this is unnecessary in almost all cases.
If you really want the singleton pattern, drop the class and use only object A, all of its members will be "static" in the sense of Java.
Note that the actual type of object A can be referred to as A.type, which by default is completely unrelated to type A if class A exists. Again, A.type could be a subtype of A if you explicitly make it so.
The companion object is not an instance of the companion class. They're not even the same type.
class A
object A {
var state = 0
def update() :Unit = state = state + 1
}
val abc :A = new A //instance of class A
val xyz :A.type = A //2nd reference to object A
// both reference the same singleton object
xyz.update() //res0: Unit = ()
A.state //res1: Int = 1
abc.state //Error: value state is not a member of A$A2521.this.A
the companion object can be thought of as the static space of a class. if you want to make A a singleton you can make it an object rather than a class
new A refers to class A (which is not a singleton), not to object A. You can easily check it: if you remove class A, the new A lines will no longer compile.
Also note that objects aren't necessarily singletons: they can be nested inside classes or traits, in this case there is one for each instance of the outer type.

Scala/Akka Syntax

I'm new to both Scala and Akka and have been following this tutorial. I came across the following and wondering what exactly this syntax does/mean?
import akka.actor.Props
val props1 = Props[MyActor] //Not sure what this means???
val props2 = Props(new ActorWithArgs("arg")) // careful, see below
val props3 = Props(classOf[ActorWithArgs], "arg")
I'm not sure what the line commented with //Not sure what this means does? It seems like a generic trait that gives a parameterised type. If I look at the source code, akka.actor.Props is defined as an Object that extends the trait AbstractProps. However, AbstractProps is not defined with a type parameter i.e. AbstractProps[T]. Can someone explain how that above line works and what it does?
In Scala, any object which implements an apply method can be called without the new keyword, simply by calling MyObject(), which will automatically lookup for it's apply.
If you look at the companion object for Props, you'll see the following method defined:
/**
* Scala API: Returns a Props that has default values except for "creator"
* which will be a function that creates an instance
* of the supplied type using the default constructor.
*/
def apply[T <: Actor: ClassTag](): Props =
apply(defaultDeploy, implicitly[ClassTag[T]].runtimeClass, List.empty)
This apply takes one type parameter and no arguments. T <: Actor means that T, the type you're passing, must extend Actor. That's how Scala knows how to create the object.
Additionally, any method with arity-0 in Scala may drop it's parenthesis. That's how you're seeing Props[MyActor] actually compile, as it is equivalent of Props[MyActor](), which is equivalent to Props.apply[MyActor]().
akka.actor.Props is defined as an Object that extends the trait
AbstractProps
Its also defined as a case class:
final case class Props(deploy: Deploy, clazz: Class[_], args: immutable.Seq[Any])
This is a common pattern in Scala, a class with a companion object. The companion object frequently houses factory methods, and thats what your actually calling in your example.
val props1 = Props[MyActor]
This simply calls apply() of the companion object. You can omit the parentheses in Scala if no arguments are neccessary and apply is a special method that can be invoked directly on the object/instance. Say you have a sequence and want the element at index 1:
val s = Seq("one", "two", "three")
// These two are equivalent
s(1) // -> "two"
s.apply(1) // -> "two"
Ultimately your code can be rewritten as
val props1 = Props.apply[MyActor]()

Can I get an instance of outer class using reflection on inner class type?

I'm trying to use scala reflection to achieve this:
val labelBar = Named("bar")
val labelFoo = NamedFunc("foo", (i: Int) => println(i))
val i = newInstance[
labelBar.Var[Int] +
labelFoo.Func[Int => Unit]
]
i.foo(2)
i.bar = 20
It uses a newInstace method to create a new object with specified members. To allow dynamic members, I use the scala's Dynamic class.
newInstance takes an implicit TypeTag argument. Var is an inner classes for the Named class:
case class Named(name: String) {
outer =>
class Var[T]() {
def getOuter: Named = outer
}
}
class +[A, B]
My problem is that I can't use reflectClass on Type representing inner class. It throws this:
scala.ScalaReflectionException: class asVar is an inner class, use reflectClass on an InstanceMirror to obtain its ClassMirror
at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$abort(JavaMirrors.scala:115)
at scala.reflect.runtime.JavaMirrors$JavaMirror.ErrorInnerClass(JavaMirrors.scala:117)
at scala.reflect.runtime.JavaMirrors$JavaMirror.reflectClass(JavaMirrors.scala:183)
Is there a way to get around it? Does Scala even hold this instance?
Edit 1:
newInstance method doesn't take the labelFoo instance as an argument, so I can't follow the advice in exception message.
Is there a way to get around it?
Yes, and the error message clearly says what it is:
use reflectClass on an InstanceMirror to obtain its ClassMirror
I.e. instead of mirror.reflectClass(classSymbol) call mirror.reflect(labelBar).reflectClass(classSymbol) (assuming you want to get a ClassMirror for a class nested in labelBar). To find the instance, you can try to match typeTag.tpe against TypeRef.

Method cannot be accessed in Macro generated class

I have the following macro defining a class and returning an instance of that class (with Scala 2.10.2 and the macro plugin):
def test[T] = macro testImpl[T]
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val className = newTypeName("Test")
c.Expr { q"""
class $className {
def method = 1
}
new $className
"""}
}
When I call the macro:
case class Cat(name: String)
val t = test[Cat].method
I get the following error:
method method in class Test cannot be accessed in Test
val t = test[Cat].method
^
My overall goal is to use vampire methods and to use quasi-quotes to describe the generated class. How can I solve this error?
In my post on vampire methods I mention this workaround for this bug. For some reason you currently aren't able to see an anonymous class's methods on the instance returned from the macro unless you create a wrapper class that extends the class with the methods and return an instance of that, instead.
You're seeing the same bug from a slightly different angle. You've named the class with the methods you want to see on the returned instance's structural type, but you still need a wrapper. The following will work:
c.Expr { q"""
class $className {
def method = 1
}
new $className {}
"""}
Note that all I've done is add a pair of brackets to the line creating the instance, so that I get an instance of an anonymous class extending $className instead of just a $className.
I have no idea what's behind this bug, and I'm not sure if Eugene knows more. I did recently confirm that it's still around in the latest build of 2.11.

Scala reflection error: this is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror

Following up on this question, I'm trying to figure out how to call a method on an object. The relevant definitions are:
trait ThirdParty { def invoke = println("right") }
trait WeatherIcon { def invoke = println("wrong") }
class MyClass {
object objA extends ThirdParty
object objB extends WeatherIcon
}
I got a Symbol for objA like this:
import reflect.runtime.universe._
val stuff = typeOf[MyClass].members.filter(_.isValue).filter(_.typeSignature <:< typeOf[ThirdParty])
That returns an Iterable with a single element, so let's say:
val objASymbol = stuff.head.asModuleSymbol
I then tried, based on this other question, this:
val mirror = runtimeMirror(getClass.getClassLoader)
mirror.reflectModule(objASymbol)
Which resulted in the error message quoted on the subject:
java.lang.Error: this is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror
at scala.reflect.runtime.JavaMirrors$JavaMirror.reflectModule(JavaMirrors.scala:118)
at scala.reflect.runtime.JavaMirrors$JavaMirror.reflectModule(JavaMirrors.scala:60)
The problem is that I can't figure out what this error message is telling me to do!
You need to write runtimeMirror.reflect(<instance of MyClass>).reflectModule(objASymbol). Plain reflectModule won't do, because some reflective operations on objA (e.g. getting its instance) require an outer instance.
Unfortunately, your use case won't work even if you write it right, because M4 only supports static objects: https://issues.scala-lang.org/browse/SI-5498. We'll implement this before 2.10.0-final.