Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a value of some path-dependent type, how can I get an instance of the "container"?

It's more easily explained in code:

class Bippy {
  val x = 42

  class Boppy {
    val y = "hello world"
  }

  val bop = new Boppy
}

val bip = new Bippy
val bop: Bippy#Boppy = bip.bop

bop is then supplied to another method, which needs to find the value x from the containing instance of Bippy. What's the magic incantation to do so?

The instance bop comes from code that I don't control, so adding new methods to Boppy isn't an option here.

like image 587
Kevin Wright Avatar asked Feb 07 '11 21:02

Kevin Wright


1 Answers

You can't. At least not without cheating. Here's how to cheat.

def getOuter(bop : Bippy#Boppy) = 
   bop.asInstanceOf[{def Bippy$Boppy$$$outer() : Bippy}].Bippy$Boppy$$$outer()

Obviously that's very dependent on details of how scalac works today and no guarantees that it will work tomorrow.

like image 185
James Iry Avatar answered Sep 19 '22 12:09

James Iry