func
Values created by the CandidType
function Func
correspond to the Candid type func, are inferred to be TypeScript [Principal, string]
tuples, and will be decoded into JavaScript array with two elements at runtime.
The first element is an @dfinity/principal and the second is a JavaScript string. The @dfinity/principal
represents the principal
of the canister/service where the function exists, and the string
represents the function's name.
A func
acts as a callback, allowing the func
receiver to know which canister instance and method must be used to call back.
TypeScript or JavaScript:
import { Canister, Func, Principal, query, text } from 'azle/experimental';
const BasicFunc = Func([text], text, 'query');
export default Canister({
getBasicFunc: query([], BasicFunc, () => {
return [
Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai'),
'getBasicFunc'
];
}),
printBasicFunc: query([BasicFunc], BasicFunc, (basicFunc) => {
console.log(typeof basicFunc);
return basicFunc;
})
});
Candid:
service : () -> {
getBasicFunc : () -> (func (text) -> (text) query) query;
printBasicFunc : (func (text) -> (text) query) -> (
func (text) -> (text) query,
) query;
}
dfx:
dfx canister call candid_canister printBasicFunc '(func "r7inp-6aaaa-aaaaa-aaabq-cai".getBasicFunc)'
(func "r7inp-6aaaa-aaaaa-aaabq-cai".getBasicFunc)