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

What does "String with Int" mean?

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

What it is String with Int?. I do not think this is even possible.

> (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)
                                   ^

Shouldn't it be PartialFunction[Nothing,Unit]?

+4
source share
2 answers

What is String with Int?

This is a type of intersection. That is, the value of this type must be at the same time Inta String.

I do not think this is even possible.

Yes, this is a desert type. However, in general, if you change Intand Stringsome types Aand Byou will receive PartialFunction[A with B, Unit], and the compiler for this is not a special case.

+7
source

, , , (, ), :

class B
class C extends B
class D extends C

val bf: PartialFunction[B, Unit] = {case b: B => println("some b") }
val cf: PartialFunction[C, Unit] = {case c: C => println("some c") }
val g = bf orElse cf

g(new D) // some b

, . :

http://www.scala-lang.org/old/node/110

http://www.scala-lang.org/files/archive/spec/2.11/03-types.html#compound-types

+1

All Articles