6. Fields and containers of owned values
An object owns its fields of tracked types. Freeing the object frees them recursively, after close() has run ("field glue"). An object that is part of an owned tree is never freed on its own, only together with its root.
@file:MemoryMode(Mode.Strict)
import mini.memory.*
// A node owns the rest of the list through its `next` field.
class Node(val v: Int, var next: Node?)
context(h: Heap)
fun push(list: @Owned Node?, v: Int): @Owned Node = Node(v, list) // moves `list` into the new node
fun length(n: Node?): Int {
var c = 0
@Borrowed var cur = n
while (cur != null) {
c++
cur = cur.next
}
return c
}
fun main() {
context(Heap()) {
var list: Node? = null
for (i in 1..5) list = push(list, i)
println(length(list))
list.free() // frees the head and, through its owned field, the whole chain
}
}Node(v, list) moves list into the new node's next field, and push returns the node as @Owned. The loop reassigns list each time, which is fine because the old value was moved into the parameter. length walks the list through a @Borrowed cursor. list.free() releases the head and, through the owned fields, the whole chain. The checker accepts this program.
Overwriting a field
Assigning to an owning field whose old content is unknown is a leak (M0102, path overwritten). Free the field or move out of it first.
Taking a value out of a field
@file:MemoryMode(Mode.Strict)
import mini.memory.*
class Box(val v: Int)
class Slot(var item: Box?)
// Taking an owned value out of a field is a partial move; the field must be restored.
fun swapOut(s: Slot): @Owned Box? {
val old = s.item.move()
s.item = null
return old
}
fun main() {
context(Heap()) {
val slot = Slot(Box(4))
val x = swapOut(slot)
println(x?.v)
x.free()
slot.free()
}
}s.item.move() is a partial move. The field is then in the moved state and must be valid again (a new value, or null) before any other call and before the function ends; otherwise M0113. Tracking goes one level deep (local.field).
Back references
@Borrowed var parent: Node? marks an unchecked back reference. Assignment is allowed and the compiler does not check the lifetime. Ownership is a tree, so graphs with cycles need an arena or @Borrowed links.
Collections of owned values (not supported yet)
Tracked values must not be put into Kotlin collections: list.add(box) is M0110. Types like List<@Owned T> and arrays of owned elements are planned. For now you build your own container class from owning fields, like the linked list above. Managed classes can still go into collections, as the Managed page shows.
Next: Lambdas and captures.