mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 16:54:25 +00:00
generics (in the compiler written in Finch)
fn max[T](T a, T b) -> T struct Stack[T] { []T items } fn Stack[T].push(T x)
The type is worked out from the arguments (max(3, 4), first([1, 2])) and written out
where it cannot be: max[float](1, 2.5), Stack[int](), Pair[str, int](...). Each use makes
its own copy, so a generic costs nothing at run time. Several parameters
(Pair[A, B]), nesting (Stack[Pair[int, str]]), methods, and printing (Stack[int](items: ...))
work. An error inside a generic says which version it was building.
boot.sh: 37 passed, 0 failed (stage 2 and 3 identical).
This commit is contained in:
parent
609ceffe58
commit
04eb0cce55
10 changed files with 575 additions and 7 deletions
10
tests/new/generics.out
Normal file
10
tests/new/generics.out
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
9 2.5 pear b
|
||||
2.5
|
||||
x 7 3 1
|
||||
<5> <hi> <Point(x: 1, y: 2)> <[1, 2]>
|
||||
["ab", "ab", "ab"] [1.5, 1.5] [7, 7]
|
||||
20 1 Stack[int](items: [10])
|
||||
Stack[str](items: ["ann", "bob"]) 2
|
||||
1
|
||||
Pair[str, int](first: "age", second: 31) Pair[int, str](first: 31, second: "age") 32
|
||||
Stack[Pair[int, str]](items: [Pair[int, str](first: 1, second: "one")])
|
||||
Loading…
Reference in a new issue