|
| 1 | +--- |
| 2 | +title: Overview |
| 3 | +--- |
| 4 | + |
| 5 | +import Button from '@site/src/components/button' |
| 6 | + |
| 7 | +# Tolk Language: overview |
| 8 | + |
| 9 | +**Tolk** is a new language for writing smart contracts in TON. Think of Tolk as the "**next‑generation FunC**". |
| 10 | +Tolk compiler is literally a fork of FunC compiler, introducing familiar syntax similar to TypeScript, |
| 11 | +but leaving all low-level optimizations untouched. |
| 12 | + |
| 13 | +```tolk |
| 14 | +import "storage.tolk" |
| 15 | +
|
| 16 | +fun loadData() { |
| 17 | + ctxCounter = getContractData().beginParse().loadUint(32); |
| 18 | +} |
| 19 | +
|
| 20 | +fun onInternalMessage(msgValue: int, msgFull: cell, msgBody: slice) { |
| 21 | + var cs = msgFull.beginParse(); |
| 22 | + var flags = cs.loadMessageFlags(); |
| 23 | + if (isMessageBounced(flags)) { |
| 24 | + return; |
| 25 | + } |
| 26 | + ... |
| 27 | +} |
| 28 | +
|
| 29 | +get currentCounter(): int { |
| 30 | + loadData(); // fills global variables |
| 31 | + return ctxCounter; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +<details> |
| 36 | + <summary><b>See same logic implemented with FunC</b></summary> |
| 37 | + |
| 38 | +```func |
| 39 | +#include "storage.fc"; |
| 40 | +
|
| 41 | +() load_data() impure { |
| 42 | + slice cs = get_data().begin_parse(); |
| 43 | + ctx_counter = cs~load_uint(32); |
| 44 | +} |
| 45 | +
|
| 46 | +() recv_internal(int msg_value, cell msg_full, slice msg_body) impure { |
| 47 | + slice cs = msg_full.begin_parse(); |
| 48 | + int flags = cs.load_uint(4); |
| 49 | + if (flags & 1) { |
| 50 | + return (); |
| 51 | + } |
| 52 | + ... |
| 53 | +} |
| 54 | +
|
| 55 | +int currentCounter() method_id { |
| 56 | + load_data(); ;; fills global variables |
| 57 | + return ctx_counter; |
| 58 | +} |
| 59 | +``` |
| 60 | +</details> |
| 61 | + |
| 62 | +<Button href="https://github.com/ton-blockchain/convert-func-to-tolk" colorType={'primary'} sizeType={'sm'}> |
| 63 | + Try a FunC → Tolk converter |
| 64 | +</Button> |
| 65 | +<Button href="/v3/documentation/smart-contracts/tolk/tolk-vs-func/in-short" colorType={'secondary'} sizeType={'sm'}> |
| 66 | + Read "Tolk vs FunC differences" |
| 67 | +</Button> |
| 68 | +<div style={{height: '2em'}}></div> |
| 69 | + |
| 70 | + |
| 71 | +## Motivation behind Tolk |
| 72 | + |
| 73 | +FunC is awesome. |
| 74 | +It is really low-level and encourages a programmer to think about compiler internals. |
| 75 | +It gives full control over TVM assembler, allowing a programmer to make his contract as effective as possible. |
| 76 | +If you get used to it, you love it. |
| 77 | + |
| 78 | +But there is a problem. |
| 79 | +FunC is "functional C", and it's for ninja. |
| 80 | +If you are keen on Lisp and Haskell, you'll be happy. |
| 81 | +But if you are a JavaScript / Go / Kotlin developer, its syntax is peculiar for you, leading to occasional mistakes. |
| 82 | +A struggle with syntax may decrease your motivation for digging into TON. |
| 83 | + |
| 84 | +Imagine, what if there was a language, also smart, also low-level, but not functional and not like C? |
| 85 | +Leaving all beauty and complexity inside, what if it would be more similar to popular languages at first glance? |
| 86 | + |
| 87 | +That's what Tolk is about. |
| 88 | + |
| 89 | + |
| 90 | +## Migrating from FunC to Tolk |
| 91 | + |
| 92 | +If you know FunC and want to try a new syntax, your way is: |
| 93 | +1. Read [Tolk vs FunC: in short](/v3/documentation/smart-contracts/tolk/tolk-vs-func/in-short). |
| 94 | +2. With blueprint, create a new Tolk contract (for example, a counter) and experiment around. Remember, that almost all stdlib functions are renamed to ~~verbose~~ clear names. Here is [a mapping](/v3/documentation/smart-contracts/tolk/tolk-vs-func/stdlib). |
| 95 | +3. Try a [converter](https://github.com/ton-blockchain/convert-func-to-tolk) for your existing contracts or one from [FunC Contracts](/v3/documentation/smart-contracts/contracts-specs/examples). Keep in mind that contracts written in Tolk from scratch would definitely look nicer than being auto-converted. For instance, using logical operators instead of bitwise tremendously increases code readability. |
| 96 | + |
| 97 | +## How to try Tolk if you don't know FunC |
| 98 | + |
| 99 | +:::tip Currently, this documentation assumes that you know FunC |
| 100 | +The documentation describes "Tolk vs FunC" differences. |
| 101 | +Later it will be adapted to land newcomers. Moreover, FunC will eventually become deprecated, |
| 102 | +and all code snippets throughout the whole documentation will be rewritten to Tolk. |
| 103 | +::: |
| 104 | + |
| 105 | +If you are new to TON, your way is: |
| 106 | +1. Dig into [this documentation](/v3/documentation/smart-contracts/overview) to acquire basic on development in TON. No matter what language you'll use, you need to be aware of cells, slices, TON asynchronous nature after all. |
| 107 | +2. Facing FunC snippets, you can still use FunC, or try to express the same in Tolk. If FunC syntax is peculiar for you, don't worry: the goal of Tolk is exactly to fix this issue. |
| 108 | +3. Once you gain some understanding of what's going on, try using Tolk with [blueprint](https://github.com/ton-org/blueprint). |
| 109 | + |
| 110 | + |
| 111 | +## Tooling around Tolk Language |
| 112 | + |
| 113 | +Sources of the Tolk compiler are a part of the `ton-blockchain` [repo](https://github.com/ton-blockchain/ton). |
| 114 | +Besides the compiler, we have: |
| 115 | + |
| 116 | +1. [tolk-js](https://github.com/ton-blockchain/tolk-js) — a WASM wrapper for Tolk compiler. |
| 117 | +2. [JetBrains IDE plugin](https://github.com/ton-blockchain/intellij-ton) supports Tolk besides FunC, Fift, TL/B, and Tact. |
| 118 | +3. [VS Code Extension](https://github.com/ton-blockchain/tolk-vscode) enabling Tolk Language support. |
| 119 | +4. [Converter from FunC to Tolk](https://github.com/ton-blockchain/convert-func-to-tolk) — convert a `.fc` file to a `.tolk` file with a single `npx` command. |
| 120 | +5. Tolk Language is available in [blueprint](https://github.com/ton-org/blueprint). |
| 121 | + |
| 122 | + |
| 123 | +## Is Tolk production-ready? |
| 124 | + |
| 125 | +The Tolk compiler, a fork of the FunC compiler, is deemed production-ready, albeit somewhat experimental at the moment. |
| 126 | + |
| 127 | +Undiscovered bugs may exist, potentially inherited from FunC or attributable to TVM characteristics. |
| 128 | +Anyway, no matter what language you use, you should cover your contracts with tests to reach high reliability. |
| 129 | + |
| 130 | + |
| 131 | +## Roadmap |
| 132 | + |
| 133 | +The first released version of Tolk is v0.6, emphasizing [missing](/v3/documentation/smart-contracts/tolk/changelog#how-tolk-was-born) FunC v0.5. |
| 134 | + |
| 135 | +Here are some (yet not all and not ordered in any way) points to be investigated: |
| 136 | +- type system improvements: boolean type, nullability, dictionaries |
| 137 | +- structures, with auto-packing to/from cells, probably integrated with message handlers |
| 138 | +- structures with methods, probably generalized to cover built-in types |
| 139 | +- some integrations with TL scheme, either syntactical or via code generation |
| 140 | +- human-readable compiler errors |
| 141 | +- easier messages sending |
| 142 | +- better experience for common use-cases (jettons, nft, etc.) |
| 143 | +- gas and stack optimizations, AST inlining |
| 144 | +- extending and maintaining stdlib |
| 145 | +- think about some kind of ABI (how explorers "see" bytecode) |
| 146 | +- think about gas and fee management in general |
| 147 | + |
| 148 | +Note, that most of the points above are a challenge to implement. |
| 149 | +At first, FunC kernel must be fully refactored to "interbreed" with abilities it was not designed for. |
| 150 | + |
| 151 | +Also, I see Tolk evolution partially guided by community needs. |
| 152 | +It would be nice to talk to developers who have created interconnected FunC contracts, |
| 153 | +to absorb their pain points and discuss how things could be done differently. |
| 154 | + |
| 155 | + |
| 156 | +## Issues and Contacts |
| 157 | + |
| 158 | +If you face issue, connect to developer society on [TON Dev Chats](https://t.me/addlist/1r5Vcb8eljk5Yzcy) or create GitHub issues. |
0 commit comments