In Sway, in addition to primitive types, you can use compound types to build more complex smart contracts.
(42, true) is a tuple that contains one u64 type and one bool type.t.0 and t.1.Point struct contains two fields: x and y..).u8), strings, or even struct instances.In the provided code, we define a smart contract named MyContract with a function called test_func that returns an instance of the Point struct.
t and destructure it, assigning its values to num and boo.one.Point struct, p, and access its fields.u8 elements, one containing strings, and one containing instances of the Point struct.mut_arr and modify the value of one of its elements.The test_func function of this smart contract ultimately returns an instance of the Point struct. This simple example is intended to show you how to use these compound types in Sway. We hope this tutorial helps you better understand the compound types of Sway.
contract;
// Address
// Contract Id
// Identity
abi MyContract {
fn test_func() -> Identity;
}
impl MyContract for Contract {
fn test_func() -> Identity {
// Address
let b: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let addr: Address = Address::from(b);
let b: b256 = addr.into();
// Contract id
let b: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let my_contract_id: ContractId = ContractId::from(b);
let b: b256 = my_contract_id.into();
// Identity type
let raw_addr: b256 = 0xddec0e7e6a9a4a4e3e57d08d080d71a299c628a46bc609aab4627695679421ca;
let addr = Address::from(raw_addr);
let my_id: Identity = Identity::Address(addr);
// Match on identity
let id: Address = match my_id {
Identity::Address(addr) => addr,
Identity::ContractId(id) => revert(0),
};
my_id
}
}