Move Adds Enums and Macros in 2024 Edition

New features set to launch on Move, Sui's native programming language, in early 2024 include Enums, macro functions, and Method syntax.

Move Adds Enums and Macros in 2024 Edition

Updates coming to the Move programming language this year give developers new ways to define data and call functions. These significant new features make building apps on Sui even more flexible and comport with the expectations for the most current programming languages.

Move, Sui's native programming language, lets developers write efficient and expressive code. Originally designed by Mysten Labs CTO and co-founder Sam Blackshear, this smart contract-focused language takes advantage of Sui's scalable, high throughput environment.

Updates in Move 2024 Edition introduce Enums, macro functions, Method syntax, and a host of other new features making it even more capable. As open source, Move and Sui allow continual improvements from the community. 

Major Move updates 

The following features will be available to test in alpha versions initially, followed by release candidate and final releases.

Method syntax

Method syntax is a syntactic transformation that allows for functions to be called “on” a value rather than directly from a module. The change greatly improves the ease of programming in Move.

For example:

let c2: Coin<SUI> = c.withdraw(10);

Expands to:

let c2: Coin = sui::coin::withdraw(&mut c, 10);

A new syntax, use fun, will be introduced for adding either public or internal method aliases. This feature can be used for creating an alias to rename a method. Or it can be used to create a local method alias outside of the associated types defining module.

Index syntax

Building on Method syntax, Index syntax adds code to access indexes, with specific code depending on the type of access. Tentatively, this code will look like the following:

&x[i] expands to x.borrow(i)
&mut x[i] expands to x.borrow_mut(i)
x[i] expands to *x.borrow(i)
x[i] = v expands to x.assign(i, v)

Macro functions

Higher-order functions, such as map, filter, fold, and for_each, are useful in many languages for concisely transforming collections. Move does not have lambdas, closures, or function pointers, which makes defining these sorts of operations impossible.

Macro functions let Move mimic these types of operations without supporting the behavior at runtime. The body of the macro mimicking the higher-order function will get inlined at each call site. And the call site can provide a lambda that will be substituted in as the macro is expanded. 

For example:

let v2 = v.map!(|x| x + 1);

or

v.for_each!(|x| foo(x));

Additionally, the "lambdas" will support control flow through break and return.

Enums

Enumerations let developers define a single type that may hold multiple different shapes of data. Unlike structs, which always have the same fields, enums can have different fields depending on the variant of the enum. For example, in enum Option<T> { None, Some(T) }, the variant None has no fields and the variant Some has a single field of type T.

Move will allow destructuring enums using match expressions. Some examples of enums in Move are the following:

public enum Color {
    RGB { red: u8, green: u8, blue: u8 },
    HSL { hue: u16, saturation: u8, lightness: u8 },
    Hex(u32)
}
public enum Option<T> {
    None,
    Some(T),
}
public fun is_rgb_color(color: Color): bool {
    match (color) {
        Color::RGB{ .. } => true,
        _ => false,
    }
}
const EOptionIsNone: u64 = 0;
public fun unwrap_some<T>(option: Option<T>): T {
    match (option) {
        Option::Some(x) => x,
        Option::None => abort EOptionIsNone,
    }
}

Although enums are a part of the Move 2024 Edition, they will not be immediately available on Sui Mainnet. Instead, the feature will first launch only on Devnet, followed by Testnet and then Mainnet, once it seems fit for a production environment. 

Additional new features

Beyond those shown above, the Move 2024 edition includes additional smaller updates, such as eliminating the need for explicit friend declarations and allowing break to take a value. The roadmap contains explicit reasoning and examples behind these updates. In general, they are designed to future-proof Move and make the coding experience more efficient.

Breaking changes

A few planned updates for Move will affect existing code. Of the three changes noted here, most require more explicit code, such as including mut annotations for local variables if they are assigned or mutably borrowed. 

Struct declarations

Currently, struct declarations can only be public, so it's not necessary to include the visibility modifier. To make room for a future where struct types have visibility other than public, the 2024 edition of Move will require public on all struct declarations.

mut annotations

Today, all local variables in Move can be assigned x = e and mutably borrowed &mut x. Variables don't need to be annotated as mut before being modified or mutably borrowed due to the fact that developers can always look locally to check for assignments or mutable borrows. However, the new method syntax, mentioned above, will automatically borrow locals in some circumstances. 

To improve readability and understandability in the presence of method calls, the 2024 edition of Move will require adding mut annotations to all local variables if they are assigned or mutably borrowed.

New keywords

Move 2024 adds new keywords that were previously accepted as identifiers. The new keywords are:

  • mut
  • enum
  • type
  • match

To help with migrating existing fields, functions, or local variables with these names, new syntax lets developers use a keyword as an identifier. For example:

let `type` = 0; `type` + 1

In short, any keyword can be used as an identifier by enclosing it in backticks, as with `type`.

Testing and implementation

The 2024 edition of Move will roll out in a considered manner, letting developers test new features and update existing code accordingly. Developers can begin testing the alpha version by specifying edition = "2024.alpha" under the [package] section in their Move.toml files. Features released under the alpha version will be unstable and might fail to compile.

A release candidate version will be released after these new features become relatively stable and the breaking changes have been added. As with the alpha, developers will be able to test the release candidate version by specifying edition = "2024.rc" under the [package] section in their Move.toml files. 

Although the release candidate version will be more stable than the alpha, small breaking changes might occur in the future. Early this year, likely at the beginning of March, the compiler will ask developers if they would like to migrate to the release candidate when they build their projects. An automated tool will then migrate the project to the new edition, adding the edition marker in the Move.toml and automatically fixing all breaking changes.

Later this year, the 2024 Move update will advance from the release candidate and edition = "2024" will become the default for all new Move packages.