我已经看到了这个问题:Applying a function to a tuple in Scala
理想情况下,我只想这样做:
scala> val t = ("A", "B", "C")
t: (java.lang.String, java.lang.String, java.lang.String) = (A,B,C)
scala> "%-10s %-50s %s".format(t) // or some closer syntax
哪个应该输出为
res12: String = A B C
或者在某种意义上,Scala编译器应该能够推断出我实际上正在调用正确的参数和类型
"%-10s %-50s %s".format(t.untuple)
扩展为"%-10s %-50s %s".format(t._1, t._2, t._3)
我可以使用宏来执行此操作吗?
我有一个元组用于格式化字符串:
scala> val t = ("A", "B", "C")
t: (java.lang.String, java.lang.String, java.lang.String) = (A,B,C)
scala> "%-10s %-50s %s".format(t.productElements.toList: _*)
warning: there were 1 deprecation warnings; re-run with -deprecation for details
res10: String = A B C
scala> "%-10s %-50s %s".format(t._1, t._2, t._3)
res11: String = A B C
到目前为止一切正常。但这失败了:
scala> val f = "%-10s %-50s %s".format(_)
f: Any* => String = <function1>
scala> f(t.productElements.toList: _*)
warning: there were 1 deprecation warnings; re-run with -deprecation for details
java.util.MissingFormatArgumentException: Format specifier '-50s'
at java.util.Formatter.format(Formatter.java:2487)
at java.util.Formatter.format(Formatter.java:2423)
at java.lang.String.format(String.java:2797)
at scala.collection.immutable.StringLike$class.format(StringLike.scala:270)
at scala.collection.immutable.StringOps.format(StringOps.scala:31)
at $anonfun$1.apply(<console>:7)
at $anonfun$1.apply(<console>:7)
at .<init>(<console>:10)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:744)
这也失败了:
scala> f.apply(t)
java.util.MissingFormatArgumentException: Format specifier '-50s'
at java.util.Formatter.format(Formatter.java:2487)
at java.util.Formatter.format(Formatter.java:2423)
at java.lang.String.format(String.java:2797)
at scala.collection.immutable.StringLike$class.format(StringLike.scala:270)
at scala.collection.immutable.StringOps.format(StringOps.scala:31)
at $anonfun$1.apply(<console>:7)
at $anonfun$1.apply(<console>:7)
at .<init>(<console>:10)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:744)
我做错了什么?如何将元组参数应用于&#34; varagrs&#34;风格功能?
答案 0 :(得分:5)
问题在于lambda定义
val f = "%-10s %-50s %s".format(_)
相当于:
val f = x => ("%-10s %-50s %s".format)(x)
,
所以你只是在这里传递序列作为第一个参数。
正确的lambda是:(x: Seq[Any]) => ("%-10s %-50s %s".format)(x: _*)
或者只是val f: Seq[Any] => String = "%-10s %-50s %s".format
示例:
scala> val f = x => ("%-10s %-50s %s".format)(x)
f: Seq[Any] => String = <function1>
scala> f(Seq(1,2,3))
java.util.MissingFormatArgumentException: Format specifier '-50s'
scala> val f: Seq[Any] => String = "%-10s %-50s %s".format
f: Seq[Any] => String = <function1>
scala> f(Seq(1,2,3))
res75: String = 1 2 3
不幸的是,scala不理解("%-10s %-50s %s".format)(_: _*)
,所以你不能使用下划线。
这里有什么有趣的:如果你试图使用下划线而不申请 - 你不必明确指定_ *:
scala> val f = ("%-10s %-50s %s": scala.collection.immutable.StringLike[_]).format _
f: Seq[Any] => String = <function1>
scala> f(Seq(1,2,3))
res81: String = 1 2 3
但是你必须明确指定类型类,因为function _
构造doesn't work with implicits。
答案 1 :(得分:2)
您可以按如下方式定义f
:
def f(t: Product) = "%-10s %-50s %s".format(t.productIterator.toList: _*)
然后您可以将其应用于t
:
scala> f(t)
res1: String = A B C
Product
scala.Product
是trait
,由许多类实现,例如Product2
和Product3
。这些类又分别是Tuple2
和Tuple3
的超类型。
如此简单,任何元组都是Product
。这就是为什么productIterator
可用于所有元组的原因。
因此,为了将元组用作vararg,一种方法是使用productIterator
并通过调用List
方法将其转换为toList
。
以下签名可能是您的功能更好的签名:
def f(fmt: String)(t:Product) = fmt(t.productIterator.toList: _*)
现在您可以应用(或部分应用)它来获得所需的结果:
val res = f("%-10s %-50s %s")(t)
val newFormatter = f("%-10s %-40s %s")_
val newResult = newFormatter(t)