Can you call an object method dynamically off class parameterization in Scala? -


i'm quite new scala, i'm trying implement following situation. suppose have trait:

trait sometrait {   def kakaw } 

and 2 scala objects extend it:

object samplea extends sometrait {   def kakaw = "woof" }  object sampleb extends sometrait {   def kakaw = "meow" } 

what i'd call 1 of these 2 object functions based on parameterized function call. example (and know furthest thing correct):

class someother {   def saysomething[t] = t.kakaw } 

so can like:

val s = new someother s.saysomething[samplea] 

is @ possible in scala?

& scala welcome scala version 2.8.1.final (java hotspot(tm) 64-bit server vm, java 1.6.0_23). type in expressions have them evaluated. type :help more information.  scala> trait sometrait {      |   def kakaw : string      | } defined trait sometrait  scala> class samplea extends sometrait {      |   def kakaw = "woof"      | } defined class samplea  scala> implicit val samplea = new samplea samplea: samplea = samplea@42c71191  scala> class sampleb extends sometrait {      |   def kakaw = "meow"      | } defined class sampleb  scala> implicit val sampleb = new sampleb sampleb: sampleb = sampleb@53601a4f  scala> class someother {      |   def saysomething[ t <: sometrait](implicit target : t) = target.kakaw      | } defined class someother  scala> val s = new someother s: someother = someother@5947e54e  scala> s.saysomething[samplea] res0: string = woof 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -