使用大量具有自我类型的 mixin

发布于 2025-01-01 12:42:45 字数 618 浏览 4 评论 0原文

我想构建一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2025-01-08 12:42:45
scala> trait PropertyN { self: Dynamic =>
   | def props: Map[String, String]
   | def applyDynamic(meth: String)(args: Any*) = props get meth
   | }
defined trait PropertyN

然后您可以按如下方式创建您的类:

scala> class MyClass(val props: Map[String, String]) extends PropertyN with Dynamic
defined class MyClass

您的类现在具有您想要的方法:

scala> new MyClass(Map("a" -> "Hello", "b" -> "World"))
res0: MyClass = MyClass@367013

scala> res0.a
dynatype: $line3.$read.$iw.$iw.res0.applyDynamic("a")()
res1: Option[String] = Some(Hello)

这当然不是很类型安全,但您的也不是。坦率地说,我认为直接使用地图会更好:

res0.properties get "a"

至少你不会有任何安全的幻觉

scala> trait PropertyN { self: Dynamic =>
   | def props: Map[String, String]
   | def applyDynamic(meth: String)(args: Any*) = props get meth
   | }
defined trait PropertyN

Then you could create your class as follows:

scala> class MyClass(val props: Map[String, String]) extends PropertyN with Dynamic
defined class MyClass

Your class now has the methods you want it to:

scala> new MyClass(Map("a" -> "Hello", "b" -> "World"))
res0: MyClass = MyClass@367013

scala> res0.a
dynatype: $line3.$read.$iw.$iw.res0.applyDynamic("a")()
res1: Option[String] = Some(Hello)

This is not very typesafe of course, but then neither is yours. Frankly, I think you are better off just using your map directly:

res0.properties get "a"

At least you are not suffering from any illusion of safety

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文