[Highlight Post] New Motoko Features in DFX 0.12

Hey Motoko Devs!

This is Kento from the Languages team at DFINITY.

We would like to walk you through some newly released changes to Motoko which are available now in the new version of dfx! (Remember to upgrade your dfx version with dfx upgrade).

We will highlight the most exciting features in this post, but be sure to check out our releases to see the full list: Releases · dfinity/motoko · GitHub.

Combine and Augment Records with new Extension Syntax

Say you have two records:

let person = { name = “Bob”; age = 30 };
let contact = { email = “bob@gmail.com”; number = “424 111 222” };

You can now create a new record using the and keyword.

let profile = { person and contact };

If the two records have overlapping fields, you can disambiguate using the with keyword.

let person = { name = “Bob”; age = 30; email = “old_email@gmail.com”};
let contact = { email = “bob@gmail.com”; number = “424 111 222” };

let profile = { person and contact with email = contact.email };

So where might you use this in the real world? Database joins!

Consider a situation where you have a few database tables (implemented as collections of records). One table might contain static information, like user data, and the other dynamic information, like access statistics. This new language feature lets you keep the database tables cleanly separated, while allowing you to join rows (records) together, potentially including dynamic values.

If you’re interested in this new syntax, you might also be interested in the already existing type-level and operator that works nicely with this feature.

In summary, the record extension syntax is now a Swiss Army knife of a feature, allowing you to

  • build records from fields
  • augment records with new fields
  • change existing fields in records (along with their types)
  • join records (disambiguating overlapping fields in the process)

Buffer Class with a lot of Bells and Whistles

Are you a fan of vectors? How about vectors with a lot of utility functions?

Wait no more!: New Buffer Class

Some cool stuff you can do with the revamped Buffer class:

buffer.sort(Nat.compare); // sort your list in place
Buffer.toText(buffer, Nat.toText); // turn your list into text
Buffer.subBuffer(buffer, 2, 5); // sublists
Buffer.forall(buffer, func x = x > 2); // some condition checking
Buffer.binarySearch(x, buffer, Nat.compare); // do you remember your algorithms?

Checkout the documentation linked above for the full list, and runnable examples! And if there are any functions you want added to base, let us know below.

More Features

Other new features include dynamically upgrading actor classes from within your Motoko program, new language primitives and operators, and improved compilation times! For more details, see our releases on GitHub.

Teaser: Generational Garbage Collection

How does faster and more efficient Motoko sound to you? Free performance gains from efficient memory management? Lookout for this and more in future releases of Motoko :slight_smile: Or if you’re impatient, the change is merged in master so you can build it locally yourself if you’re so inclined: see discussion here.

More of these coming in the future!

The languages team wanted to increase our communication about what we’re building and releasing. So we’ll be doing more of these to keep you guys up to date. And if you see any features you think are particularly cool, let us know and spread the word! We’d greatly appreciate it.

Till next time!

– DFINITY Languages team.

25 Likes