E1010: value cannot be moved out of a view or transferred with dat
A dat argument moves ownership into the callee (ADR-0016).
A dat argument moves ownership into the callee (ADR-0016). Only an owner can
be moved, so the argument must be an erg binding, a dat binding or an
erg field of such a binding, or an expression that produces a fresh owned
value (ADR-0129): a struct or pack literal, an enum constructor, an array
constructor such as Array[Int, 2](), a call whose declared result is owned
(not abs) or copy(value: abs x); an if or case of aggregate type is
not accepted as an argument, so bind it first. Nobody else holds such a value, so it needs no binding. An
abs binding is a view of someone else's value. A constructor such as
Buffer[4] has no binding to hand over. An ins binding is a loan and is
rejected with E1082. The message names the binding or field; for an argument
that is not a name it names the construct it rejects, such as a ref view or
the abs result of a call. The error points at the argument.
The same rule covers a field or an element read by value through an abs view
(ADR-0124). A struct, enum, pack, array or buffer taken from view.field,
view.a.b or view.items[0] and returned, bound to an erg, assigned,
passed to a dat parameter, or stored in a struct literal field or an enum
payload cannot be moved, because a view owns nothing, and it is not copied,
because nothing writes a copy, so it would be shared silently. The message names
the path. When the type is a copy type it says to write
copy(value: abs view.field); otherwise it says the value cannot leave the
view: borrow it with abs, or take ownership of the owner. Through an ins
loan the same read is E1082. A scalar read is not affected, nor is taking a view
of the field (abs h = ref view.field, an abs argument, -> abs).
An array element is always owned by its array: the language does not track
moves of single elements, so a struct, enum, pack or array (or a buffer) cannot
be read by value out of an element of an owner, at a constant or a run-time
index, or through a field of an element (xs[i], items[0].name), whether it
holds a resource or only plain data (ADR-0124). The array would still release
the element, so a value that holds a resource would be released twice
(ADR-0125), and nothing writes a copy, so plain data would be shared silently.
The rule covers a binding, an assignment, an element assignment, a struct field
initializer, an enum payload, an argument, a return and a branch value. The
message names the path. When the element type is a copy type it says to write
copy(value: abs xs[i]); otherwise borrow the element with abs, or assign a
new value to the element, which releases the value it held. A scalar element is
not affected.
Example
Fix
For a view field or an array element that is a copy type, write the copy. Otherwise borrow the
field, or let the caller own the value. Declare the forwarding parameter dat, or bind
the new value to an erg first and move that binding.
A fresh owned value needs no binding (ADR-0129):
Run actus explain E1010 in your terminal to read this entry offline. Browse Ownership & types or all error codes.