【发布时间】:2015-03-08 14:07:20
【问题描述】:
我有这行代码,我使用我知道的最惯用的方式来解构从函数返回的对象:
val (a, b) = foo match { case MyObjectType(a, b) => (a, b) }
对象的原型是:
case class MyObjectType(Type1: SomeType1, Type2: SomeType2)
我当然可以:
val returnType = foo
val (a, b) = (returnType.a, returnType.b)
但后者是陈述同一问题的完全不同的形式——这确实不优雅。 Scala 宏能否提供一个简洁的习语?也许允许类似以下的语法:
val (a, b) = foo deconstruct { MyObjectType(a, b) => (a, b) } // merely more shorthand, like scala allows e.g. within a map block
val (a, b) = tuplize(foo) // assumes tuplize can iterate the vals of MyObjectType
tupleResult(a, b) = foo // radical macro api exploring the limits of macro safety...
tupledVars(foo) // macro extreme
【问题讨论】:
标签: scala macros destructuring