Create new canister or new actor motoko

111 Views Asked by At

I am new to motoko. As i understand an Actor in motoko is consider as a class.And I want to create multiple object from that Actor. My question is am I right about it and if the answer is yes then how can i identify or control these object.

2

There are 2 best solutions below

0
On

According to the Motoko docs:

An actor is similar to an object, but is different in that its state is completely isolated, its interactions with the world are entirely through asynchronous messaging, and its messages are processed one-at-a-time, even when issued in parallel by concurrent actors

0
On

An actor is like a plain object. If you want to create multiple instances, then you need to define an actor class, which is like a regular class but produces actors:

actor class A(n : Nat) {
  var x : Nat = n;
  public func get() : Nat { x };
  public func set(n : Nat) { x := n };
};


// ... elsewhere, spawn two actors:
// (needs await because actor creation is asynchronous)
let a1 = await A(1);
let a2 = await A(2);

// ... and use them:
let n = (await a1.get()) + (await a2.get());