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

标签 scala types type-inference

> 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/

相关文章:

scala - Lift Web 框架的开源应用程序

scala - 如何测试向另一个 Actor 发送消息的 Akka Actor ?

sql - PostgreSQL 中的派生类型

postgresql - 使用Spark JdbcRDD读取PostgreSQL表出错

scala - 在 Scala 中管理灵活的、类型化的、不可变的数据结构的正确方法是什么?

c# - 我能以某种方式整理这个(过度使用吗?)泛型?

c++ - 在 C++ 中将非 const 转换为 const

java - 泛型类型推断不适用于方法链接?

c++ - 为 RAII 模板类编写对象生成器的更好方法?

typescript - 使用参数或泛型来约束另一个参数的类型?