Is there any Scala feature that allows you to call a method whose name is stored in a string?

后端 未结 4 1369
刺人心
刺人心 2020-12-05 03:20

Assuming you have a string containing the name of a method, an object that supports that method and some arguments, is there some language feature that allows you to call th

4条回答
  •  悲&欢浪女
    2020-12-05 03:50

    scala> val commandExecutor = Map("cleanup" -> {()=> println("cleanup successfully")} )
    commandExecutor: scala.collection.immutable.Map[String,() => Unit] = Map(cleanup -> )
    
    scala> val command="cleanup"
    command: String = cleanup
    
    scala> commandExecutor(command).apply
    cleanup successfully
    

提交回复
热议问题