val foo: PartialFunction[String, Unit] = { case i: String => } foo: PartialFunction[String,Unit] =-6ren">
gpt4 book ai didi

scala - "String with Int"是什么意思?

转载 作者:行者123 更新时间:2023-12-04 23:20:59 24 4
gpt4 key购买 nike

> val foo: PartialFunction[String, Unit] = { case i: String => }
foo: PartialFunction[String,Unit] = <function1>

> val bar: PartialFunction[Int, Unit] = { case i: Int => }
bar: PartialFunction[Int,Unit] = <function1>

> foo orElse bar
PartialFunction[String with Int,Unit] = <function1>

什么是 String with Int? .我认为这甚至不可能。
> (foo orElse bar)(new String with Int)
error: illegal inheritance from final class String
(foo orElse bar)(new String with Int)
^
error: class Int needs to be a trait to be mixed in
(foo orElse bar)(new String with Int)
^

不应该是 PartialFunction[Nothing,Unit] ?

最佳答案

What is String with Int?



它是一种交叉类型。 IE。这种类型的值必须是 Int和一个 String同时。

I don't think that's even possible.



是的,这是一种无人居住的类型。但是,一般情况下,如果替换 IntString某些类型 AB , 你会得到 PartialFunction[A with B, Unit]并且编译器对此没有特殊情况。

关于scala - "String with Int"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com