def unpack_tuple_of_tuples(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [*Ts]), Type.template_instantiation('std::tuple', [Type('int'), *Ts])]): Ts, })
def unpack_tuple_of_tuples(t: Type): return match(t)(lambda Ts, Us: { Type.template_instantiation('std::tuple', [ Type.template_instantiation('std::tuple', [*Ts]), Type.template_instantiation('std::tuple', [*Us]) ]): [Ts, Us] })
def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [*X]): X, Type.template_instantiation('std::tuple', [Type('int'), X]): [X], })
def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [*X]), # note: A previous match as a List\[Type\] was here Type.template_instantiation('std::tuple', [X])]): # error: Can't match X as a Type because it was already used to match a List\[Type\] Type('int'), })
def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [X]), # note: A previous match as a Type was here Type.template_instantiation('std::tuple', [*X])]): # error: List extraction can't be used on X because it was already used to match a Type Type('int'), })
def test_match_expr_extract_list_also_used_as_type_after_different_branch_ok(): from tmppy import Type, match def f(t: Type): return match(t)(lambda X: { Type.template_instantiation('std::tuple', [*X]): X, Type.template_instantiation('std::tuple', [Type('int'), X]): [X], }) assert f(Type.template_instantiation('std::tuple', [Type('int'), Type('double')])) == [Type('double')] assert f(Type.template_instantiation('std::tuple', [Type('double'), Type('int')])) == [Type('double'), Type('int')]
def test_match_expr_extract_list_multiple_times_matched_without_default_case(): from tmppy import Type, match def unpack_tuple_of_tuples(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [*Ts]), Type.template_instantiation('std::tuple', [Type('int'), *Ts])]): Ts, }) assert unpack_tuple_of_tuples(Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [Type('float'), Type('double')]), Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double')])])) \ == [Type('float'), Type('double')]
def test_match_expr_extract_multiple_lists(): from tmppy import Type, match def unpack_tuple_of_tuples(t: Type): return match(t)(lambda Ts, Us: { Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [*Ts]), Type.template_instantiation('std::tuple', [*Us])]): [Ts, Us] }) assert unpack_tuple_of_tuples(Type.template_instantiation('std::tuple', [Type.template_instantiation('std::tuple', [Type('int'), Type('float')]), Type.template_instantiation('std::tuple', [Type('double')])])) \ == [[Type('int'), Type('float')], [Type('double')]]
def f(t: Type): return match(t)( lambda T: { Type.template_instantiation('std::tuple', [T, Type('float')]): T, T: Type('double'), })
def unpack_tuple(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [*Ts]): match(Type('int'))(lambda T: { T: Ts }) })
def test_match_expr_extract_list_empty(): from tmppy import Type, match, empty_list def unpack_tuple(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [*Ts]): Ts }) assert unpack_tuple(Type.template_instantiation('std::tuple', empty_list(Type))) \ == empty_list(Type)
def test_match_expr_extract_list_optimization(): from tmppy import Type, match def _unpack_tuple(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [*Ts]): Ts }) assert _unpack_tuple(Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double')])) \ == [Type('int'), Type('float'), Type('double')]
def test_match_expr_extract_list_and_front_elements(): from tmppy import Type, match def unpack_tuple(t: Type): return match(t)(lambda FirstT, SecondT, Ts: { Type.template_instantiation('std::tuple', [FirstT, SecondT, *Ts]): [[FirstT, SecondT], Ts] }) assert unpack_tuple(Type.template_instantiation('std::tuple', [Type('int'), Type('float'), Type('double'), Type('char')])) \ == [[Type('int'), Type('float')], [Type('double'), Type('char')]]
def test_template_instantiation_type_expr_as_match_expr_not_matched_success(): from tmppy import Type, match def f(t: Type): return match(t)(lambda T: { Type.template_instantiation('std::tuple', [T, Type('float')]): T, T: Type('double'), }) assert f(Type.template_instantiation('std::tuple', [Type('int'), Type('void')])) == Type('double')
def test_attribute_access_on_function_result_expr_success(): from tmppy import Type def f(x: Type): return x assert f( Type.template_instantiation( 'std::remove_pointer', [Type.pointer(Type('int'))])).type == Type('int')
def _unpack_tuple(t: Type): return match(t)(lambda Ts: { Type.template_instantiation('std::tuple', [*Ts]): Ts })
def unpack_tuple(t: Type): return match(t)( lambda Ts, LastT: { Type.template_instantiation('std::tuple', [*Ts, LastT]): # error: List extraction is only allowed at the end of the list [Ts, [LastT]] })
def f(x: bool): y = Type.template_instantiation('std::remove_pointer', [Type.pointer(Type('int'))]) assert y.type == Type('int') return True
def f(x: bool): return Type.template_instantiation( 'std::vector', Type('int') ) # error: The second argument passed to Type.template_instantiation should have type List\[Type\], but was: Type
def test_attribute_access_success(): from tmppy import Type assert Type.template_instantiation( 'std::remove_pointer', [Type.pointer(Type('int'))]).type == Type('int')
def f(x: bool): return Type.template_instantiation( 'F<X, Y>', Type('int'), Type('float') ) # error: Type.template_instantiation\(\) takes 2 arguments. Got: 3
def f(x: bool): return Type.template_instantiation( 1, # error: The first argument passed to Type.template_instantiation should be a string [Type('int')])
def f(x: bool): return Type.template_instantiation( 'int' ) # error: Type.template_instantiation\(\) takes 2 arguments. Got: 1
def f(x: bool): return Type.template_instantiation( template_atomic_type= 'std::dummy<int>::add_pointer', # error: Keyword arguments are not supported in Type.template_instantiation\(\) args=[Type('int')])
def f(x: bool): return Type.template_instantiation( 'std::dummy<int>::add_pointer', [Type('int')] ) # error: Invalid atomic type. Atomic types should be C\+\+ identifiers \(possibly namespace-qualified\).
def test_template_instantiation_literal_success(): from tmppy import Type assert Type.pointer(Type('int')) == Type.template_instantiation( 'std::add_pointer', [Type('int')]).type
def unpack_tuple(t: Type): return match(t)(lambda FirstT, SecondT, Ts: { Type.template_instantiation('std::tuple', [FirstT, SecondT, *Ts]): [[FirstT, SecondT], Ts] })