How to acquire the package of an object? How to acquire the exact runtime type of an object?
Dear all,
Consider the following configuration
package base {
class B
}
package p1 {
class D1 extends base.B
def f(d: D1, i: Int) = i
}
package p2 {
class D2 extends base.B
def f(d: D2, i: Int) = i * 2
}
along with the following client package
package p3 {
def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}
Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like
def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)
I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?
TIA,
--Hossein
--------------------------------------------------------------------------------------------------------------
Seyed H. HAERI (Hossein)
Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany
ACCU - Professionalism in programming -
http://www.accu.org/
--------------------------------------------------------------------------------------------------------------