Skip to content

Commit e745696

Browse files
committed
[Tolk] Type aliases type NewName = <existing type>
An alias creates a new name for an existing type but remains interchangeable with it: > type UserId = int32; > type MaybeOwnerHash = bytes32?; > type MyStorage = (int, int, slice, cell); Aliases never occur at runtime, they are compile-time only (they exist as types until IR generation). That's why lots of code comparing types use `type->unwrap_alias()` or `try_as<TypeDataAlias>`. In stdlib, a special alias was introduced: > type dict = cell?; Functions working with dictionaries use `dict`, improving code clarity.
1 parent 303e92b commit e745696

39 files changed

+909
-165
lines changed

crypto/smartcont/tolk-stdlib/common.tolk

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
// More specific functions are required to be imported explicitly, like "@stdlib/tvm-dicts".
44
tolk 0.10
55

6+
/// In Tolk v1.x there would be a type `map<K,V>`.
7+
/// Currently, working with dictionaries is still low-level, with raw cells.
8+
/// But just for clarity, we use "dict" instead of a "cell?" where a cell-dictionary is assumed.
9+
/// Every dictionary object can be null. TVM NULL is essentially "empty dictionary".
10+
type dict = cell?;
11+
612
/**
713
Tuple manipulation primitives.
814
Elements of a tuple can be of arbitrary type.
@@ -144,9 +150,9 @@ fun getMyOriginalBalance(): int
144150

145151
/// Same as [getMyOriginalBalance], but returns a tuple:
146152
/// `int` — balance in nanotoncoins;
147-
/// `cell` — a dictionary with 32-bit keys representing the balance of "extra currencies".
153+
/// `dict` — a dictionary with 32-bit keys representing the balance of "extra currencies".
148154
@pure
149-
fun getMyOriginalBalanceWithExtraCurrencies(): [int, cell?]
155+
fun getMyOriginalBalanceWithExtraCurrencies(): [int, dict]
150156
asm "BALANCE";
151157

152158
/// Returns the logical time of the current transaction.
@@ -476,12 +482,12 @@ fun getLastBits(self: slice, len: int): slice
476482
/// Loads a dictionary (TL HashMapE structure, represented as TVM cell) from a slice.
477483
/// Returns `null` if `nothing` constructor is used.
478484
@pure
479-
fun loadDict(mutate self: slice): cell?
485+
fun loadDict(mutate self: slice): dict
480486
asm( -> 1 0) "LDDICT";
481487

482488
/// Preloads a dictionary (cell) from a slice.
483489
@pure
484-
fun preloadDict(self: slice): cell?
490+
fun preloadDict(self: slice): dict
485491
asm "PLDDICT";
486492

487493
/// Loads a dictionary as [loadDict], but returns only the remainder of the slice.
@@ -556,7 +562,7 @@ fun storeBool(mutate self: builder, x: bool): self
556562
/// Stores dictionary (represented by TVM `cell` or `null`) into a builder.
557563
/// In other words, stores a `1`-bit and a reference to [c] if [c] is not `null` and `0`-bit otherwise.
558564
@pure
559-
fun storeDict(mutate self: builder, c: cell?): self
565+
fun storeDict(mutate self: builder, c: dict): self
560566
asm(c self) "STDICT";
561567

562568
/// Stores (Maybe ^Cell) into a builder.
@@ -720,7 +726,7 @@ fun reserveToncoinsOnBalance(nanoTonCoins: int, reserveMode: int): void
720726

721727
/// Similar to [reserveToncoinsOnBalance], but also accepts a dictionary extraAmount (represented by a cell or null)
722728
/// with extra currencies. In this way currencies other than Toncoin can be reserved.
723-
fun reserveExtraCurrenciesOnBalance(nanoTonCoins: int, extraAmount: cell?, reserveMode: int): void
729+
fun reserveExtraCurrenciesOnBalance(nanoTonCoins: int, extraAmount: dict, reserveMode: int): void
724730
asm "RAWRESERVEX";
725731

726732

0 commit comments

Comments
 (0)