def f(x: Type): result = match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): id(T), Type.pointer(Type.function(Type('float'), [T])): T, }) return result
def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): T, }, wrong_arg=True ) # error: Keyword arguments are not allowed in match
def f(x: Type): return match(x)( lambda T, U: { Type.pointer(Type.function(T, [U])): h(T)(Type('double')), Type.pointer(Type.function(Type('int'), [T])): T, Type.pointer(Type.function(Type('float'), [T])): T, })
def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), # note: A previous branch returning a Type was here. Type.pointer(Type.function(Type('int'), [T])): True, # error: All branches in a match\(\) must return the same type, but this branch returns a bool while a previous branch in this match expression returns a Type })
def test_type_function_pointer_literal_success(): from tmppy import Type assert Type.pointer( Type.function( Type('int'), [Type('float'), Type('double')])) == Type.pointer( Type.function(Type('int'), [Type('float'), Type('double')]))
def f(x: Type): return match(x)(lambda T, U: { # error: Malformed match\(\) Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): T, }, {})
def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): 1, Type.pointer(Type.function(Type('int'), [T])): 2, Type.pointer(Type.function(Type('float'), [T])): 3, })
def f(x: Type): return match(x)(lambda T: { T: T, Type.pointer(Type.function(Type('int'), empty_list(Type))): Type('bool'), })
def f(x: Type, y: Type): return match(x)(lambda T, U: { T: y, Type.pointer(Type.function(T, [U])): match(T, U)(lambda V: { (Type('int'), V): y, (Type('float'), V): Type('bool'), }), })
def test_match_with_int_expr_call(): from tmppy import Type, match def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): 1, Type.pointer(Type.function(Type('int'), [T])): 2, Type.pointer(Type.function(Type('float'), [T])): 3, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == 2
def test_match_with_equality_comparison_success(): from tmppy import Type, match def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double') == Type('int'), Type.pointer(Type.function(Type('int'), [T])): T == Type('int'), Type.pointer(Type.function(Type('float'), [T])): T == Type('int'), }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])))
def test_match_with_capture_success(): from tmppy import Type, match def f(x: Type, y: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): y, Type.pointer(Type.function(Type('float'), [T])): T, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])), Type('bool')) == Type('bool')
def test_match_in_assignment_success(): from tmppy import Type, match def f(x: Type): result = match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): Type('double'), Type.pointer(Type.function(Type('int'), [T])): T, Type.pointer(Type.function(Type('float'), [T])): T, }) return result assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
def test_nested_match_success(): from tmppy import Type, match def f(x: Type): return match(x)(lambda T, U: { T: Type('double'), Type.pointer(Type.function(T, [U])): match(T, U)(lambda V: { (Type('int'), V): V, (Type('float'), V): Type('bool'), }), }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
def test_nested_match_with_capture(): from tmppy import Type, match def f(x: Type, y: Type): return match(x)(lambda T, U: { T: y, Type.pointer(Type.function(T, [U])): match(T, U)(lambda V: { (Type('int'), V): y, (Type('float'), V): Type('bool'), }), }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')])), Type('bool')) == Type('bool')
def test_match_with_function_expr_call(): from tmppy import Type, match def g(x: Type): return x def h(x: Type): return g def f(x: Type): return match(x)(lambda T, U: { Type.pointer(Type.function(T, [U])): h(T)(Type('double')), Type.pointer(Type.function(Type('int'), [T])): T, Type.pointer(Type.function(Type('float'), [T])): T, }) assert f(Type.pointer(Type.function(Type('int'), [Type('int')]))) == Type('int')
def test_type_function_pointer_literal_with_no_args_success(): from tmppy import Type, empty_list assert Type.pointer(Type.function(Type('int'), empty_list(Type))) == Type.pointer( Type.function( Type('int'), empty_list(Type)))
def f(x: bool): return Type.function( Type('double'), Type('int') ) # error: The second argument passed to Type.function should have type List\[Type\], but was: Type
def f(x: bool): return Type.function( 1, [] ) # error: The first argument passed to Type.function should have type Type, but was: int
def f(x: bool): return Type.function( x=1 ) # error: Keyword arguments are not supported in Type.function\(\)
def f(x: bool): return Type.function( 'int', 'float', 'char') # error: Type.function\(\) takes 2 arguments. Got: 3