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
funmain() {
// define empty variable, set value laterlet a: u8;
a = 10;
// define variable, set typelet a: u64 = 10;
// finally simple assignmentlet a = 10;
// simple assignment with defined value typelet a = 10u128;
// in function calls or expressions you can use ints as constant valuesif (a < 10) {};
// or like this, with typeif (a < 10u8) {}; // usually you don't need to specify type
}
public fun create_from_rational(numerator:u64, denominator:u64):FixedPoint32{
let scaled_numerator = (numerator asu128) << 64;let scaled_denominator = (denominator asu128) << 32;assert!(scaled_denominator != 0,EDENOMINATOR);let quotient = scaled_numerator / scaled_denominator;FixedPoint32{value:(quotient asu64)}}
负数的话用u8类型举例子 1-127表示负数 128-256表示正数展现应用自行转换一下就行
集合数据类型
address
数组vector
字符串 string
hashmap( table ,bag)
自定义数据结构,struct
和 C Rust GO的struct一致,C++ Java Swift Javascript面向对象编程语言的类类似
public structDonutShop has key {id:UID,price:u64,balance:Balance<SUI>}
常量
constMAX:u64 = 100;
自定义方法
public entry fun eat_donut(d:Donut){letDonut{ id } = d;
object::delete(id);}
init方法
只能是私有的
会在发布合约的是自动调用一次
只有两种形式
fun init(ctx:&mut TxContext){}
fun init(witness:Struct, ctx:&mut TxContext){}
module examples::one_timer {
use sui::transfer;
use sui::object::{Self, UID};
use sui::tx_context::{Self, TxContext};
publicstructCreatorCapabilityhaskey {
id: UID
}
funinit(ctx: &mutTxContext) {
transfer::transfer(CreatorCapability {
id: object::new(ctx),
}, tx_context::sender(ctx))
}
}
方法访问控制
方法签名
调用范围
返回值
fun call()
模块内调用
可以有
public fun call()
只能模块能调用
可以有
public entry fun call()
模块能调用,DApp能调用
暂时没有
entry fun call()
只能DApp调用
暂时没有
public(package) fun call()
当前模块调用
可以有
泛型
move 支持在调用动态的确定数据类型
public structBox_U64{value:u64}
public structBox<T>{value:T}
public fun create_box(value:u64):Box<u64> {Box<u64>{ value }}
public fun create_box<T>(value:T):Box<T> {Box<T> { value }}
能力
理解为内置的基础权限,可以组合出一些常用的功能
structT has key{}structT has key,store{}structT has copy,drop{}