Function ezno_checker::types::subtyping::type_is_subtype
source · pub fn type_is_subtype(
base_type: TypeId,
ty: TypeId,
state: &mut State<'_>,
environment: &Environment<'_>,
types: &TypeStore
) -> SubTypeResult
Expand description
Checks whether ty
is a subtype of the base_type
- equivalently …
base_type :>= ty
(ty <=: base_type
) - equivalently … whether
ty
could be substituted asbase_type
. - equivalently … whether
ty
’s properties imply the existence ofbase_type
properties.
Examples found in repository?
examples/calculate_subtypes.rs (line 99)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
fn contributions(environment: &mut Environment, types: &mut TypeStore) {
// TODO types API, which doesn't is less hand-holdy
let generic_parameter =
environment.new_explicit_type_parameter("T", Some(TypeId::NUMBER_TYPE), None, types);
// create `{}` and add `inner: T`
let object = types.new_anonymous_interface_type();
let inner = PropertyKey::String(std::borrow::Cow::Owned("inner".to_owned()));
environment.info.register_property(
object,
Publicity::Public,
inner.clone(),
PropertyValue::Value(generic_parameter.type_id),
false,
source_map::SpanWithSource::NULL,
);
let or = types.new_or_type(generic_parameter.type_id, object);
let parameter = types.new_function_parameter(or);
let five = types.new_constant_type(Constant::Number(5f64.try_into().unwrap()));
let five_obj = {
let mut basis = ObjectBuilder::new(
None,
types,
source_map::SpanWithSource::NULL,
&mut environment.info,
);
basis.append(
environment,
Publicity::Public,
inner,
PropertyValue::Value(five),
source_map::SpanWithSource::NULL,
);
basis.build_object()
};
// x(5)
{
let contributions = Contributions::default();
let mut state = State {
already_checked: Default::default(),
mode: Default::default(),
contributions: Some(contributions),
others: SubTypingOptions::default(),
object_constraints: None,
};
let result = type_is_subtype(parameter, five_obj, &mut state, &environment, &types);
let contributions = state.contributions.as_ref().unwrap();
eprintln!(
"{:?}:>{:?}
result={:?}
staging_covariant={:?}
staging_contravariant={:?}",
parameter,
five,
result,
contributions.staging_covariant,
contributions.staging_contravariant
);
}
}