Scala 集合中的通用类型类

发布于 2024-12-18 19:23:25 字数 939 浏览 1 评论 0原文

我在 Scala 中创建的通用类遇到问题。我有以下课程:

class Channel[T, U](val endPoint : EventSource[U], val filter : Occurrence[T] => Boolean,
    val map : Occurrence[T] => Occurrence[U]) {

    def send(occurrence : Occurrence[T]) {
        if (filter(occurrence)) {
            endPoint.occur(map(occurrence))
        }
    }
}

这里 Channel[T,U] 可以被视为将 Occurrence[T] 从 EventSource[T] 传播到 EventSource[U] 的一种方式。仅当过滤器函数为 true 时才会发送该事件,如果是,则将该事件传递给映射并发送其结果。

这个类似乎可以正确编译和运行。我的问题是我想将多个 Channel 附加到 EventSource[T] 实例,以便它可以将出现次数传播到不同类型的多个不同 EventSource。我的困惑基本上是如何做到这一点:

class EventSource[T] {
    var List[Channel[T,U]] list = ...
}

U在这里是什么? T 只是从 list 所属(是其成员)的 EventSource 中的类型 T 引用的。

抱歉,如果这含糊或令人困惑!

编辑:我应该注意到,我也希望能够附加到此列表,如下所示:

list = list ++ List[Channel[T, U](new Channel[T, U](endPoint, filter, map))

附加是真正的问题吗?

I'm having a problem with a generic class I'm creating in Scala. I have the following class:

class Channel[T, U](val endPoint : EventSource[U], val filter : Occurrence[T] => Boolean,
    val map : Occurrence[T] => Occurrence[U]) {

    def send(occurrence : Occurrence[T]) {
        if (filter(occurrence)) {
            endPoint.occur(map(occurrence))
        }
    }
}

Here a Channel[T,U] can be seen as a way of propagating an Occurrence[T] from an EventSource[T] to an EventSource[U]. The occurrence is only sent if the filter function is true and if so the occurrence is passed to map and the result of this is sent.

This class seems to compile and function correctly. My problem is that I want to attach several Channels to an EventSource[T] instance so it can propagate Occurrences to several different EventSources of different types. My confusion is basically how to do this:

class EventSource[T] {
    var List[Channel[T,U]] list = ...
}

What is U here? T is simply referenced from the type T in the EventSource that list belongs to (is a member of).

Sorry if this is a vague or confusing!

EDIT: I should have noted that I also want to be able to append to this list as in:

list = list ++ List[Channel[T, U](new Channel[T, U](endPoint, filter, map))

Is the append the real problem?

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

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

发布评论

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

评论(2

心是晴朗的。 2024-12-25 19:23:25

如果我正确理解您的问题,您可以使用 Any:

class EventSource[T] {
  val list: List[Channel[T, Any]] = ...

编辑:您使用附加的代码示例是否被复制?因为我注意到您缺少 Channel 的类型。
另外,如果您只想将一个元素添加到列表中,您可以使用 cons,它将新元素添加到列表的开头:

Channel[Your, Types](your, para, meters)::list

如果您出于某种原因绝对想将新元素添加到列表末尾 em> 该列表中,您可以使用 :+

If I understand your problem correctly, you could use Any:

class EventSource[T] {
  val list: List[Channel[T, Any]] = ...

EDIT: Is your code example where you use append copied? Because I noted that you're missing the types for Channel.
Also, if you just want to add one element to your list you can use cons, which adds the new element at the beginning of your list:

Channel[Your, Types](your, para, meters)::list

If you for some reason absolutely want to add the new element to the end of that list, you can use :+.

乙白 2024-12-25 19:23:25

要保持输入,您确实需要使用通配符_类型。这允许您以这样的方式定义列表:在将通道类型添加到列表时,您关心通道类型的 U 参数,而不是在将事件发送到列表中的所有通道时。下面的代码可以编译,但就目前情况而言,它是相当循环的。您需要一个通道的子类来执行除将其发送到另一个 EventSource 之外的其他操作。

class Occurrence[T]
class Channel[T, U](val endPoint : EventSource[U], val filter : Occurrence[T] => Boolean,
    val map : Occurrence[T] => Occurrence[U]) {
    def send(occurrence : Occurrence[T]) {
        if (filter(occurrence))
            endPoint.occur(map(occurrence))
    }
}
class EventSource[T] {
  var list: List[Channel[T,_]]  = Nil
  def addChannel[U]( endPoint : EventSource[U], filter : Occurrence[T] => Boolean, map : Occurrence[T] => Occurrence[U]) {
    list = list ++ List[Channel[T, U]](new Channel[T, U](endPoint, filter, map))
  }
  def occur( occurrence : Occurrence[T] ) {
    list foreach { _.send( occurrence ) }
  }
}

To maintain typing you really need to use the wild card _ type. This allows you to define list in such a way where you care about the U parameter of the Channel type when adding it to the list but not when sending an occurrence to all the channels in the list. The following compiles but as it stands it is rather circular. You need a sub class of channel that does something other than send it to another EventSource.

class Occurrence[T]
class Channel[T, U](val endPoint : EventSource[U], val filter : Occurrence[T] => Boolean,
    val map : Occurrence[T] => Occurrence[U]) {
    def send(occurrence : Occurrence[T]) {
        if (filter(occurrence))
            endPoint.occur(map(occurrence))
    }
}
class EventSource[T] {
  var list: List[Channel[T,_]]  = Nil
  def addChannel[U]( endPoint : EventSource[U], filter : Occurrence[T] => Boolean, map : Occurrence[T] => Occurrence[U]) {
    list = list ++ List[Channel[T, U]](new Channel[T, U](endPoint, filter, map))
  }
  def occur( occurrence : Occurrence[T] ) {
    list foreach { _.send( occurrence ) }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文