简体   繁体   中英

Lower bound and upper bound types for scala and dotty

Martin Odersky mentioned that they introduced compound and intersection types in dotty. On his slides there were two pictures representing the difference in upper and lower bounds for old and new type system.

This is the old one:

在此处输入图片说明

This is the new one:

在此处输入图片说明

Can someone explain what actually upper bound for T <: U means? Why it's infinite for current version of scala? How it would be differ for T >: U for example.

If T <: U (ie T is a subtype of U or U is a supertype of T ) then U is an upper bound for T and T is a lower bound for U .

In Scala 2

T with U <: T
T with U <: U
U with T <: T
U with T <: U

In Dotty

T & U =:= U & T
T | U =:= U | T
T & U <: T
T & U <: U
T <: T | U
U <: T | U

If T <: U then

T with U =:= T
T & U =:= T
T | U =:= U

If T >: U then U <: T and vice versa

T with U =:= U
T & U =:= U
T | U =:= T

In Scala 2 if T and U are from different inheritance hierarchies (or more precisely, subtyping hierarchies) then the best what can be said is

T <: Any
U <: Any

In this sense upper bound is infinite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM