我回答一個問題關於Defining a Map from String to Function in Scala地圖的功能而導致Function1[_, String]
我相信這是正確的,只要打字的問題,但可能是無用的,因爲我不知道如何調用這個函數:如何在Scala中調用Function1 [_,String]?
scala> def f(x: Int) = x.toString
f: (x: Int)java.lang.String
scala> f(8)
res0: java.lang.String = 8
scala> val g: Function1[_, String] = f _
g: Function1[_, String] = <function1>
scala> g(8)
<console>:8: error: type mismatch;
found : Int(8)
required: _$1 where type _$1
g(8)
^
scala> val h: Function1[Int, String] = g
<console>:7: error: type mismatch;
found : (_$1) => String where type _$1
required: (Int) => String
val h: Function1[Int, String] = g
有什麼方法可以使用g
?
g.asInstanceOf [Function1 [Int,String]](8)的作品,但我不認爲這是一個很好的答案...不是有任何。 – pedrofurla 2011-01-06 17:58:16
假設[另一個問題]的語法(http://stackoverflow.com/questions/4617660/defining-a-map-from-string-to-function-in-scala)(不是你的答案,但實際的原始問題)實際上起作用了,調用地圖中函數的代碼是什麼樣的?在執行Map查找之後,他打算如何將正確的類型發送給正確的功能? – 2011-01-06 18:02:30