是否可以通过名称用字符串设置函数的参数。
例如:
下式给出:
def foo(a: String, b: String)
我可以使用类似
的Map动态调用此函数Map(("a", "bla"), ("b", "blub"))
若然,怎么样?
谢谢!
答案 0 :(得分:5)
将args的地图反射地应用于方法:
apm@mara:~/tmp$ scalam
Welcome to Scala version 2.11.0-M4 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val m = Map(("a", "bla"), ("b", "blub"))
m: scala.collection.immutable.Map[String,String] = Map(a -> bla, b -> blub)
scala> import reflect.runtime._
import reflect.runtime._
scala> import universe._
import universe._
scala> class Foo { def foo(a: String, b: String) = a + b }
defined class Foo
scala> val f = new Foo
f: Foo = Foo@2278e0e7
scala> typeOf[Foo].member(TermName("foo"))
res0: reflect.runtime.universe.Symbol = method foo
scala> .asMethod
res1: reflect.runtime.universe.MethodSymbol = method foo
scala> currentMirror reflect f
res2: reflect.runtime.universe.InstanceMirror = instance mirror for Foo@2278e0e7
scala> res2 reflectMethod res1
res3: reflect.runtime.universe.MethodMirror = method mirror for Foo.foo(a: String, b: String): java.lang.String (bound to Foo@2278e0e7)
scala> res1.paramss.flatten
res5: List[reflect.runtime.universe.Symbol] = List(value a, value b)
scala> .map(s => m(s.name.decoded))
res7: List[String] = List(bla, blub)
scala> res3.apply(res7: _*)
res8: Any = blablub
请注意,您需要展平参数列表paramss.flatten
。这是一个有序的参数列表,你可以轻松地映射到你的args。
聪明的命名约定paramss
旨在传达多重嵌套;我个人认为它好像用英语拼写paramses
。
但是现在我看到它拼写出来了,我可以用埃及法老来说它与它押韵。
答案 1 :(得分:0)
好的,这里有反思。它不是通过Map
按参数名称绑定的,但是你必须按顺序保存一个带有参数值的序列,所以第一个值是a
,第二个值是b
等。我会说足够接近
所有内容都取自here,实际上它是镜像的绝佳资源。
scala> import scala.reflect.runtime.{ universe => ru }
import scala.reflect.runtime.{universe=>ru}
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> class Bar { def foo(a : String, b : String) { println(a + ", " + b + "!")
}}
defined class Bar
scala> val instanceMirror = ru.runtimeMirror(getClass.getClassLoader).reflect(new Bar)
instanceMirror: reflect.runtime.universe.InstanceMirror = instance mirror for Bar@47f3b292
scala> val fooMethod = typeOf[Bar].declaration(newTermName("foo")).asMethod
fooMethod: reflect.runtime.universe.MethodSymbol = method foo
scala> val method = instanceMirror.reflectMethod(fooMethod)
method: reflect.runtime.universe.MethodMirror = method mirror for Bar.foo(a: String, b: String): scala.Unit (bound to Bar@47f3b292)
scala> val args = Seq("Hey", "you")
args: Seq[String] = List(Hey, you)
scala> method(args: _*)
Hey, you!
res0: Any = ()
如果方法返回任何内容,没问题,但返回类型为Any
(docs):
scala> class Bar { def foo(a : String, b : String) = a + b }
defined class Bar
scala> val instanceMirror = ru.runtimeMirror(getClass.getClassLoader).reflect(ne
w Bar)
instanceMirror: reflect.runtime.universe.InstanceMirror = instance mirror for Bar@642e0260
scala> val fooMethod = typeOf[Bar].declaration(newTermName("foo")).asMethod
fooMethod: reflect.runtime.universe.MethodSymbol = method foo
scala> val method = instanceMirror.reflectMethod(fooMethod)
method: reflect.runtime.universe.MethodMirror = method mirror for Bar.foo(a: String, b: String): java.lang.String (bound to Bar@642e0260)
scala> val args = Seq("Testing", "concatenation")
args: Seq[String] = List(Testing, concatenation)
scala> method(args: _*)
res5: Any = Testingconcatenation
答案 2 :(得分:0)
没有反思,你的设置:
val valmap = Map (("a", "bla"), ("b", "blub"))
def foo (a: String, b: String) = a + "-baz-" + b
负责名称映射的函数:
def fmap (m: Map[String, String]) : String = (m.get("a"), m.get ("b")) match {
case (Some(s), Some (t)) => foo (s, t)
case _ => "didn't match"
}
Invokation:
fmap (valmap)
// res0: String = bla-baz-blub
比fmap更好的名称(可能与flatmap混淆)可能很有用。请注意,Google地图并不保证包含某些密钥,因此我们不会收到字符串,但会返回字符串选项。
在编译时保证名称不匹配,最后用s和t调用函数。但偶尔你可能需要这样的功能,并有内部证明," a"和" b"在地图上。当然你可以调用每个方法,这个方法需要2个字符串,任意名称,最简单的一个用b,a代替a,b:
// ...
case (Some(s), Some (t)) => foo (t, s)
// ...
使用Array的类似方法:
val arr: Array[String] = (Seq.fill ('c')("not initialized")).toArray
arr('a')="bla"
arr('b')="blub"
def fmap (ar: Array[String]) : String = foo (ar('a'), ar('b'))
fmap (arr)
Seq.fill(' c')...从' \ 0'填充数组。为了得到一个非常稀疏的数组,只需要处理2个值。如果您要填写它直到'#,您可以动态调用该函数:
def fmap (ar: Array[String], c1: Char, c2: Char) : String = foo (ar(c1), ar(c2))
fmap (arr, 'a', 'b')
fmap (arr, 'b', 'a')
fmap (arr, 'a', 'a')
fmap (arr, 'i', 't')
等等。