编辑1
我已经看到了这个问题:在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
编辑2
或者在某种意义上,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)
我做错了什么?如何将元组参数应用于“varagrs”样式的函数?