Home / Docs / Language guide / Expressions and control flow
take
What it does
From edition 0.2, a parameter declared take consumes its argument, which means the caller gives the value away: after f(take a), the caller cannot use a until an assignment gives it a new value. This lets the backend (the part of the compiler that produces the running program) reuse the memory of the value passed instead of copying it. That reuse is never observable, so the meaning stays that of values.
Learn more: Giving a value up: take.
module shop
pub kind Cart
items: Int
pub checkout(take cart: Cart) -> Int = cart.items
pub close(cart: Cart) -> Int
=
let count = checkout(take cart)
countForms
In the grammar below, each rule gives a name, =, what the name stands for and ;, quoted words are written as shown, NEWLINE, INDENT and DEDENT stand for a line break and for one more or one less level of indentation, [ ] marks an optional part, { } a part that may repeat, | separates alternatives, ( ) groups them, and (* *) holds a comment.
param = [ "edit" | "take" ] lower_ident ":" type ;
arg = [ lower_ident ":" ] ( "edit" place | "take" lower_ident | line_expr ) ;take x: Tin a parameter list.take aat the matching argument, whereais a binding, not a field.
Examples
A let edit binding can be given a new value after it was taken, and is usable again:
module shop
pub ship(take items: List[Int]) -> Int = items.len
pub ship_and_refill(items: List[Int]) -> Int
=
let edit stock = items
let shipped = ship(take stock)
stock = [1, 2]
stock.len + shippedUsing a binding after it was taken is rejected:
-- fails with U2017
module shop
pub ship(take items: List[Int]) -> Int = items.len
pub ship_twice(items: List[Int]) -> Int
=
let shipped = ship(take items)
items.len + shippedEdge cases
The marker is required:
-- fails with U2020
module shop
pub ship(take items: List[Int]) -> Int = items.len
pub ship_unmarked(items: List[Int]) -> Int = ship(items)A binding, not a field:
-- fails with U2020
module shop
pub kind Order
items: List[Int]
pub ship(take items: List[Int]) -> Int = items.len
pub ship_order(order: Order) -> Int
=
let shipped = ship(take order.items)
shippedFurther rules:
- Branches and loops. A binding taken in one branch of an
ifor awhencounts as taken after it. A binding taken in a loop body is taken in every later iteration, so it is assigned before the end of the body, or declared inside it (U2017). - Any local binding can be taken: a plain
let, alet editor a parameter. Only an assignment revives it, so a plain binding stays taken. - Exclusivity. Another use of a binding after its
take, even in the same call, is U2017. Ataketogether with aneditof the same binding in one call is U2018. - Not a value. A function with a
takeparameter can only be called (U2021). - Not on operations. Capability operations and handlers cannot declare
takeparameters (U2020).
Diagnostics
- U0103:
takein edition0.1, where it is reserved. - U2017: a binding used after it was taken, in a later line or later in the same call.
- U2018: a
taketogether with aneditof the same binding in one call. - U2020: a missing or extra
takemarker, or a field given totake. - U2021: a function with a
takeparameter used as a value.
Edition
take exists from edition 0.2 on. In edition 0.1 it is reserved:
-- edition 0.1
-- fails with U0103
module shop
pub ship(take items: List[Int]) -> Int = items.lenNormative source
- spec/17 §1: syntax.
- spec/17 §3: parameter conventions, rules 1 to 8.
- spec/17 §7 rule 3: backend freedom.
- Decision record: ADR 0056, for the memory model of edition
0.2.