Scala:柯里化构造函数
我有以下 Scala 类:
class Person(var name : String, var age : Int, var email : String)
我想使用 Person 构造函数作为柯里化函数:
def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e)
这可行,但是还有其他方法可以实现此目的吗?这种方法似乎有点乏味且容易出错。我可以想象像 Function.curried 这样的东西,但是对于构造函数。
I have the following Scala class:
class Person(var name : String, var age : Int, var email : String)
I would like to use the Person constructor as a curried function:
def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e)
This works, but is there another way to accomplish this? This approach seems a bit tedious and error-prone. I could imagine something like Function.curried, but then for constructors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将起作用:
This will work:
参加这个聚会有点晚了,但是如果您将
Person
设为案例类:Scala 会生成一个包含
Person.apply(String, Int, String)
和其他一些内容的伴生对象为你。然后你可以这样做:这是缩写:
它也适用于 var 参数。
A bit late to this party, but if you make
Person
a case class:Scala generates a companion object containing
Person.apply(String, Int, String)
and some other stuff for you. Then you can do:Which is shorthand for:
It works with var parameters too.
可能是这样:
may be so: