From 22e9e305fccbcf26007e867a70a31fcc78c10b23 Mon Sep 17 00:00:00 2001
From: Novus Nota <68142933+novusnota@users.noreply.github.com>
Date: Tue, 12 Mar 2024 19:26:30 +0100
Subject: [PATCH] feat: Struct & Message declaration syntax + Optionals (#95)
* added motivation to use custom numeric ids to match opcodes
---
pages/book/_meta.js | 5 +-
pages/book/defining-types.mdx | 148 ++++++++++++++++++++++++++++++++++
pages/book/types.mdx | 77 ++++++++++--------
3 files changed, 193 insertions(+), 37 deletions(-)
create mode 100644 pages/book/defining-types.mdx
diff --git a/pages/book/_meta.js b/pages/book/_meta.js
index c4eb8415..aa405dae 100644
--- a/pages/book/_meta.js
+++ b/pages/book/_meta.js
@@ -6,10 +6,11 @@ export default {
'--': {
type: 'separator',
},
- types: 'Type system',
+ types: 'Type system overview',
functions: 'Functions',
statements: 'Statements',
constants: 'Constants',
+ 'defining-types': 'Defining composite types',
receive: 'Receive Messages',
bounced: 'Bounced Messages',
external: 'External Messages',
@@ -37,4 +38,4 @@ export default {
href: 'https://twitter.com/tact_language',
newWindow: true
},
-}
+}
\ No newline at end of file
diff --git a/pages/book/defining-types.mdx b/pages/book/defining-types.mdx
new file mode 100644
index 00000000..0c02631d
--- /dev/null
+++ b/pages/book/defining-types.mdx
@@ -0,0 +1,148 @@
+# Defining composite types
+
+import { Callout } from 'nextra/components'
+
+Tact supports a number of [primitive data types](/book/types#primitive-types) that are tailored for smart contract use. However, using individual means of storage often becomes cumbersome, so there are two main ways to combine multiple primitives together: [Structs](#structs) and [Messages](#messages).
+
+Note, while Traits and Contracts are also considered a part of the Tacts type system, one can't pass them around like [Structs](#structs) or [Messages](#messages). Instead, one can obtain the initial state of the given Contract by using the [initOf](/book/statements#initof) statement described later in the Book.
+
+
+
+ **Warning**: Currently circular types are **not** possible. This means that struct/message **A** can't have a field of a struct/message **B** that has a field of the struct/message **A**.
+
+ Therefore, the following code **won't** compile:
+
+ ```tact
+ struct A {
+ circularFieldA: B;
+ }
+
+ struct B {
+ impossibleFieldB: A;
+ }
+ ```
+
+
+
+## Structs
+
+Structs can define complex data types that contain multiple fields of different types. They can also be nested.
+
+```tact
+struct Point {
+ x: Int as int64;
+ y: Int as int64;
+}
+
+struct Line {
+ start: Point;
+ end: Point;
+}
+```
+
+Structs can also include both default fields and optional fields. This can be quite useful when you have many fields but don't want to keep respecifying them.
+
+```tact
+struct Params {
+ name: String = "Satoshi"; // default value
+ age: Int?; // optional field
+ point: Point; // nested Structs
+}
+```
+
+Structs are also useful as return values from getters or other internal functions. They effectively allow a single getter to return multiple return values.
+
+```tact
+contract StructsShowcase {
+ params: Params; // Struct as a Contract persistent state variable
+
+ init() {
+ self.params = Params{point: Point{x: 4, y: 2}};
+ }
+
+ get fun params(): Params {
+ return self.params;
+ }
+}
+```
+
+The order of fields does not matter. Unlike other languages, Tact does not have any padding between fields.
+
+## Messages
+
+Messages can hold [Structs](#structs) in them:
+
+```tact
+struct Point {
+ x: Int;
+ y: Int;
+}
+
+message Add {
+ point: Point; // holds a struct Point
+}
+```
+
+Messages are almost the same thing as [Structs](#structs) with the only difference that Messages have a 32-bit integer header in their serialization containing their unique numeric id. This allows Messages to be used with [receivers](/book/receive) since the contract can tell different types of messages apart based on this id.
+
+Tact automatically generates those unique ids for every received Message, but this can be manually overwritten:
+
+```tact
+// This Message overwrites its unique id with 0x7362d09c
+message(0x7362d09c) TokenNotification {
+ forwardPayload: Slice as remaining;
+}
+```
+
+This is useful for cases where you want to handle certain opcodes (operation codes) of a given smart contract, such as [Jetton standard](https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md). The short-list of opcodes this contract is able to process is [given here in FunC](https://github.com/ton-blockchain/token-contract/blob/main/ft/op-codes.fc). They serve as an interface to the smart contract.
+
+
+
+ For more in-depth information on this see:\
+ [Convert received messages to `op` operations](/book/func#convert-received-messages-to-op-operations)\
+ [Internal message body layout in TON Docs](https://docs.ton.org/develop/smart-contracts/guidelines/internal-messages#internal-message-body)\
+ [Messages of the Jetton implementation in Tact](https://github.com/howardpen9/jetton-implementation-in-tact/blob/9eee917877a92af218002874a9f2bd3f9c619229/sources/messages.tact)\
+ [Jetton Standard in Tact on Tact-by-Example](https://tact-by-example.org/07-jetton-standard)
+
+
+
+## Optionals
+
+As it was mentioned in [type system overview](/book/types), most [primitive types](/book/types#primitive-types), [Structs](#structs) and [Messages](#messages) could be nullable. That is, they don't necessarily hold any value, aside from `null{:tact}` — a special value, which represents the intentional absence of any other value.
+
+[Variables](/book/statements#variable-declaration) or fields of [Structs](#structs) and [Messages](#messages) that can hold `null{:tact}` are called "optionals". They're useful to reduce state size when the variable isn't necesserily used.
+
+You can make any variable an optional by adding a question mark (`?`) after its type declaration. The only exceptions are [`map<>{:tact}`](/book/types#maps) and [`bounced<>{:tact}`](/book/bounced#bounced-messages-in-tact), where you can't make them, inner key/value type (in case of a map) or the inner [Message](#messages) (in case of a bounced) optional.
+
+Optional variables that are not defined hold the `null{:tact}` value by default. You cannot access them without checking for `null{:tact}` first. But if you're certain the optional variable is not `null{:tact}`, use the non-null assertion operator (`!!`, also called double-bang or double exclamation mark operator) to access its value.
+
+Trying to access the value of an optional variable without using `!!` or checking for `null{:tact}` beforehand will result in a compilation error.
+
+Example of optionals:
+
+```tact
+struct StOpt {
+ opt: Int?; // Int or null
+}
+
+message MsOpt {
+ opt: StOpt?; // Notice, how the struct StOpt is used in this definition
+}
+
+contract Optionals {
+ opt: Int?;
+ address: Address?;
+
+ init(opt: Int?) { // optionals as parameters
+ self.opt = opt;
+ self.address = null; // explicit null value
+ }
+
+ receive(msg: MsOpt) {
+ let opt: Int? = 12; // defining a new variable
+ if (self.opt != null) { // explicit check
+ self.opt = opt!!; // using !! as we know that opt value isn't null
+ }
+ }
+}
+```
\ No newline at end of file
diff --git a/pages/book/types.mdx b/pages/book/types.mdx
index ab4f5a21..bfa025af 100644
--- a/pages/book/types.mdx
+++ b/pages/book/types.mdx
@@ -1,46 +1,52 @@
-# Tact type system
+# Type system overview
-Every variable, item, and value in a Tact program has a type:
+import { Callout } from 'nextra/components'
-* Primitives: Int, Bool, Slice, Cell, Builder, String and StringBuilder;
-* Map
-* Structs and Messages
-* Contracts and Traits
+Every variable, item, and value in Tact programs has a type. They can be:
-Also, most primitive types, structs, and messages could be nullable.
+* One of the [primitive types](#primitive-types)
+* [Maps](#maps)
+* Composite types, such as [Structs and Messages](#structs-and-messages)
+* or [Contracts](#contracts) and [Traits](#traits)
+
+Also, many of those types [can be made nullable](/book/defining-types#optionals).
## Primitive types
-* Int - all integers in Tact are 257-bit signed integers.
-* Bool - classical boolean with true/false values.
-* Address - standard address.
-* Slice, Cell, Builder - low-level primitive of TON VM.
-* String - type that represents text strings in TON VM.
-* StringBuilder - helper type that allows you to concatenate strings in a gas-efficient way
+* Int — all numbers in Tact are 257-bit signed integers, but [smaller representations](/book/integers#serialization) can be used to reduce storage costs.
+* Bool — classical boolean with `true` and `false` values.
+* Address — standard [smart contract address](https://docs.ton.org/learn/overviews/addresses#address-of-smart-contract) in TON Blockchain.
+* Slice, Cell, Builder — low-level primitives of TON VM.
+* String — represents text strings in TON VM.
+* StringBuilder — helper type that allows you to concatenate strings in a gas-efficient way.
## Structs and Messages
-Structs and Messages are almost the same thing with the only difference being that a message has a header in its serialization and therefore could be used as receivers.
-
-> **Warning**
-> Currently circular **types** are not possible. This means that struct/message **A** can't have a field of a struct/message **B** that has a field of the struct/message **A**.
+[Struct](/book/defining-types#structs) example:
-Example:
```tact
struct Point {
x: Int;
y: Int;
}
+```
+
+[Message](/book/defining-types#messages) example:
-message SetValue {
+```tact
+// Custom numeric id of the Message
+message(0x11111111) SetValue {
key: Int;
value: Int?; // Optional
+ coins: Int as coins; // Serialization into TL-B types
}
```
+Learn more about them on a dedicated page about [defining composite types](/book/defining-types).
+
## Maps
-The type `map` is used as a way to associate data with corresponding keys.
+The type `map{:tact}` is used as a way to associate data with corresponding keys.
Possible key types:
* Int
@@ -51,39 +57,40 @@ Possible value types:
* Bool
* Cell
* Address
-* Struct/Message
+* [Struct](/book/defining-types#structs)
+* [Message](/book/defining-types#messages)
```tact
contract HelloWorld {
- counters: map;
+ counters: map;
}
```
## Contracts
-Contracts are the main entry of a smart contract on the TON blockchain. It holds all functions, getters, and receivers of a contract.
+Contracts are the main entry of a smart contract on the TON blockchain. It holds all functions, getters, and receivers of a TON contract.
```tact
contract HelloWorld {
- counter: Int;
+ counter: Int;
- init() {
- self.counter = 0;
- }
+ init() {
+ self.counter = 0;
+ }
- receive("increment") {
- self.counter = self.counter + 1;
- }
+ receive("increment") {
+ self.counter = self.counter + 1;
+ }
- get fun counter(): Int {
- return self.counter;
- }
+ get fun counter(): Int {
+ return self.counter;
+ }
}
```
## Traits
-Tact doesn't support classical class inheritance, but instead introduces the concept of **traits**. Trait defines functions, receivers, and required fields. The trait is like abstract classes, but it does not define how and where fields must be stored. **All** fields from all traits must be explicitly declared in the contract itself. Traits itself also don't have constructors and all initial field initialization also must be done in the main contract.
+Tact doesn't support classical class inheritance, but instead introduces the concept of **traits**. Trait defines functions, receivers, and required fields. The trait is like abstract classes, but it does not define how and where fields must be stored. **All** fields from all traits must be explicitly declared in the contract itself. Traits themselves don't have `init(){:tact}` constructors, so all initial field initialization also must be done in the main contract.
```tact
trait Ownable {
@@ -110,4 +117,4 @@ contract Treasure with Ownable {
self.owner = owner;
}
}
-```
+```
\ No newline at end of file