🫧 Tolk v0.12: structures, generics, and methods
Finally! Or maybe
not yet?
This update brings Tolk one step closer to its final form by introducing:
✅ Notable changes in Tolk v0.12:
1. Structures
2. Generics
3. Methods
4. Stdlib with short naming
5. Fift output enhancements
PR on GitHub with detailed info.
✔ Structures
Looks like TypeScript — but works in TVM!
struct Point {
x: int;
y: int;
}
fun calcMaxCoord(p: Point) {
return p.x > p.y ? p.x : p.y;
}
// declared like a JS object
var p: Point = { x: 10, y: 20 };
// called like a JS object
calcMaxCoord({ x: 10, y: 20 });
Point a just
named tensor — identical to
(int, int) at the TVM level.
Key features:
- Compiler guesses what you mean — automatically:
fun loadData(): StoredInfo {
return {
counterValue: ...,
ownerAddress: ...,
}
}
- Works with
shorthand syntax { x, y }
-
Default values for fields are supported
-
Nested objects are supported
-
No overhead for single-field structs over a plain value
- Nullability, smart casts, union types,
all language features — everything works
✔ Generics
They exist only at the type level (no runtime cost):
struct Nullable<T> {
item: T? = null;
}
The type system becomes powerful enough to express complex scenarios:
struct Ok<TResult> { result: TResult }
struct Err<TError> { err: TError }
type Response<R, E> = Ok<R> | Err<E>;
match (r) {
Ok => { r.result }
Err => { r.err }
}
✔ Methods — for any types
For structures:
fun Point.getX(self) {
return self.x
}
fun Point.create(x: int, y: int): Point {
return { x, y }
}
Or extend built-in types:
fun slice.load32(mutate self): int32 {
return self.loadInt(32);
}
Or — tensors, unions, generics, even "any receiver". A method is just an extension function with predictable resolution rules.
✔ stdlib — with short methods naming
// before
someCell.cellHash();
someTuple.tupleSize();
someBuilder.getBuilderBitsCount();
// now
someCell.hash();
someTuple.size();
someBuilder.bitsCount();
Even more — some global functions are now static methods:
// before
getMyAddress();
setContractData(c);
getLogicalTime();
// now
contract.getAddress();
contract.setData(c);
blockchain.logicalTime();
It looks pretty, and since IDE suggest only available methods after dot, it's extremely useful. For a full list of renamings, consider
documentation.
✔ A long-awaited bonus for Fift ninja
Look
at the PR and enjoy how readable Fift assembler output is. Yes, it contains original .tolk lines as comments! And yes, it works perfectly.
🌳 You now see how Tolk's features are aligning together towards automatic serialization, fully described by the type system. Cell references, constructor prefixes, message sending, and other use cases will be expressed with the features above.
Обсуждение 0
Обсуждение не доступно в веб-версии. Чтобы написать комментарий, перейдите в приложение Telegram.
Обсудить в Telegram