从任意类型的联合中删除一种类型的类型级别
如果我在Scala 3中有一个任意类型的联盟,是否可以编写一种从联盟中“删除”一种类型的方法?
类似于shapeless.opp.coduct.remove
,但是对于本机scala 3
。特定错误类型,并将其余错误作为新的联合类型。
val result: Either[Foo | Bar | Baz | Bang, Thing]
val otherResult: Either[Foo | Bar, OtherThing]
// pretend syntax
def recoverBar[X, A](error: Bar | ...X)(f: Bar => A): Either[X, A] =
error match {
case e: Bar => Right(f(e))
case otherError => Left(otherError)
}
// example usage
val recoveredResult: Either[Foo | Baz | Bang, Option[Thing]] = result
.map { Option.apply }
.left.flatMap { recoverBar(_)(_ => None) }
val recoveredOther: Either[Foo, OtherThing] = otherResult
.left.flatMap { recoverBar(_)(_ => OtherThing.default) }
即某种类型的通用方法
[Foo | Bar | Baz | Bang] =>> [Foo | Baz | Bang]
[Foo | Bar] =>> [Foo]
[Bar] =>> [Nothing]
If I have an arbitrary type union in Scala 3, is it possible to write a method that "removes" one type from the union?
Similar to shapeless.ops.coproduct.Remove
, but for native Scala 3.
For example, if I have a union type representing a handful of different errors, and I want to write a function that recovers from one specific error type and leave the remaining errors as a new union type.
val result: Either[Foo | Bar | Baz | Bang, Thing]
val otherResult: Either[Foo | Bar, OtherThing]
// pretend syntax
def recoverBar[X, A](error: Bar | ...X)(f: Bar => A): Either[X, A] =
error match {
case e: Bar => Right(f(e))
case otherError => Left(otherError)
}
// example usage
val recoveredResult: Either[Foo | Baz | Bang, Option[Thing]] = result
.map { Option.apply }
.left.flatMap { recoverBar(_)(_ => None) }
val recoveredOther: Either[Foo, OtherThing] = otherResult
.left.flatMap { recoverBar(_)(_ => OtherThing.default) }
I.e. some kind of type-level generic way to do
[Foo | Bar | Baz | Bang] =>> [Foo | Baz | Bang]
[Foo | Bar] =>> [Foo]
[Bar] =>> [Nothing]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
typeTest
:匹配无法推断另一种情况必须是
b
,但我认为这是我们可以做的最好的,这很糟糕。目前。You can do it with a
TypeTest
:It kinda sucks that the match is unable to infer that the other case must be a
B
but I think this is the best we can do for now.