Unbound variable Principal

Hi everyone,

what’s wrong with this:

public shared({caller}) func newEntry(): async () {
      let p = Principal.toText(caller);        
      Debug.print (p);

Unfortunately it does not compile. Why?

type error [M0057], unbound variable Principal

How can I log the caller?

dfx version 0.6.23

Thanks

The Principal in Principal.toText() is a module in the base library, where the function toText is declared, double check you’ve got a line importing it:

import Principal “mo:base/Principal”;

———

You can actually explore these modules on your local machine, in a terminal:
cd $(dfx cache show)/base

Or if you prefer, the same files can be found on the Motoko base library’s github repo:
https://github.com/dfinity/motoko-base/tree/master/src

Thanks, what confused me is that for example the access-control tutorial works heavily with principals like below. And I see no imports in the file.

 func principal_eq(a : Principal, b: Principal):Bool{
        return a == b;
    };

So it looks like I can have them as arguments without import, but if I want to use them I do need to import them.

In a: Principal, Principal represents a type which doesn’t need an extra import statement as the compiler knows the type already.

But in Principal.toText() the Principalrepresents a module in the base library, which you need to import first for the compiler to be able to use it.

Hope this helps! :slight_smile:

1 Like

Thanks for the explanation, I can live with that. Coming from java I was under the (false) impression that I can use anything without additional imports if they appear in the file.

Coming from Java something you might also want to keep in mind as you start using the base library modules, eg Array, is that by example you’d need to write statements like:

import Array “mo:base/Array”;

var a: [Nat] = [];
a := Array.append(a, [10]);

And not,

var a: [Nat] = [];
a.append(10);
3 Likes