mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner. This is a common approach for functional languages, where types are inferred from usage through unification. As a result, type declarations are not necessary: () f(a,b) { return a+b; } // a and b now int, since `+` (int, int) While this approach works for now, problems arise with the introduction of new types like bool, where `!x` must handle both int and bool. It will also become incompatible with int32 and other strict integers. This will clash with structure methods, struggle with proper generics, and become entirely impractical for union types. This PR completely rewrites the type system targeting the future. 1) type of any expression is inferred and never changed 2) this is available because dependent expressions already inferred 3) forall completely removed, generic functions introduced (they work like template functions actually, instantiated while inferring) 4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)` 5) `as` keyword, for example `t.tupleAt(0) as int` 6) methods binding is done along with type inferring, not before ("before", as worked previously, was always a wrong approach)
This commit is contained in:
parent
3540424aa1
commit
799e2d1265
101 changed files with 5402 additions and 2713 deletions
|
@ -17,17 +17,17 @@ fun createEmptyTuple(): tuple
|
|||
/// Appends a value to tuple, resulting in `Tuple t' = (x1, ..., xn, value)`.
|
||||
/// If its size exceeds 255, throws a type check exception.
|
||||
@pure
|
||||
fun tuplePush<X>(mutate self: tuple, value: X): void
|
||||
fun tuplePush<T>(mutate self: tuple, value: T): void
|
||||
asm "TPUSH";
|
||||
|
||||
/// Returns the first element of a non-empty tuple.
|
||||
@pure
|
||||
fun tupleFirst<X>(t: tuple): X
|
||||
fun tupleFirst<T>(t: tuple): T
|
||||
asm "FIRST";
|
||||
|
||||
/// Returns the [`index`]-th element of a tuple.
|
||||
@pure
|
||||
fun tupleAt<X>(t: tuple, index: int): X
|
||||
fun tupleAt<T>(t: tuple, index: int): T
|
||||
builtin;
|
||||
|
||||
/// Returns the size of a tuple (elements count in it).
|
||||
|
@ -37,7 +37,7 @@ fun tupleSize(t: tuple): int
|
|||
|
||||
/// Returns the last element of a non-empty tuple.
|
||||
@pure
|
||||
fun tupleLast(t: tuple): int
|
||||
fun tupleLast<T>(t: tuple): T
|
||||
asm "LAST";
|
||||
|
||||
|
||||
|
@ -306,11 +306,11 @@ fun getBuilderDepth(b: builder): int
|
|||
*/
|
||||
|
||||
/// Dump a variable [x] to the debug log.
|
||||
fun debugPrint<X>(x: X): void
|
||||
fun debugPrint<T>(x: T): void
|
||||
builtin;
|
||||
|
||||
/// Dump a string [x] to the debug log.
|
||||
fun debugPrintString<X>(x: X): void
|
||||
fun debugPrintString<T>(x: T): void
|
||||
builtin;
|
||||
|
||||
/// Dumps the stack (at most the top 255 values) and shows the total stack depth.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue