diff --git a/docs/developer-docs/smart-contracts/call/arguments.mdx b/docs/developer-docs/smart-contracts/call/arguments.mdx index ccf4465eb0..4d62e6728b 100644 --- a/docs/developer-docs/smart-contracts/call/arguments.mdx +++ b/docs/developer-docs/smart-contracts/call/arguments.mdx @@ -29,34 +29,18 @@ The following example defines an array of numbers and a function that returns th Motoko differentiates between immutable arrays, which cannot be altered, and mutable arrays, which can be modified. -To declare an immutable array (default): - ```motoko -actor { - let a : [Nat] = [0, 1, 2, 3]; -} - -``` - -To declare a mutable array, the `[var _]` syntax is used: +import Nat "mo:base/Nat"; -```motoko actor { - let a : [var Nat] = [var 0, 1, 2, 3] ; -} -``` - -To declare a function that returns an array: - -```motoko -import Nat "mo:base/Nat" + let numbers1 : [Nat] = [1, 2, 3, 4, 5, 6, 7]; // Immutable array + let numbers2 : [var Nat] = [var 1, 2, 3, 4, 5, 6, 7] ; // Mutable array -actor { - let a : [Nat] = [0, 1, 2, 3]; - public func get_numbers(a: [Nat]) : async [Nat] { + public func get_numbers(numbers2: [Nat]) : [Nat] { return a; } } + ``` The `Array` Motoko base library provides utility functions on arrays. To learn more about the `Array` Motoko base library, refer to the [Motoko base library reference on Array](/docs/current/motoko/main/base/Array) and the [Motoko language quick reference on arrays](/docs/current/motoko/main/reference/language-manual#arrays). @@ -68,7 +52,7 @@ The `Array` Motoko base library provides utility functions on arrays. To learn m Rust uses the `vec` type to represent vectors (sequences, lists, arrays). ```rust -let numbers = vec![0, 1, 2, 3 ]; +let numbers = vec![1, 2, 3, 4, 5, 6, 7 ]; #[query] fn get_numbers(numbers: Vec) -> Vec { numbers @@ -84,59 +68,14 @@ Azle refers to the `Vec` type to represent the equivalent of an `array` in TypeS ```typescript import { IDL, query } from 'azle'; -const Day = IDL.Variant({ - Sun: IDL.Text, - Mon: IDL.Text, - Tue: IDL.Text, - Wed: IDL.Text, - Thu: IDL.Text, - Fri: IDL.Text, - Sat: IDL.Text -}); -type Day = - | { Sun: string } - | { Mon: string } - | { Tue: string } - | { Wed: string } - | { Thu: string } - | { Fri: string } - | { Sat: string }; - export default class { - @query([Day], IDL.Text) - getText(day: Day): string { - if ('Sun' in day) { - return day.Sun; - } - - if ('Mon' in day) { - return day.Mon; - } - - if ('Tue' in day) { - return day.Tue; - } - - if ('Wed' in day) { - return day.Wed; - } - - if ('Thu' in day) { - return day.Thu; - } - - if ('Fri' in day) { - return day.Fri; - } - - if ('Sat' in day) { - return day.Sat; - } - - throw new Error(`Variant "Day" has unknown tag`); + @query([], IDL.Vec(IDL.Nat8)) + get_numbers(): Nat { + return [1, 2, 3, 4, 5, 6, 7]; } } ``` + To learn more about variants in Typescript via Azle, refer to [the Azle code](https://github.com/demergent-labs/azle/blob/main/tests/end_to_end/candid_rpc/class_syntax/complex_types/src/candid_types.ts#L3). @@ -144,33 +83,12 @@ To learn more about variants in Typescript via Azle, refer to [the Azle code](ht }> ```python -from kybra import nat32, Variant, query +from kybra import int32, query, Vec -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" +def get_numbers() -> Vec[int32]: + return [1, 2, 3, 4, 5, 6, 7] ``` To learn more about variants in Python in Kybra, refer to [the Kybra book reference on variants](https://demergent-labs.github.io/kybra/reference/candid/variant.html). @@ -189,7 +107,7 @@ Assuming you have a method named `get_text` that accepts a `Variant` parameter, ```bash dfx canister call canister_name get_text '(variant {Sun})' ``` -To learn more about calling a method from a canister in bash, refer to the DFX reference on the [dfx canister call command](/docs/current/developer-docs/developer-tools/cli-tools/cli-reference/dfx-canister#dfx-canister-call). +To learn more about calling a method from a canister in bash, refer to the `dfx` reference on the [dfx canister call command](/docs/current/developer-docs/developer-tools/cli-tools/cli-reference/dfx-canister#dfx-canister-call). For additional examples, refer to [type variant in the Candid Reference](/docs/current/references/candid-ref#type-variant--n--t--) diff --git a/docs/developer-docs/smart-contracts/call/overview.mdx b/docs/developer-docs/smart-contracts/call/overview.mdx index a4958d8842..b6996e3397 100644 --- a/docs/developer-docs/smart-contracts/call/overview.mdx +++ b/docs/developer-docs/smart-contracts/call/overview.mdx @@ -35,11 +35,13 @@ Update calls are executed on all nodes of a subnet ```motoko -actor countCharacters { - public func test(text : Text) : async Bool { - let size = Text.size(text); - return size % 2 == 0; - }; +actor Counter { + + stable var counter = 0; + + public func inc() : async () { + counter += 1; + }; }; ``` @@ -48,9 +50,16 @@ actor countCharacters { ```rust +use candid::types::number::Nat; +use std::cell::RefCell; + +thread_local! { + static COUNTER: RefCell = RefCell::new(Nat::from(0_u32)); +} + #[ic_cdk_macros::update] -fn increment() { - COUNTER.with(|counter| *counter.borrow_mut() += 1); +fn inc() { + COUNTER.with(|counter| *counter.borrow_mut() += 1_u32); } ``` @@ -62,13 +71,13 @@ fn increment() { import { IDL, update } from 'azle'; export default class { - count: bigint = 0n; + counter: bigint = 0n; @update([], IDL.Nat64) - incrementCount(): bigint { - this.count += 1n; + inc(): bigint { + this.counter += 1n; - return this.count; + return this.counter; } } ``` @@ -78,10 +87,19 @@ export default class { }> ```python +from kybra import nat64, query, update + +counter: nat64 = 0 + +@query +def count() -> nat64: + return counter + @update -def set_message(new_message: str) -> void: - global message - message = new_message +def inc() -> nat64: + global counter + counter += 1 + return counter ``` @@ -102,11 +120,12 @@ The downside of query calls is that the response is not trusted since it's comin ```motoko -actor Echo { +actor { - public query func say(phrase : Text) : async Text { - return phrase; + public func greet(name : Text) : async Text { + return "Hello, " # name # "!"; }; + }; ``` @@ -130,11 +149,9 @@ fn greet(name: String) -> String { import { IDL, query } from 'azle'; export default class { - count: bigint = 0n; - - @query([], IDL.Nat64) - readCount(): bigint { - return this.count; + @query([], IDL.Text) + greet(name){ + return "Hello, {name}"; } } ``` @@ -144,9 +161,11 @@ export default class { }> ```python +from kybra import query + @query -def get_message() -> str: - return message +def greet(name: str) -> str: + return f"Hello, {name}!" ```