There seems to be a somewhat common concept according to which the Scala types form a lattice .
I can get three quarters of the way there and define semilattice by specifying top = Any , bottom = Nothing and meet = with . Then โค is at least basically equivalent to <: i.e. Subtyping ratio.
Scala is kind enough to help us test at least some of the laws of the lattice:
scala> implicitly[Nothing =:= Nothing] res0: =:=[Nothing,Nothing] = <function1> scala> implicitly[Nothing =:= String with Nothing] res1: =:=[Nothing,String with Nothing] = <function1> scala> implicitly[Nothing =:= String with Any] <console>:11: error: Cannot prove that Nothing =:= String with Any. implicitly[Nothing =:= String with Any] ^ scala> implicitly[Any =:= String with Any] <console>:11: error: Cannot prove that Any =:= String with Any. implicitly[Any =:= String with Any] ^ scala> implicitly[Any =:= Any] res4: =:=[Any,Any] = <function1> scala> implicitly[Any =:= Any with String] <console>:11: error: Cannot prove that Any =:= Any with String. implicitly[Any =:= Any with String] ^ scala> implicitly[String =:= Any with String] res6: =:=[String,Any with String] = <function1> scala> implicitly[String =:= String with Any] res7: =:=[String,String with Any] = <function1> scala> implicitly[String =:= (String with Any)] res8: =:=[String,String with Any] = <function1> scala> implicitly[Int with String =:= String with Int] res9: =:=[Int with String,String with Int] = <function1> scala> implicitly[Boolean with (Int with String) =:= (Boolean with Int) with (Boolean with String)] res11: =:=[Boolean with Int with String,Boolean with Int with Boolean with String] = <function1>
So now all that is missing is a suitable definition of a union. Is there a union equivalent in Scala?
You do not need to use my definitions above, above and below, but this is preferable, since it is more than just a theoretical question. I really want to use a union of two types. As a workaround now, since I donโt know how to write A join B , I will define a new trait with abstract methods for methods in both A and B , and modify A and B to inherit from it.
source share