Possible improvements to the Motoko Base Library

Hey Motoko Team,

I wanted to share some feedback and suggestions based on the last edition of Motoko Bootcamp, where we introduce developers to our language.

  1. Array.contains:
  • Right now, the Array library is missing a contains method. While you can replicate it using Array.filter and switch/case, having a native .contains method would be a simple and helpful addition.
  1. Handling Deprecated Methods:
  • The Motoko Base Library has what we call “deprecated” methods, which can be confusing for developers. From my experience they can be separated into two categories:

    • Functions with a clear alternative: For example, the Buffer library now recommends using static methods like Buffer.toArray instead of class methods like .toArray(). In such cases, it might be a good idea to remove these deprecated methods from the documentation, unless they are needed for compatibility reasons.
    • Functions without a clear alternative: let’s take a look at Array.append. I understand it to be deprecated to avoid its performances drawbacks, but in some cases, using Array.append is straighforward and doesn’t hurt performance much.
  • Additionally, we could consider:

    • Allowing developers to remove individual warnings rather than using the broad --remove-warnings option in moc, which wipes out all warnings. This would be great for developers who know what they’re doing and want to use Array.append or other deprecated methods on purpose.
    • Removing deprecated methods with clear alternatives from the Base Library, or at least excluding them from the documentation to keep things cleaner for new developers.
    • Changing the warning message. Instead of calling Array.append deprecated we could simply indicate that it’s a really inefficient method (something that every developer that goes through the Bootcamp is being repeated!). In my view, a deprecated method implies that it will eventually be phased out and removed from the language. If the intention is to retain Array.append (which I believe is the case), then it might be beneficial to modify the warning message. Perhaps we need to introduce a new type of warning specifically focused on performances impact?
  1. Missing Hash Functions:
  • The Hash module is missing some important hash methods for common types like Nat. The deprecated Hash.hash method might not be ideal for new developers, who might not want to create their own hash methods. Can we explore adding a recommended method for hashing Nat?

I’m also curious to know if other Motoko developers share my feedback - the discussion is open.
Thanks!

8 Likes

I think the closest from the base library is Array.find (instead of filter).

2 Likes

I agree with all those points, especially the need for decent hashing.

1 Like