跳转到内容

编译时 (Compile-time)

此页面列出了所有内置的全局静态函数,这些函数在构建 Tact 项目时进行求值,并且无法与非常量的运行时数据一起使用。 这些函数通常被称为 “编译时函数(compile-time functions)“。

address

fun address(s: String): Address;

编译时函数,用于将包含地址的 String 转换为 Address 类型,并嵌入到合约中。

示例用法:

contract Example {
// Persistent state variables
addr: Address =
address("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"); // works at compile-time!
}

cell

fun cell(bocBase64: String): Cell;

编译时函数,用于将 base64 编码的BoC bocBase64 作为Cell嵌入到合约中。

使用示例:

contract Example {
// Persistent state variables
stored: Cell =
// Init package for Wallet V3R1 as a base64-encoded BoC
cell("te6cckEBAQEAYgAAwP8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVD++buA="); // works at compile-time!
}

slice

Available since Tact 1.5

fun slice(bocBase64: String): Slice;

编译时函数,将 base64 编码的 BoC bocBase64 作为 Slice 嵌入到合约中。

使用示例:

contract Example {
// Persistent state variables
stored: Slice =
// Works at compile-time!
slice("te6cckEBAQEADgAAGEhlbGxvIHdvcmxkIXgtxbw="); // Hello world!
}

rawSlice

Available since Tact 1.5

fun rawSlice(hex: String): Slice;

编译时函数,用于将hex String 的内容转换为 slice的十六进制编码和可选的位填充内容。

如果 String 的末尾有下划线 _,则内容将以位填充。 填充去掉了所有尾部的零和前面的最后一个 11 位:

// Not bit-padded
rawSlice("4a").loadUint(8); // 74, or 1001010 in binary
// Bit-padded
rawSlice("4a_").loadUint(6); // 18, or 10010 in binary

请注意,该功能是有限的,最多只能指定 10231023 位。

示例用法:

contract Example {
// Persistent state variables
stored: Slice =
rawSlice("000DEADBEEF000"); // CS{Cell{03f...430} bits: 588..644; refs: 1..1}
bitPadded: Slice =
rawSlice("000DEADBEEF000_"); // CS{Cell{03f...e14} bits: 36..79; refs: 0..0}
}

ascii

Available since Tact 1.5

fun ascii(str: String): Int;

编译时函数,用于将 str 中字符的十六进制值连接成一个,并将生成的 Int 嵌入到合约中。 仅适用于最多占用 3232 字节的字符串,即最多可表示 3232 ASCII 码88 44- 字节 Unicode 码点

用法示例:

contract Example {
// Persistent state variables
a: Int = ascii("a"); // 97 or 0x61, one byte in total
zap: Int = ascii("⚡"); // 14850721 or 0xE29AA1, 3 bytes in total
doubleZap: Int = ascii("⚡⚡"); // 249153768823457 or 0xE29AA1E29AA1, 6 bytes in total
}

crc32

Available since Tact 1.5

fun crc32(str: String): Int;

编译时函数,使用 CRC-32 算法计算校验和,并将计算结果 Int 值嵌入到合约中。

用法示例:

contract Example {
// Persistent state variables
checksum: Int = crc32("000DEADBEEF000"); // 1821923098
}

ton

fun ton(value: String): Int;

一个编译时函数,将给定的 Toncoin value 从人类可读格式 String 转换为 nanoToncoin Int 格式。

用法示例:

contract Example {
// Persistent state variables
one: Int = ton("1"); // one Toncoin, which is equivalent to 10^9 nanoToncoins
pointOne: Int = ton("0.1"); // 0.1 Toncoin, which is equivalent to 10^8 nanoToncoins
nano: Int = ton("0.000000001"); // 10^-9 Toncoins, which is equivalent to 1 nanoToncoin
// works at compile-time!
}