You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: pages/book/structs-and-messages.mdx
+87Lines changed: 87 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -103,3 +103,90 @@ This is useful for cases where you want to handle certain opcodes (operation cod
103
103
[Jetton Standard in Tact on Tact-by-Example](https://tact-by-example.org/07-jetton-standard)
104
104
105
105
</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):
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
// 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):
0 commit comments