在 Scala 中重载通用事件处理程序

编程入门 行业动态 更新时间:2024-10-26 22:22:46
本文介绍了在 Scala 中重载通用事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我定义以下通用事件处理程序

If I define the following generic event handler

trait Handles[E <: Event] { def handle(event: E) }

事件类型是这样的

trait Event { } class InventoryItemDeactivated(val id: UUID) extends Event; class InventoryItemCreated(val id: UUID, val name: String) extends Event;

然后我如何创建一个为每个这些事件实现事件处理程序的类?.我试过了:

how do I then make a single class that implements event handlers for each of these events ?. I tried:

class InventoryListView extends Handles[InventoryItemCreated] with Handles[InventoryItemDeactivated] { def handle(event: InventoryItemCreated) = { } def handle(event: InventoryItemDeactivated) = { } }

但是 Scala 抱怨一个特性不能被继承两次.

but Scala complains that a trait cannot be inherited twice.

我发现这个 answer 暗示了一个解决方案,但它似乎需要多个类(每个处理程序一个).这真的是唯一的方法,还是有一些其他的 Scala 构造我可以用来使单个类实现多个通用事件处理程序(即使用案例类、清单或其他一些花哨的构造)?.

I found this answer which hints at a solution, but it seams to require multiple classes (one for each handler). Is this really the only way or is there then some other Scala construct I can use to make a single class implement multiple generic event handlers (i.e. using case classes, manifests or some other fancy construct) ?.

推荐答案

我不知道有什么方法可以在一个类中完成(除非将 Event 设为 ADT 并定义句柄以接受Event 类型的参数.但这会带走您似乎正在寻找的类型安全性).

I don't know of a way to do it in one class (except by making Event an ADT and defining handle to accept a parameter of type Event. But that would take away the kind of typesafety you seem to be looking for).

我建议改用类型类模式.

I'd suggest using type-class pattern instead.

trait Handles[-A, -E <: Event] { def handle(a: A, event: E) } trait Event { ... } class InventoryItemDeactivation(val id: UUID) extends Event class InventoryItemCreation(val id: UUID, val name: String) extends Event class InventoryListView { ... } implicit object InventoryListViewHandlesItemCreation extends Handles[InventoryListView, InventoryItemCreation] = { def handle(v: InventoryListView, e: InventoryItemCreation) = { ... } } implicit object InventoryListViewHandlesItemDeactivation extends Handles[InventoryListView, InventoryItemDeactivation] = { def handle(v: InventoryListView, e: InventoryItemDeactivation) = { ... } } def someMethod[A, E <: Event](a: A, e: E) (implicit ev: InventoryListView Handles InventoryItemCreation) = { ev.handle(a, e) ... }

更多推荐

在 Scala 中重载通用事件处理程序

本文发布于:2023-11-09 15:05:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   程序   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!