Passing arguments
Overview
Arrays
- Rust
- TypeScript
- Python
- bash
Rust uses the vec
type to represent vectors (sequences, lists, arrays).
let numbers = vec![0, 1, 2, 3 ];
#[query]
fn get_numbers(numbers: Vec<u32>) -> Vec<u32> {
numbers
}
Azle refers to the Vec
type to represent the equivalent of an array
in Typescript.
import { Cube, int32, Vec, query } from 'azle';
let numbers = [0,1,2,3];
export default Cube({
get_numbers: query([Vec(int32], Vec(int32), () => {
return numbers;
})
});
To learn more about the Vec
object in Typescript via Azle, refer to the Azle book reference on Vec.
Kybra refers to the Vec
type to represent the equivalent of an array
in Python. This is because Vec
aligns with the Candid type.
from kybra import int32, query, Vec
numbers = [0, 1, 2, 3]
@query
def get_numbers(Vec[int32]) -> Vec[int32]:
return numbers
To learn more about Vec
in Python via Kybra, refer to the Kybra book reference on Vec.
You can pass a vector as an argument by explicitly specifying the canister name and method name using the dfx
tool in the following format:
dfx canister call canister_name method_name '(vec {})'
Assuming you have a method named get_numbers
that accepts a Vec
parameter, as exemplified in other examples:
dfx canister call canister_name get_numbers '(vec {1; 2; 3})'
Variants
- Rust
- TypeScript
- Python
- bash
type Day = variant {
Sun;
Mon;
Tue;
Wed;
Thu;
Fri;
Sat;
}
[query]
fn get_text(d: Day) -> &'static str {
match d {
Day::Sun => "Sunday",
Day::Mon => "Monday",
Day::Tue => "Tuesday",
Day::Wed => "Wednesday",
Day::Thu => "Thursday",
Day::Fri => "Friday",
Day::Sat => "Saturday",
}
}
import { Cube, query, Variant } from 'azle';
const Day = Variant({
Sun;
Mon;
Tue;
Wed;
Thu;
Fri;
Sat;
});
export default Cube({
get_text: query([Day], Day, (d)) => {
switch (d) {
case Day.Sun:
return "Sunday";
case Day.Mon:
return "Monday";
case Day.Tue:
return "Tuesday";
case Day.Wed:
return "Wednesday";
case Day.Thu:
return "Thursday";
case Day.Fri:
return "Friday";
case Day.Sat:
return "Saturday";
}
}
});
To learn more about variants in Typescript via Azle, refer to the Azle book reference on variants.
from kybra import nat32, Variant, query
class Day(Variant, total=False):
Sun: str
Mon: str
Tue: str
Wed: str
Thu: str
Fri: str
Sat: str
@query
def get_text(d: Day) -> str:
if d == Day.Sun:
return "Sunday"
elif d == Day.Mon:
return "Monday"
elif d == Day.Tue:
return "Tuesday"
elif d == Day.Wed:
return "Wednesday"
elif d == Day.Thu:
return "Thursday"
elif d == Day.Fri:
return "Friday"
elif d == Day.Sat:
return "Saturday"
To learn more about variants in Python in Kybra, refer to the Kybra book reference on variants.
You can pass a variant as an argument by explicitly specifying the canister name and method name using the dfx
tool in the following format:
dfx canister call canister_name method_name '(variant {})'
Assuming you have a method named get_text
that accepts a Variant
parameter, as exemplified in other examples:
dfx canister call canister_name get_text '(variant {Sun})'