简体   繁体   English

Scala案例类映射扩展

[英]Scala Case Class Map Expansion

In groovy one can do: 在groovy中可以做到:

class Foo {
  Integer a,b
}
Map map = [a:1,b:2]
def foo = new Foo(map) // map expanded, object created

I understand that Scala is not in any sense of the word, Groovy, but am wondering if map expansion in this context is supported 据我所知,Scala对Groovy这个词没有任何意义,但我想知道在这种情况下是否支持地图扩展

Simplistically, I tried and failed with: 简单地说,我试过并失败了:

case class Foo(a:Int, b:Int)
val map = Map("a"-> 1, "b"-> 2)
Foo(map: _*) // no dice, always applied to first property

A related thread that shows possible solutions to the problem. 一个相关的线程 ,显示问题的可能解决方案。

Now, from what I've been able to dig up, as of Scala 2.9.1 at least, reflection in regard to case classes is basically a no-op. 现在,从我已经能够挖掘的内容来看,至少从Scala 2.9.1开始,关于案例类的反思基本上是无操作的。 The net effect then appears to be that one is forced into some form of manual object creation, which, given the power of Scala, is somewhat ironic. 然后,净效应似乎是强制进入某种形式的手动对象创建,考虑到Scala的强大功能,这有点具有讽刺意味。

I should mention that the use case involves the servlet request parameters map. 我应该提一下,用例涉及servlet请求参数映射。 Specifically, using Lift, Play, Spray, Scalatra, etc., I would like to take the sanitized params map (filtered via routing layer) and bind it to a target case class instance without needing to manually create the object, nor specify its types. 具体来说,使用Lift,Play,Spray,Scalatra等,我想采用已清理的params映射(通过路由层过滤)并将其绑定到目标案例类实例,而无需手动创建对象,也不指定其类型。 This would require "reliable" reflection and implicits like "str2Date" to handle type conversion errors. 这将需要“可靠”的反射,并且需要像“str2Date”那样来处理类型转换错误。

Perhaps in 2.10 with the new reflection library, implementing the above will be cake. 或许在2.10中使用新的反射库,实现以上将是蛋糕。 Only 2 months into Scala, so just scratching the surface; 进入斯卡拉只有2个月,所以只是刮擦表面; I do not see any straightforward way to pull this off right now (for seasoned Scala developers, maybe doable) 我没有看到任何直接的方法来解决这个问题(对于经验丰富的Scala开发人员,可能是可行的)

Well, the good news is that Scala's Product interface, implemented by all case classes, actually doesn't make this very hard to do. 好消息是,Scala的产品界面由所有案例类实现,实际上并没有让这很难做到。 I'm the author of a Scala serialization library called Salat that supplies some utilities for using pickled Scala signatures to get typed field information 我是一个名为Salat的Scala序列化库的作者,它提供了一些使用pickle Scala签名来获取类型字段信息的实用程序

https://github.com/novus/salat - check out some of the utilities in the salat-util package. https://github.com/novus/salat - 查看salat-util包中的一些实用程序。

Actually, I think this is something that Salat should do - what a good idea. 实际上,我认为这是Salat应该做的事情 - 这是一个好主意。

Re: DC Sobral's point about the impossibility of verifying params at compile time - point taken, but in practice this should work at runtime just like deserializing anything else with no guarantees about structure, like JSON or a Mongo DBObject. Re:DC Sobral关于在编译时无法验证params的观点 - 在实践中这应该在运行时工作,就像反序列化其他任何不保证结构的东西一样,如JSON或Mongo DBObject。 Also, Salat has utilities to leverage default args where supplied. 此外,Salat还有一些工具可以利用提供的默认args。

这是不可能的,因为在编译时无法验证是否在该映射中传递了所有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM