candid icon indicating copy to clipboard operation
candid copied to clipboard

Allow generating all funcs as unit structs

Open lastmjs opened this issue 2 years ago • 1 comments

While implementing Azle Candid is giving me some trouble because it is converting func Candid types sometimes into candid::Func but at other times (when boxing is required) into a unit struct. Having to deal with both outputs will add greater complication to Azle. It would be nice to simply force all func types to be converted to the unit struct.

Here's a simple service I am working with:

type User = record {
    "id": text;
    "basic_func": BasicFunc;
    "complex_func": ComplexFunc;
};

type Reaction = variant { "Good": null; "Bad": null; "BasicFunc": BasicFunc; "ComplexFunc": ComplexFunc };

type BasicFunc = func (text) -> (text);
type ComplexFunc = func (User, Reaction) -> (nat64);

service: {
    "basic_func_param": (BasicFunc) -> (BasicFunc) query;
    "basic_func_return_type": () -> (BasicFunc) query;
    "complex_func_param": (ComplexFunc) -> (ComplexFunc) query;
    "complex_func_return_type": () -> (ComplexFunc) query;
}

The Rust generated for BasicFunc:

type BasicFunc = candid::Func;

The Rust generated for ComplexFunc, which uses boxing because it is part of a self-referencing type (I assume):

# [derive(CandidType , Deserialize)] struct ComplexFunc(candid::Func);

If these types could be unified that would be easier to deal with. A setting to force this would be sufficient.

lastmjs avatar May 10 '22 14:05 lastmjs

You are right that ComplexFunc is boxed because it's recursive. OTOH, Rust doesn't require this, because it's using an opaque Func type. Depending on how we resolve #273, we may lift this difference.

chenyan-dfinity avatar May 11 '22 00:05 chenyan-dfinity