使用大量具有自我类型的 mixin
我想构建一些 scala 类来建模 RDF。我有类和属性。这些属性混合到类中,并且由于其自身类型而可以使用 properties
哈希图。
随着类获得更多属性,我必须使用大量 mixin(50+),我想知道这是否仍然是性能方面良好的解决方案?
trait Property
trait Properties {
val properties =
new scala.collection.mutable.HashMap[String, Property]
}
abstract class AbstractClass extends Properties
trait Property1 {
this: AbstractClass =>
def getProperty1 = properties.get("property1")
}
trait Property100 {
this: AbstractClass =>
def getProperty100 = properties.get("property100")
}
class Class1 extends AbstractClass
with Property1 with Property100
I want to build some scala classes to model RDF. I have classes and properties. The properties are mixed in to the classes and can use the properties
hashmap because of their self type.
As the classes get more properties I have to use a lot of mixins (50+) and I wonder if this is still a good solution performance wise?
trait Property
trait Properties {
val properties =
new scala.collection.mutable.HashMap[String, Property]
}
abstract class AbstractClass extends Properties
trait Property1 {
this: AbstractClass =>
def getProperty1 = properties.get("property1")
}
trait Property100 {
this: AbstractClass =>
def getProperty100 = properties.get("property100")
}
class Class1 extends AbstractClass
with Property1 with Property100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后您可以按如下方式创建您的类:
您的类现在具有您想要的方法:
这当然不是很类型安全,但您的也不是。坦率地说,我认为直接使用地图会更好:
至少你不会有任何安全的幻觉
Then you could create your class as follows:
Your class now has the methods you want it to:
This is not very typesafe of course, but then neither is yours. Frankly, I think you are better off just using your map directly:
At least you are not suffering from any illusion of safety