Scala 不可变对象和具有 val 字段的特征
我想仅使用不可变对象构建我的域模型。但我也想将特征与 val 字段一起使用,并将一些功能移至特征。请看下面的示例:
trait Versionable {
val version = 0
def incrementVersion = copy(version=version+1)
}
不幸的是,这样的代码不起作用 - 复制方法对于特征版本化来说是未知的。
我认为为每个特征和类生成复制方法会很好。此类方法应创建对象的浅表副本,并使用与原始对象相同的类型返回它,并根据传递给方法的参数修改给定字段。
因此,在下面的示例中:
class Customer(val name: String) extends Versionable {
def changeName(newName: String) = copy(name = newName)
}
val customer = new Customer("Scot")
customer.changeName("McDonald")
应返回一个对象实例 Customer(version = 0, name = "McDonald")
和
customer .incrementVersion
还应该返回一个对象实例 Customer(version = 1, name = "Scot")
据我所知,Scala 目前缺乏此类功能,不允许使用不可变类和特征,而不用特征字段污染类构造函数。在我的示例中,我不想向 Customer 类引入名为 version 的参数,因为我希望将版本处理的功能封装在 Versionable 特征中。
我知道案例类中复制方法的功能,以及使用默认参数在类中编写自己的复制方法的能力 - 但我认为此功能不能解决我的问题,因为不可能在特征中使用此类复制方法。现有功能的另一个缺点是使用复制方法的父类返回父类,而不是实际复制的对象的类。
我的问题:
1)您知道如何以优雅的方式处理上面的示例吗?我对 Scala 很陌生,所以也许已经有很好的解决方案了。在我看来,优雅的解决方案应该具有以下特征:
不应该使用反射
不应该使用序列化
应该很快
应该在编译时可验证
2) 您对编写编译器插件来为上面的示例生成复制方法的代码有何看法?是否可以使用编译器插件来做到这一点?您有任何示例或提示如何做到这一点吗?
I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example:
trait Versionable {
val version = 0
def incrementVersion = copy(version=version+1)
}
Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable.
I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of object and return it using the same type as for original object with given field modified accoring to arguments passed to method.
So in the following example:
class Customer(val name: String) extends Versionable {
def changeName(newName: String) = copy(name = newName)
}
val customer = new Customer("Scot")
customer.changeName("McDonnald")
should return an object instance Customer(version = 0, name = "McDonnald")
and
customer.incrementVersion
should also return an object instance Customer(version = 1, name = "Scot")
As far as I know current lack of such functionality in Scala doesn't allow to use immutable classes and traits without polluting class constructor with trait fields. In my example I don't want to introduce parameter named version to Customer class because functionality of version handling I want to have encapsulated in Versionable trait.
I know functionality of copy method in case classes and ability to write own copy method in class using default parameters - but I think that this functionality doesn't solve my problem because it is not possible to use such copy method in traits. Another drawback of existing functionality is that parent class using copy method returns parent class and not class of object that is actually copied.
My questions:
1) do you have idea how to handle above example in elegant way. I'm quite new to Scala so maybe there is good solution already. In my opinion elegant solutions should have following features:
should not use reflection
should not use serialization
should be fast
should be verifiable in compile time
2) what do you think about writing compiler plugin to generate code for copy method for my above example? Is it possible to do that using compiler plugin? Do you have any examples or tips how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最干净的解决方案可能是从版本化中删除一些实现逻辑,并将其从类型堆栈中推送到案例类(其中您可以使用复制方法)。为版本属性指定默认值以完成设计。
如果需要,您还可以在某处为版本编号定义类型别名:
You cleanest solution is probably to drop some implementation logic from
Versionable
, and push it down the type stack to a case class (where thecopy
method will be available to you). Give the version property a default value to complete the design.If you want, you can also define a type alias somewhere for the version numbering:
这是另一个解决方案,与OP的代码一样,不起作用。然而,它可以为扩展语言提供一个更简单(并且更普遍有用)的起点。
如果编译器将 Customer 类的
copy
方法识别为符合 Versionable 的自类型注释中定义的方法(这似乎是使用命名参数和默认参数的自然方式),则该代码将起作用。Here's another solution that, like the OP's code, doesn't work. However, it may provide a simpler (and more generally useful) starting point for extending the language.
The code would work if the compiler recognized the Customer class's
copy
method as conforming to the method defined in Versionable's self-type annotation, which seems like a natural way to use named and default parameters.尽管您说过,您不想使用案例类。这是使用它们的解决方案:
现在您可以这样做:
Although you said, you don't want to use case classes. Here is a solution using them:
Now you can do this:
这应该可以满足您的需求:
然后您可以像这样使用它
This should do what you are looking for:
Then you can use it like
很难看出它是如何工作的以及如何与 Scala 的语义保持一致——特别是在特征中定义的不可变字段的语义。考虑 Versionable 特征:
此声明表示,除非被覆盖,版本字段的值将始终为 0。要更改
version
的值,“不会用特征字段污染类构造函数”(即没有显式地覆盖版本字段)将违反这些语义。It's difficult to see how this would work and be consistent with Scala's semantics -- in particular, the semantics of an immutable field defined in a trait. Consider the Versionable trait:
This declaration says that, unless overridden, the version field will always have the value 0. To change the value of
version
"without polluting the class constructor with trait fields" (i.e. without explicitly overriding the version field) would violate these semantics.