How to call a canister by using canister principal in a canister?

Canister.mo :

shared(installer) actor class canister() = this{
    public query func test() : async Text{
        "test canister"
    };
};

test.mo :

private type interface = actor{
    test : query() -> async Text;
};

public shared func test() : async (){
    let canister = await Canister.canister();
    let principal = Principal.fromActor(canister);
    let text = Principal.toText(principal);
    let c = actor text : interface;
}

The code of test.mo is wrong at :

let c = actor text : interface;

How can I call a canister from canisterโ€™s principal in a canister ?
&
other use case of โ€œthisโ€ beside :

shared(installer) actor class canister() = this { ยทยทยทยทยทยท }; 
ยทยทยท
Principal.fromActor(this)
ยทยทยท

Blind guess (on the phone right now), but try actor (text), else the completer might think you are defining a new actor named text here.

2 Likes

Perfect answer, I have solved it !
example code :

canister.mo

shared(installer) actor class canister() = this{
    public query(msg) func test() : async Text{
        "test canister"
    };
};

main.mo

import Debug "mo:base/Debug";
import Principal "mo:base/Principal";
import c "canister";
actor {
    private type testCanister = actor{
        test : query() -> async Text;
    };
    public func test() : async (){
        var canister = await c.canister();
        var principal = Principal.fromActor(canister);
        var text = Principal.toText(principal);
        let canister_ = actor (text) : testCanister;
        Debug.print(await canister_.test())
    };
};

2 Likes

I am guessing we have to define the type otherwise we get type error [M0058], no type can be inferred for actor reference. Edit. Yup that seems to be the case.