codon/test/core/generics.codon

105 lines
2.5 KiB
Python
Raw Normal View History

2021-09-28 02:02:44 +08:00
class A[TA,TB,TC]:
a: TA
b: TB
c: TC
def dump(a, b, c):
print a, b, c
# non-generic method:
def m0(self: A[TA,TB,TC], a: int):
print a
# basic generics:
def m1[X](self: A[TA,TB,TC], other: A[X,X,X]):
print other.a, other.b, other.c
# non-generic method referencing outer generics:
def m2(a: TA, b: TB, c: TC):
A.dump(a, b, c)
# generic args:
def m3(self, other):
return self.a
# lots of nesting:
def m4[TD](self: A[TA,TB,TC], d: TD):
def m5[TA,TB,TC,TD,TE](a: TA, b: TB, c: TC, d: TD, e: TE):
print a, b, c, d, e
m5(self.a, self.b, self.c, d, d)
# instantiating the type:
def m5(self):
x = A(self.a, self.b, self.c)
A.dump(x.a, x.b, x.c)
# deeply nested generic type:
def m6[T](v: array[array[array[T]]]):
return v[0][0][0]
# explicit realization:
Typechecker refactoring (#20) * Initial refactor commit * Support external vars * Simplify refactor; Python scoping [wip] * Python scoping [wip] * Python scoping [fix loops; wip] * Fix lambdas * Python scoping [test fixes; wip] * Fix scoping [wip] * Fix basic tests [no-ci] * Fix tests * CallExpr refactoring [wip] * CallExpr refactoring [wip] * Remove activeUnbounds tracking * Add core.codon * Move Function and other core types to core.codon; Revamp Function and Callable types * Refactor IntExpr, FloatExpr and CallExpr * Refactor ClassStmt * Refactor context, IdExpr and DotExpr * Refactor DotExpr and AssignStmt * Refactor ImportStmt * Refactor FunctionStmt * Refactor * Remove UpdateStmt * Refactor AssignReplacementVisitor * Make SimplifyVisitor in-place * Fix new scoping * Fix import type alias handling * Add docstrings; Complete Simplify refactoring * Fixes for seqtest * Refactor typecheck [wip] * Refactor typecheck [wip] * Refactor typecheck/access; Remove void anduse NoneType; Fix #18 * Refactor typecheck/assign * clang-format and cmake-format * Fix none types in IR * Multi-error support in simplify * Fix IR tests for new void * Simplify ClassStmt * Refactor cond.cpp * Refactor error.cpp * Refactor function.cpp and simplify unbounds * Refactor op.cpp * Refactor call.cpp [wip] [no-ci] * seqassertn updates [noci] * Refactor call.cpp * Refactor call.cpp * Refactor call.cpp * Refactor typecheck * clang-tidy updates [noci] * Refactor infer.cpp [wip] * Refactor infer.cpp * Refactor wrapExpr * Remove visitedAsts * Remove old base logic * Refactor typecheck ctx * Fix JIT bug * Fix JIT tests * Scoping fixes [wip] [noci] * Fix ImperativeForFlow var store * Add newlines [noci] * Dump IR module with log flag * Fix scoping bugs; Add &, ^ and | static operations; Address stylistic review issues * Fix side effect analysis for for-loops * Add support for class variables and ClassVar * Refactor special dot-member cases * Add codon app tests * Fix class variables; clang-tidy * Fix __argv__ * Add datetime constants and update tests * Fix #25; Add Py_None, Py_True and Py_False; External var support [wip] * External var support [wip] * Dump LLVM IR when debug flags are active * clang-format * Fix arg var construction * Extern var fixes * Undo extern var changes related to stdout etc. * Fix tuple magics * Fix extern vars and tuple magics * Fix duplicate var name error * Fix extern vars * Fix #16 * Fix side-effect analysis for try-catch * Move test C var to test executable * Add staticmethod * Fix var status for try-catch * Fix tests * Fix shell var name * Fix test * Fix app test * Fix scoping issue (remove dominated identifier from stack) * Fix no-pie issue * Use PIC when building library object * Don't use -no-pie when building library [noci] * Use -relocation-model=pic in test * Fix lib build on Linux * Fix lib build * Update exceptions to use subclasses vs. header * Fix __repr__ * Fix tests * Fix exceptions test * Don't build docs Co-authored-by: A. R. Shajii <ars@ars.me>
2022-07-27 04:06:00 +08:00
def m7(T: type, S: type):
2021-09-28 02:02:44 +08:00
print "works"
class B1[T]:
a: T
def foo[S](self: S) -> B1[int]:
return B1[int](111)
class B2[T]:
a: T
def foo[S](self: B2[S]):
return B2[int](222)
a1 = A(42, 3.14, "hello")
a2 = A(1, 2, 3)
b1 = B1[bool](True).foo()
b2 = B2[str]("x").foo()
v1 = array[array[array[str]]](1)
v2 = array[array[str]](1)
v3 = array[str](1)
v1[0] = v2
v2[0] = v3
v3[0] = "world"
f = a2.m0
a1.m1(a2) # EXPECT: 1 2 3
A[int,float,str].m2(1, 1.0, "one") # EXPECT: 1 1 one
A[int,int,int].m2(11, 22, 33) # EXPECT: 11 22 33
print a1.m3(a2) # EXPECT: 42
print a1.m3(a2) # EXPECT: 42
print a2.m3(a1) # EXPECT: 1
a1.m4(True) # EXPECT: 42 3.14 hello True True
a1.m4([1]) # EXPECT: 42 3.14 hello [1] [1]
a2.m4("x") # EXPECT: 1 2 3 x x
a1.m5() # EXPECT: 42 3.14 hello
a2.m5() # EXPECT: 1 2 3
print A.m6(v1) # EXPECT: world
m7(str,float) # EXPECT: works
m7(str,float) # EXPECT: works
m7(float,str) # EXPECT: works
f(99) # EXPECT: 99
print b1.foo().a # EXPECT: 111
print b2.foo().a # EXPECT: 222
# recursive generics with different inner type parameter
def foo(a):
if not a:
foo(True)
print a
# EXPECT: True
# EXPECT: 0
foo(0)
def bar(a):
def baz(x):
if not x:
bar(True)
print(x)
baz(a)
# EXPECT: True
# EXPECT: 0
bar(0)