From 6daa3023dc0a4b1bfe7b90f08ae0ee3358be579f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Mar 2025 18:49:04 +0000 Subject: Add notes on explicit mutation --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index b803d116..ec016979 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,23 @@ var message = "hello, "; set! message = message! + "world\n"; print!(message); ``` + +Explicit heap / mutation context? + +``` +native type MutableArray[!M <: Mutate, T] { + native fun size(This) -> Int; + + // Precondition: unsafeSet has previously been called with the same index. + native fun unsafeGet(This, Int) ! M -> T; + + // Precondition: the index is in the range [0, size). + native fun unsafeSet(This, Int, T) ! M -> Unit; +} +``` + +Issue: without linear types, no way of knowing whether there are multiple +references to the mutable array, therefore building the array within a list +builder requires either a new, immutable array to be constructed, or for the +builder to mark its internal array as having been built, and therefore a copy +should be created on the next append. -- cgit v1.2.3