Skip to content

Support simpler array update syntax #2378

Closed
@swernli

Description

@swernli

A major pain point for folks learning Q# (or AI generating it) is the copy-update syntax used for modifying arrays:

mutable a = [1, 2, 3];
let idx = 0;
let update = 0;
a w/= idx <- update; // Sets the first entry to be `0`

We should consider a simpler syntax that is more intuitive while either conforming to current memory copying patterns or introducing new patterns.

One possible solution: we introduce new syntactic suger that is lowered into the same hir::ExprKind::AssignIndex structure such that runtime behavior and analysis via Runtime Capabilities Analysis remains the same. We could support familiar syntax like:

mutable a = [1, 2, 3];
let idx = 0;
let update = 0;
a[idx] = update; // Sets the first entry to be `0`

However, we'd have to be careful about how to handle different forms of indexing (like range-based updates) and arrays of arrays. So something like:

mutable a = [ [1, 2, 3], [1, 2, 3] ];
a[0][0] = 0;

would have to be treated as the equivalent of the older syntax:

mutable a = [ [1, 2, 3], [1, 2, 3] ];
a w/= 0 <- (a[0] w/ 0 <- 0);

Where the older syntax more clearly communicates that a copy of the entire inner array is incurred.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions