0

Problem : I have to call Objects methods which stored in a map, for a given key how to do it ? Code :

trait Processor00 { def process(x:String): Unit } case class Processor20() extends Processor0 { override def process(x:String): Unit = println("Processor20 x :" + x) } case class Processor30() extends Processor0 { override def process(x:String): Unit = println("Processor30 x :" + x) } object UnitTest1 { def main( args : Array[String]):Unit ={ val s:String = "yes" val myFuncs: Map[String,(String) => Unit ]= Map( "string2" -> Processor20().process(s), //Eroor 1. type mismatch; found : Unit required: String ⇒ Unit "string3" -> Processor30().process(s) ) myFuncs.values.foreach(v => v());//how to call all Objects' i.e. process("Hi") here ??? } } 
1
  • @jrook , sir created a new ticket. Commented Oct 26, 2018 at 8:38

1 Answer 1

1

Processor20().process(s) is called a function process and return Unit

but Processor20().process is a function that you could save in map

Try something like:

trait Processor00 { def process(x:String): Unit } case class Processor20() extends Processor00 { override def process(x:String): Unit = println("Processor20 x :" + x) } case class Processor30() extends Processor00 { override def process(x:String): Unit = println("Processor30 x :" + x) } object Main extends App { val s: String = "yes" val myFuncs: Map[String, String => Unit] = Map( "string2" -> Processor20().process, "string3" -> Processor30().process ) myFuncs.foreach { case (key, f) => f(s) } // or f(key) } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.