Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map expressions #1241

Open
anton-trunov opened this issue Dec 24, 2024 · 2 comments
Open

Map expressions #1241

anton-trunov opened this issue Dec 24, 2024 · 2 comments
Labels
language feature scope: maps The map datatype and operations on it scope: parser
Milestone

Comments

@anton-trunov
Copy link
Member

anton-trunov commented Dec 24, 2024

// or map<K, V> {
map {  
  k1: v1,
  ...
  [kn]: vn, // <- optional trailing comma
}

where k1, ..., kn -- key expressions, v1, ..., vn -- value expressions. If a simple literals and Identifiers are used as key expressions, we the syntax allows using them directly and if those are complex expressions, then the user needs to wrap those in square brackets (see an example below).

If all the key and value expressions can be computed at compile-time, it should translate into a constant cell primitive, a-la cell(...), otherwise we desugar something like let m: ... = map { [x + 1]: 42, [b]: y - 5, 24: 0 } as follows:

let m: ... = emptyMap();
m.set(x + 1, 42);
m.set(b, y - 5);
m.set(24, 0);
@anton-trunov anton-trunov added language feature scope: maps The map datatype and operations on it scope: parser labels Dec 24, 2024
@anton-trunov anton-trunov added this to the v1.6.0 milestone Dec 24, 2024
@verytactical
Copy link
Contributor

Reference

@novusnota
Copy link
Member

novusnota commented Dec 24, 2024

Reference

i.e. the current syntax of Struct and Message instantiations (instance expressions), but without the names.

To reduce ambiguity, I'd consider adding an explicit map<K, V> before, like:

let m = map<Int, Cell> {
    5: cell("..."),
    6: cell("..."),
};

Or maybe without <K, V>, since that can be inferred and because map is a reserved word:

let m: map<Int, Cell> = map {
    5: cell("..."),
    6: cell("..."),
};

And for the case of keys of Address type:

let m: <Address, Cell> = map {
    address("..."): cell("..."),
    address("..."): cell("..."),
    // or even this
    "EQ...": cell("..."),
};

Additionally, there's a question of whether this should still be considered a map<K, V> or Cell in the Tact's type system. I'd say this should be a map<K, V>, and if user wants to, they can add explicit .asCell() cast as such:

let c: Cell = map<Int, Cell> {
    5: cell("..."),
    6: cell("..."),
}.asCell();

@anton-trunov anton-trunov changed the title Map literals Map expressions Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language feature scope: maps The map datatype and operations on it scope: parser
Projects
None yet
Development

No branches or pull requests

3 participants