Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 95a2ab8

Browse files
authored
feat: small Structs and Messages update (#222)
* Instantiation description * Trailing comma * Field punning * `.toCell()` extension function * Cross-link to Expressions#instantiation
1 parent 9bba642 commit 95a2ab8

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pages/book/structs-and-messages.mdx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,90 @@ This is useful for cases where you want to handle certain opcodes (operation cod
103103
[Jetton Standard in Tact on Tact-by-Example](https://tact-by-example.org/07-jetton-standard)
104104

105105
</Callout>
106+
107+
## Operations
108+
109+
### Instantiate
110+
111+
Creation of [Struct](#structs) and [Message](#messages) instances resembles [function calls](/book/expressions#static-function-call), but instead of paretheses `(){:tact}` one needs to specify arguments in braces `{}{:tact}` (curly brackets):
112+
113+
```tact
114+
struct StA {
115+
field1: Int;
116+
field2: Int;
117+
}
118+
119+
message MsgB {
120+
field1: String;
121+
field2: String;
122+
}
123+
124+
fun example() {
125+
// Instance of a Struct StA
126+
StA{field1: 42, field2: 68 + 1, }; // trailing comma is allowed
127+
128+
// Instance of a Message MsgB
129+
MsgB{field1: "May the 4th", field2: "be with you!", }; // trailing comma is allowed
130+
}
131+
```
132+
133+
When the name of a variable or constant assigned to a field coincides with the name of such field, Tact provides a handy syntactic shortcut sometimes called field punning. With it, you don't have to type more than it's necessary:
134+
135+
```tact
136+
struct PopQuiz {
137+
vogonsCount: Int;
138+
nicestNumber: Int;
139+
}
140+
141+
fun example() {
142+
// Let's introduce a couple of variables
143+
let vogonsCount: Int = 42;
144+
let nicestNumber: Int = 68 + 1;
145+
146+
// You may instantiate the Struct as usual and assign variables to fields,
147+
// but that is a bit repetetive and tedious at times
148+
PopQuiz{vogonsCount: vogonsCount, nicestNumber: nicestNumber, };
149+
150+
// Let's use field punning and type less,
151+
// because our variable names happen to be the same as field names
152+
PopQuiz{vogonsCount, nicestNumber, }; // trailing comma is allowed here too!
153+
}
154+
```
155+
156+
<Callout>
157+
158+
Because instantiation is an expression in Tact, it's also described on the related page: [Instantiation expression](/book/expressions#instantiation).
159+
160+
</Callout>
161+
162+
### Convert to a `Cell`, `.toCell()` [#tocell]
163+
164+
It's possible to convert an arbitrary [Struct](#structs) or [Message](#messages) to the [`Cell{:tact}`][p] type by using the `.toCell(){:tact}` [extension function](/book/functions#extension-function):
165+
166+
```tact
167+
struct Big {
168+
f1: Int;
169+
f2: Int;
170+
f3: Int;
171+
f4: Int;
172+
f5: Int;
173+
f6: Int;
174+
}
175+
176+
fun convertationFun() {
177+
dump(Big{
178+
f1: 10000000000, f2: 10000000000, f3: 10000000000,
179+
f4: 10000000000, f5: 10000000000, f6: 10000000000,
180+
}.toCell()); // x{...cell with references...}
181+
}
182+
```
183+
184+
<Callout>
185+
186+
See those extension functions in the Reference:\
187+
[`Struct.toCell(){:tact}`](/book/api-cells#structtocell)\
188+
[`Message.toCell(){:tact}`](/book/api-cells#messagetocell)
189+
190+
</Callout>
191+
192+
[p]: /book/types#primitive-types

0 commit comments

Comments
 (0)