Consider this case
T1 = &'a fn(&'b i32)->&'c i32
T2 = &'a fn(&'b1 i32)->&'c1 i32
If we say T1
is a subtype of T2
, what is the relationship between 'b
and b1
, and c
and c1
? Currently, I only have a slow way to compute the relationship. My thought process is:
&'a U
is a subtype of&'a U2
ifU
is a subtype ofU2
since&'a T
is covariant in itsT
- Hence,
fn(&'b i32)->&'c i32
must be a subtype offn(&'b1 i32)->&'c1 i32
- Since
fn(T)->U
is contravariant in itsT
and covariant in itsU
- Hence,
&'b i32
must be a supertype of&'b1 i32
- The above step means
'b1: 'b
&'c i32
must be a subtype of&'c1 i32
, which gets'c:'c1
Hence, when
'b1: 'b and 'c:'c1
T1
is a subtype of T2
. However, this way is too verbose and easy to be wrong. Is there a simple way to compute the relationship?
BTW, if 'b : 'b1
and c:'c1
, what is the variance relationship between fn(&'b i32)->&'c i32
and fn(&'b1 i32)->&'c1 i32
?