Dynamically importing canisters with Motoko

I am building a large storage network wherein I would like to have a function which takes in a text value and returns a canister.

Equivalently, I need a way to dynamically import a canister and save it as a variable.

What I have in mind is something like:

import Mystoragecanister "canister:mystoragecanister";

	func getcanister(canistername: Text) : Canister {
		return Mystoragecanister;

Of course this does not work and the error I get is: “type error [M0029], unbound type Canister.”

So, how can I dynamically import Canisters with Motoko?

Thanks in advance!

1 Like

I think I can adapt this answer Programmatic import in Motoko - #3 by gilbert to solve my problem. Basically, creating a type in the format if your actor and then creating a function that outputs that type should do the trick (as long as all your canisters you wish to choose from to return are of the same type).

If it works, I will post again and let you all know.

Update: This solution works like a charm!