[Solved] How to avoid the use of asInstanceOf while passing data between modules in Scala


I don’t understand the question. I think you have either made a separate mistake and misattributed your error to the phantom symptom you describe in the question, or I have misunderstood you.

Here is a version of your posted code, with minor tweaks that appear to me to be unrelated to your question, that compiles just fine (try it here):

class Program { }
object Program { }
trait Logic {}


object Generator{
    val program: Program = new Program

  def met = {
    val mychecker = Checker(program)
    mychecker.check
  }
}

trait Checker {
  val program: Program 
  def check
}

trait Order {
  val checker: Checker
  def met1: Program = checker.program
}

object Checker{
  def apply(p: Program) = new { 
    val program: p.type = p
  } with Checker { self =>
    object AnOrder extends {
      val checker: self.type = self     
    } with Order

    val order = AnOrder

    def check = println("check here")
  }

}

println("compiled OK")

Note that I have changed def met1: Nothing to def met1: Program and it works fine.

You asked:

How you can avoid converting with asInstanceOf when you have two modules M1 and M2, one passes some data of type T to M2, which does some computation and then returns the very same datatype

I think that you do not need to use asInstanceOf when two modules M1 and M2 both interact with a common type, like Program in this case.

7

solved How to avoid the use of asInstanceOf while passing data between modules in Scala