How to properly interact with Actor Classes?

Hi,
I have been looking at the (Actor Classes)[Untitled :: Internet Computer] guide in the SDK and trying to implement my own basic example but have difficulties interacting with it.

My basic project consists of two files, ‘main.mo’ and in the same folder ‘testclass.mo’.

// main.mo
import testclass "testclass";

actor {
    public func greet(name : Text) : async Text {
        return "Hello, " # name # "!";
    };
    public func test() : async Text {
        let thisclass = testclass.Hello();
        return await thisclass.run();
    };
};

and

// testclass.mo
actor class Hello(){
    public func run(): async Text {
        return "Hello World 12345!";
    };
};

The problem I have us that I get a build error on the return line of the test() function in the ‘main.mo’ file.

type or paste code here

I posted too early sorry so I will add the type error I received here:

After using dfx deploy:
Stderr: /home/sg/Documents/Programming/DFINITY/test_classes/src/test_classes/main.mo:9.22-9.31: type error [M0070], expected object type, but expression produces type async<$test> Hello


This is how the build error appeared inside of VS Code.

The way I understand how to use an actor class is like an object.
Here I take an instance of it by calling let thisclass = testclass.Hello(); and then expect to use the run() function by calling await thisclass.run();.

Thanks for your help :slight_smile:

Based on the error message it looks like you might need to await the class constructor as well:

let thisclass = await testclass.Hello();
1 Like

Thank you so much, that fixed the issue!

I need to improve my understanding of asynchronous method calls.