Merge: typing: add services for intersecting and subtracting types
authorJean Privat <jean@pryen.org>
Mon, 20 Mar 2017 20:09:01 +0000 (16:09 -0400)
committerJean Privat <jean@pryen.org>
Mon, 20 Mar 2017 20:09:01 +0000 (16:09 -0400)
commit035bc86b157c73e974713808d206eae7209881ac
tree487195f741b7a35f524c16e0c085312e7341721e
parenta89f0533da07ab11e6518d5bb391b639c4bde748
parent0f51a321cc983d9a54afa48058f2ebd8da81d059
Merge: typing: add services for intersecting and subtracting types

Nit does not have intersection nor union types, however, the intersection and the difference operations on types are needed for a better behavior of the adaptive typing system.

Currently, the only need of type intersection and type difference is with `isa`. The previous implementation did not really implement them and only did the following:

~~~nit
var x: X
...
if x isa Y then
   # if X<:Y then x is still a X (and you get a warning) else x is now a Y
   ...
else
   # nothing never change here. x is a X
   ...
end
~~~

Now, the `then` branch, handle the merge of nullable types and the `else` branch might also be adapted.

~~~nit
var x: nullable X # where Y <: X
...
if x isa Object then
   # x is a X (instead of Object as before)
else
  #  x is null (instead of nullable X as before)
end

if x isa nullable Object then # warning, useless isa
   # x is a nullable X (as before)
else
   # x is dead (instead of nullable X as before)
end

if x isa Y then
   # x is a Y (same as before)
else
   # x is a nullable X (same as before)
end

if x isa nullable Y then
   # x is a nullable Y (as before)
else
   # x is a X without nullable (instead of nullable X as before)
end
~~~

I do not expect that these change improve that much the expressiveness of the language. However, it makes the type system a little more consistent and a little less hackish.

Pull-Request: #2385
Reviewed-by: Jean-Christophe Beaupré <jcbrinfo.public@gmail.com>