Scala ObservableSet Trait 的使用示例

发布于 2024-09-03 10:42:13 字数 246 浏览 8 评论 0原文

谁能帮我告诉我如何使用scala的ObservableSet特征

预先非常感谢

Could anyone help me telling me how to use scala's ObservableSet trait?

Thank you very much in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故事未完 2024-09-10 10:42:13

ObservableSet 是一个从Publisher 特征,给出一些基本的发布订阅行为。使用此功能的一个简单示例是:

scala> class Counter(var count: Int) extends Publisher[String] {
          def inc(): Unit = {
              count += 1
              super.publish("updated count to: " + count)
          }
     }

scala> class S[Evt, Pub] extends Subscriber[Evt, Pub] {
         def notify(pub: Pub, event: Evt): Unit = println("got event: " + event)
      }
defined class S

scala> val s = new S[String, Counter#Pub]     
s: S[String,Counter#Pub] = S@7c27a30c

scala> val c = new Counter(1)
c: Counter = Counter@44ba70c

scala> c.subscribe(s)

scala> c.inc
got event: updated count to: 2

参阅以下示例(类 S 定义如上):

scala> class MySet extends HashSet[Int] with ObservableSet[Int] {       
     override def +=(elem: Int): this.type = super.+=(elem);
     override def -=(elem: Int): this.type = super.-=(elem);
     override def clear: Unit = super.clear;      
}

defined class MySet

scala> val set = new MySet
set: MySet = Set()

scala> val subS = new S[Any, Any]
subCol: S[Any,Any] = S@3e898802

scala> set.subscribe(subS)

scala> set += 1
got event: Include(NoLo,1)
res: set.type = Set(1)

ObservableSet 执行类似的操作,当使用 += 或 +- 方法添加或删除元素时,它会调用publish 方法,请 通过用 Any 类型定义 S 来变得很懒,但我无法立即正确输入,并且没有花太长时间试图弄清楚它。

ObservableSet is a trait extending from the Publisher trait, giving some basic publish subscribe behaviour. A simple example of using this would be:

scala> class Counter(var count: Int) extends Publisher[String] {
          def inc(): Unit = {
              count += 1
              super.publish("updated count to: " + count)
          }
     }

scala> class S[Evt, Pub] extends Subscriber[Evt, Pub] {
         def notify(pub: Pub, event: Evt): Unit = println("got event: " + event)
      }
defined class S

scala> val s = new S[String, Counter#Pub]     
s: S[String,Counter#Pub] = S@7c27a30c

scala> val c = new Counter(1)
c: Counter = Counter@44ba70c

scala> c.subscribe(s)

scala> c.inc
got event: updated count to: 2

ObservableSet does something similar, it calls the publish method when elements are added or removed with the += or +- method, see the following example (with class S defined as above):

scala> class MySet extends HashSet[Int] with ObservableSet[Int] {       
     override def +=(elem: Int): this.type = super.+=(elem);
     override def -=(elem: Int): this.type = super.-=(elem);
     override def clear: Unit = super.clear;      
}

defined class MySet

scala> val set = new MySet
set: MySet = Set()

scala> val subS = new S[Any, Any]
subCol: S[Any,Any] = S@3e898802

scala> set.subscribe(subS)

scala> set += 1
got event: Include(NoLo,1)
res: set.type = Set(1)

I've beem lazy by defining S with types Any, but I couldn't get the typing right immediately, and haven't spend too long trying to figure it out.

梦中的蝴蝶 2024-09-10 10:42:13

所有的输入信息有点碍眼,但这就是我让它工作的方式。我欢迎有关如何使打字更加简洁的建议。 包括类型别名的各种编辑

import collection.mutable._
import collection.script._

val set = new HashSet[String] with ObservableSet[String] { }

type Msg = Message[String] with Undoable
type Sub = Subscriber[Msg, ObservableSet[String]]

val sub = new Sub() {
  def notify(pub: ObservableSet[String], event: Msg): Unit = {
    println("%s sent %s".format(pub, event))
    event match {
      case r:Remove[_] => println("undo!"); event.undo()
      case _ => 
    }
  }
}

set.subscribe(sub)

set += "foo"
set += "bar"
set -= "bar"

打印:

Set(foo) sent Include(NoLo,foo)
Set(bar, foo) sent Include(NoLo,bar)
Set(foo) sent Remove(NoLo,bar)
undo!
Set(bar, foo) sent Include(NoLo,bar)

有趣的是,撤消导致发布另一条消息...

It's a bit of an eyesore with all the typing information, but this is how I was able to get it to work. I welcome suggestions on how to make the typing more concise. various edits including type aliases:

import collection.mutable._
import collection.script._

val set = new HashSet[String] with ObservableSet[String] { }

type Msg = Message[String] with Undoable
type Sub = Subscriber[Msg, ObservableSet[String]]

val sub = new Sub() {
  def notify(pub: ObservableSet[String], event: Msg): Unit = {
    println("%s sent %s".format(pub, event))
    event match {
      case r:Remove[_] => println("undo!"); event.undo()
      case _ => 
    }
  }
}

set.subscribe(sub)

set += "foo"
set += "bar"
set -= "bar"

That prints:

Set(foo) sent Include(NoLo,foo)
Set(bar, foo) sent Include(NoLo,bar)
Set(foo) sent Remove(NoLo,bar)
undo!
Set(bar, foo) sent Include(NoLo,bar)

Interestingly, undo caused another message to be published...

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