Esempio n. 1
0
    def instantiated_generic_structural_type_is_sub_type_of_other_instantiated_generic_structural_type_if_it_has_matching_attributes(self):
        iterator = types.generic_structural_type("iterator", [types.covariant("T")], lambda T: [
            types.attr("__iter__", types.func([], iterator(T))),
            types.attr("__next__", types.func([], T)),
        ])

        iterable = types.generic_structural_type("iterable", [types.covariant("T")], lambda T: [
            types.attr("__iter__", types.func([], iterator(T))),
        ])
        
        assert types.is_sub_type(
            iterable(types.int_type),
            iterator(types.int_type),
        )
Esempio n. 2
0
 def recursive_instantiated_generic_structural_type_is_sub_type_of_same_instantiated_generic_structural_type_if_it_has_matching_attributes(self):
     recursive = types.generic_structural_type("recursive", [types.covariant("T")], lambda T: [
         types.attr("__iter__", types.func([], recursive(T))),
     ])
     
     assert types.is_sub_type(
         recursive(types.int_type),
         recursive(types.int_type),
     )
Esempio n. 3
0
 def invariant_type_parameter_can_be_unified_when_part_of_recursive_structural_type(self):
     invariant_type_param = types.invariant("T")
     recursive = types.generic_structural_type("recursive", [types.covariant("T")], lambda T: [
         types.attr("__iter__", types.func([], recursive(T))),
     ])
     
     assert types.is_sub_type(
         recursive(invariant_type_param),
         recursive(types.int_type),
         unify=[invariant_type_param]
     )
Esempio n. 4
0
 def covariant_type_parameter_is_substituted_with_common_super_type_of_actual_type_params(self):
     covariant_type_param = types.covariant("T")
     first_class_type = types.class_type("User")
     second_class_type = types.class_type("Role")
     generic_class = types.generic_class("Pair", [covariant_type_param, covariant_type_param])
     
     type_map = types.is_sub_type(
         # TODO: need a reliable way of getting the underlying type (but as an instantiated type)
         generic_class(covariant_type_param, covariant_type_param),
         generic_class(first_class_type, second_class_type),
         unify=[covariant_type_param]
     )
     assert_equal(types.union(first_class_type, second_class_type), type_map[covariant_type_param])
Esempio n. 5
0
 def instantiated_class_is_sub_type_of_other_instantiated_class_if_formal_param_is_covariant_and_type_params_are_subtypes(self):
     generic_class = types.generic_class("iterator", [types.covariant("T")])
     
     assert types.is_sub_type(generic_class(types.object_type), generic_class(types.int_type))
     assert not types.is_sub_type(generic_class(types.int_type), generic_class(types.object_type))