Compute Only/local Actor Classes

A quick question about actor classes. Let say I have some code that I may want other people to consume via a X-Cannister call or be able to reference local to their canister.

Is there a way to instantiate an actor that doesn’t require seeding cycles and doing a x-canister call? I just want this code to execute a few functions, run inside my current canister, use my cycles, and then go away. I know I can just create a class and then wrap it with an actor to get this done, but I’m just curious about the syntax and inner workings of actors. It would just be less code if I don’t have to have a wrapper.

let result : ComputableResponse =  switch(whatIWantToDo){
    case(#aLocalActor(aRequest){
        let a : ComputableActor = LocalActorClass(aRequest.initilizationParams);
        a.process();
    };
    case(#aRemoteActor(aRequest){
         
        let b : ComputableActor = actor.fromPrincipal(aRequest.remoteCanister);
        b.process();
    };
};

Motoko actors directly represent IC canisters, so any call to an actor is a cross-canister call by definition. When you are creating a new actor you are creating a new canister.

If you want to run local to the current canister, then regular objects/classes are in fact what you want.

2 Likes

Thanks for the confirmation Andress…I’ll try to come up with an easy pattern to wrap a class in an actor.

A class can be defined inside an actor, if that’s what you mean.

No…I mean I want an actor that has all the same functions of my class, but sometimes I want it to be referenced inside my canister with out a x-canister call and sometimes I want a different canister but will the same methods as my class.

An Example: I have an indexing code that parses data and holds an index. I may have a blog service that uses this internally…i don’t want to send each message to another canister every time because it is a 9_000_000 gas cost. On the other hand I may want to expose this same indexing functionality to other services that don’t know about my code but know my interface, so they can call a canister running this same code remotely and it is worth the extra gas cost to them. So locally I’ll just instantiate the class and use it, and if I want an actor, I’ll need to create an actor that instantiates the class and exposes the same methods. I was just hoping I could skip some of that extra layer.

I see, makes sense. Yes, if you need an actor to proxy some class/object then you have to program that explicitly I’m afraid.