# Actor Constructor/Initialization (not actor class)

**URL:** <https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525>\
**Category:** Programs & Applications\
**Created:** [December 17, 2022, 9:23pm UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525 "2022-12-17T21:23:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)\
**Post date:** [December 17, 2022, 9:23pm UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/1 "2022-12-17T21:23:29Z")

</div>

In Motoko are actors allowed to have constructors like actor classes?  
If not what is the best way to allow an initialization process to happen for the actor.  
For example, if I want to ‘whitelist’ user(s) to have access to do certain actions, the options I see are

1. Create a single actor class instance, initialized with the data I want
2. Hard code in the principal Id(s) into the code
3. Allow the ‘controller’ of the canister access to update the user(s) with a custom function (but as far as I can tell the only way to get the controllers is an async call to `ic.canister_status(..)`

Any thoughts on best practices/missing information would be wonderful

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 17, 2022, 9:24pm UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/2 "2022-12-17T21:24:51Z")

</div>

Why not just do an actor class?

---

<div class="post-metadata">

**Author:** ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)\
**Post date:** [December 17, 2022, 9:43pm UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/3 "2022-12-17T21:43:47Z")

</div>

No specific reason, just felt weird about having a single canister be an actor class. So far I’ve found it more tedious to manage via dfx

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [December 18, 2022, 12:50am UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/4 "2022-12-18T00:50:07Z")

</div>

The `class` keyword in Motoko is basically just convenience for manually defining a type and a function that returns a value of that type. The function plays the role of the class constructor.

> <https://github.com/dfinity/motoko/issues/67#issuecomment-437650178>
>
> I can emulate this really well already by doing the following:
> 
> \`\`\`swift
> type… FooClass = {
> init : Int -\> FooInstance;
> someStaticMethod : () -\> Text;
> };
> 
> type FooInstance = {
> someInstanceMethod : () -\> Int;
> };
> 
> let Foo : FooClass = new Self {
> init = func (x : Int) : FooInstance {
> new self {
> someInstanceMethod = func () : Int {
> x;
> };
> };
> };
> 
> someStaticMethod = func () : Text {
> "Some text";
> };
> };
> 
> print(Foo.someStaticMethod() # "\\n");
> let foo = Foo.init(42);
> printInt(foo.someInstanceMethod());
> \`\`\`
> 
> This also allows me to reference class members inside of the main block using \`Self\` and instance members via \`self\`.
> 
> However, it isn’t transparent (mainly) because the instance type \`FooInstance\` and binding name \`Foo\` aren’t allowed to be the same and it leaks into other types, e.g \`FooInstance\[\]\` instead of \`Foo\[\]\`
> 
> Is there any possibility of doing either of the following?
> 
> 1. Distinguish between types and values so that \`type Foo = {};\` and \`let Foo = {};\` are allowed to coexist.
> 2. Support static/class members natively via the \`class\` keyword
> 
> The latter has the benefit of dropping \`init\` methods but I’d also request something like \`Self\` and \`self\` for \`class\` in that case. Perhaps \`self\` would be sufficient if the class can be referenced within itself by name, e.g. \`Foo.someStaticMember\`

In your case you could do something like this:

```auto
import Buffer "mo:base/Buffer";
import Debug "mo:base/Debug";
import Principal "mo:base/Principal";

```

```auto
type MyClassType = {
  // static members
  new : MyClassConstructorArgs -> MyClass;
};

```

```auto
type MyClassConstructorArgs = {
  controllers : [Principal];
};

```

```auto
type MyClass = {
  // instance members
  addController : Principal -> ();
  getControllers : () -> [Principal];
};

```

```auto
let MyClass : MyClassType = object This {
  public func new(args : MyClassConstructorArgs) : MyClass {
    // private instance members
    let controllers = Buffer.fromArray<Principal>(args.controllers);
    
    // public instance members
    return object this {
      public func addController(controller : Principal) {
        controllers.add(controller);
      };
      
      public func getControllers() : [Principal] {
        return controllers.toArray();
      };
    };
  };
};

```

```auto
let myClassInstance = MyClass.new({
  controllers = [];
});

// prints []
Debug.print(debug_show(myClassInstance.getControllers()));

```

```auto
myClassInstance.addController(Principal.fromText("aaaaa-aa"));

// prints [aaaaa-aa]
Debug.print(debug_show(myClassInstance.getControllers()));

```

I tried to port this to `return actor this` and made the return types `async` but that ended up in a cryptic `type error [M0038], misplaced await` error despite not even using `await` anywhere (FYI @claudio)

@Gekctek it’s probably simpler to use an actor class 🙂

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [December 18, 2022, 1:00am UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/5 "2022-12-18T01:00:57Z")

</div>

You can do something like

```auto
shared ({ caller = owner }) actor class Clazz({
  authorizedUsers: [Principal]
}) {
  ...
}

```

Then just upgrade the canister with the arguments passed to the constructor if you want to make a change.

If you’re doing this canister wasm upgrade through another canister the arguments field (`args`) expects a candid encoded `Blob`, so you can encode the new constructor arguments (new authorized users) with `to_candid`.

As a rule of thumb I generally try not to hold the access control list or any frequently updated settings in the canister’s constructor since that requires a full state serialization/de-serialization (expensive).

Better to hold arguments that will change very infrequently

---

<div class="post-metadata">

**Author:** ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)\
**Post date:** [December 18, 2022, 1:09am UTC](https://forum.dfinity.org/t/actor-constructor-initialization-not-actor-class/17525/6 "2022-12-18T01:09:31Z")

</div>

Thanks @skilesare, @paulyoung and @icme  
Not sure why I was so against the `actor class`’s, my mental model was that they were just dynamic actors created from a ‘normal’ actor. But its seems that they are more similar than I thought. Ill go this route and see if I have any issues

> [@icme](#):
>
> As a rule of thumb I generally try not to hold the access control list or any frequently updated settings in the canister’s constructor since that requires a full state serialization/de-serialization (expensive).

Ya, I agree. Im trying to do a temporary `oracle` like setup from an outside source, then converting to an http outcall method. Just was trying to avoid complexity to get a proof of concept going
