# \`borrow\` and \`take\` parameter ownership modifiers

**URL:** <https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581>\
**Category:** Pitches\
**Created:** [August 11, 2022, 1:43am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581 "2022-08-11T01:43:12Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [August 11, 2022, 5:21pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/21 "2022-08-11T17:21:02Z")

</div>

Thinking about it a bit, if we say that a `take` parameter is bound to a `var` internally, then it shouldn't be immediately source-breaking to the implementation, since it wouldn't be able to modify the unannotated argument previously. That would make it so that removing `take` or changing it to `borrow` might break the implementation source if it modifies the taken parameter, necessitating an explicit local copy, but that change is at least localized to the implementation already being modified. So it may be a reasonable default to have it so that `take` parameters are internally mutable.

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [August 11, 2022, 5:21pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/22 "2022-08-11T17:21:29Z")

</div>

I guess the trick is going to be how to reconcile inout variable semantics with the new keyword so that mutability is somewhat similar.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [August 11, 2022, 7:05pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/23 "2022-08-11T19:05:25Z")

</div>

> [@xwu](#):
>
> I think clearly the answer is to call these `yoink` and `yeet`.

SHIP IT.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [August 11, 2022, 8:04pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/24 "2022-08-11T20:04:35Z")

</div>

> [@Karl](#):
>
> I'll tell you what feels the most awkward to me - that these are verbs, not adjectives.
> 
> Having `borrow String` or `take any Collection<Int>` in parameter lists just feels weird. Like I'm writing a recipe, not a declaration. What do we call them? "Take parameters"?

Another reason we might not want to use the exact same keywords is user confusion. The difference between putting `take` on a parameter and `take` on an argument is a little tricky to explain, and using precisely the same word for both probably won’t help.

Ideally I think we might want to say something like `takeable` and `borrowable` on the parameters and then `take` and `borrow` on the arguments; using different but closely related words communicates which keywords match which, while the `-able` suffix roughly conveys the relationship between the parameter and the argument (expressing the capability vs. actually using it).

```swift
func foo(_: takeable Foo) { ... }

foo(take myFoo)

```

However, the specific words `take` and `borrow` read a little awkwardly with the `-able` suffix, so I’m not married to those exact choices.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [August 11, 2022, 8:52pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/25 "2022-08-11T20:52:19Z")

</div>

> [@beccadax](#):
>
> Another reason we might not want to use the exact same keywords is user confusion. The difference between putting `take` on a parameter and `take` on an argument is a little tricky to explain, and using precisely the same word for both probably won’t help.
> 
> Ideally I think we might want to say something like `takeable` and `borrowable` on the parameters and then `take` and `borrow` on the arguments; using different but closely related words communicates which keywords match which, while the `-able` suffix roughly conveys the relationship between the parameter and the argument (expressing the capability vs. actually using it).

Hm, I like that using the same word makes the connection explicit. The `-able` suffix, aside from opening an English spelling argument every time (is it "takable" or "takeable"), also doesn't seem appropriate once move-only values factor into the equation. When you take implicit copying away, the parameter not just _can_ take the argument, it _must_.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [August 11, 2022, 8:54pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/26 "2022-08-11T20:54:39Z")

</div>

I actually like using the same word just fine, but thinking this through, wouldn't the logical pairing be give/take rather than take/takeable?

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [August 11, 2022, 8:58pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/27 "2022-08-11T20:58:11Z")

</div>

I like `own` instead of `take`, so wouldn't this be `own / borrow` and `ownable / borrowable` or `owned / borrowed`? Personally I don't think the correctness of the grammar is the issue here, so a single set of modifiers would be fine to me.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 11, 2022, 10:39pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/28 "2022-08-11T22:39:55Z")

</div>

> [@David\_Smith](#):
>
> I actually like using the same word just fine, but thinking this through, wouldn't the logical pairing be give/take rather than take/takeable?

And along these lines, if we don't want to use the same word, the other pair would naturally be "lend/borrow," which works out nicely.

---

<div class="post-metadata">

**Author:** ![asdf](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@asdf](https://forums.swift.org/u/asdf)\
**Post date:** [August 11, 2022, 11:33pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/29 "2022-08-11T23:33:09Z")

</div>

replace inout with &mut  
replace borrow with &

or replace inout with &  
(& is already used to get ref to object and pass it as inout param into func call)  
and replace borrow with &const

for e.g. instead of this  
`func test(_ arg1: inout Int, _ arg2: borrow Int)`  
will be this  
`func test(_ arg1: & Int, _ arg2: &const Int)`

This leads to breaking change, so make the change in swift 6

---

<div class="post-metadata">

**Author:** ![toph42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toph42/32/7807_2.png) [@toph42](https://forums.swift.org/u/toph42)\
**Post date:** [August 12, 2022, 6:21am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/30 "2022-08-12T06:21:14Z")

</div>

> [@Joe\_Groff](#):
>
> After the SE-0366 discussion, I think there's a lot of benefit to having the name of the "move operator" align with the "take" modifier, so we'd call it `take x` instead.

This sure is confusing. On that discussion @beccadax wrote that `move` and ` __owned` were not synonymous. Are you saying now that they are? This pitch wants to rename `__ owned` to `take` and you want `move` to _also_ be `take`? I’m so confused.

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [August 12, 2022, 6:50am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/32 "2022-08-12T06:50:13Z")

</div>

Parameter Modifier:  
mutable borrow (today inout)  
immutable borrow ( today \_\_owned)  
mutable take (todo future)  
immutable take (today \_\_consuming)

Operator:  
mutable borrow (&x ) // inout today  
immutable borrow ( borrow x )  
mutable take (todo future)  
immutable take (take x) // previously proposed as move x

---

<div class="post-metadata">

**Author:** ![toph42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toph42/32/7807_2.png) [@toph42](https://forums.swift.org/u/toph42)\
**Post date:** [August 12, 2022, 6:52am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/33 "2022-08-12T06:52:58Z")

</div>

How does `move` fit in this picture?

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [August 12, 2022, 6:54am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/34 "2022-08-12T06:54:43Z")

</div>

take operator was previously proposed as move x

---

<div class="post-metadata">

**Author:** ![jayton](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jayton/32/5592_2.png) [@jayton](https://forums.swift.org/u/jayton)\
**Post date:** [August 12, 2022, 9:19am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/35 "2022-08-12T09:19:43Z")

</div>

> [@masters3d](#):
>
> mutable borrow (today inout)  
> immutable borrow ( today \_\_owned)  
> mutable take (todo future)  
> immutable take (today \_\_consuming)

The mutable/immutable distinction that exists between `inout` and ` __owned`/`borrow` has no equivalent for `__ consuming`/`take`, and presenting it as some sort of matrix is not helpful. If you take ownership of a value, any mutations are local and unobservable, so whether or not they happen shouldn’t be part of the function’s type.

The question of whether `take` variables should be implicitly `var`, or reintroducing `var`-parameter syntax, is a minor quality-of-life issue that isn’t semantically important the way `inout` is.

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [August 12, 2022, 4:50pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/36 "2022-08-12T16:50:19Z")

</div>

> [@jayton](#):
>
> The question of whether `take` variables should be implicitly `var`, or reintroducing `var`-parameter syntax, is a minor quality-of-life issue that isn’t semantically important the way `inout` is.

Yes, you are right that pass-by-move (take) should not focused too much on immutable vs mutable axis since once something is move/take into a function then that value is consumed but the way I think about it is if we pass-by-move to a let parameter then that is immutable. If we pass-by-move to a var (implicit) parameter then that is mutable.

If I have full ownership of a value then I should be able to mutate it without rebinding it so I guess I am proposing implicit var for move/take.

> [@Joe\_Groff](#):
>
> Without any additional language features beyond what's proposed, note that you could still do this to put your taken parameter in a locally mutable variable:
> 
> ```swift
> func foo(x: take [Int]) {
> var myX = take x
> modifyInPlace(&myX)
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [August 12, 2022, 6:26pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/37 "2022-08-12T18:26:11Z")

</div>

> [@toph42](#):
>
> This sure is confusing. On that discussion @beccadax wrote that `move` and ` __owned` were not synonymous. Are you saying now that they are? This pitch wants to rename `__ owned` to `take` and you want `move` to _also_ be `take`? I’m so confused.

They aren't synonymous per se, but you can look at them as opposite ends of the same ownership transfer process. Declaring a function with a `take` parameter like `func foo(x: take T)` makes it so that the function takes ownership of (possibly a copy of) the argument. Calling the function with `foo(x: take x)` ensures that the argument is taken without copying.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [August 12, 2022, 8:01pm UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/38 "2022-08-12T20:01:27Z")

</div>

> [@Joe\_Groff](#):
>
> Calling the function with `foo(x: take x)` ensures that the argument is taken without copying.

This implies an error if the function declares parameter `x` as `borrow`, right? The other possibility is that it ends the lifetime of variable `x` immediately after the call stops borrowing it, which I think is what `move x` would have done in the earlier proposal. I think the distinction is important. Call-side `take` is anchored at the call site and must align with the function declaration, whereas `move` can occur anywhere regardless of whether it makes sense in relation to the function call.

Any thought about operators? Can I do `(take x)*(take y)`?

Or auto-closures? Would `assert(false, take x) ` make any sense to avoid making a copy of `x`? It could probably work the way `move` was defined, but not with `take` as an argument modifier. Am I correct?

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [August 13, 2022, 1:14am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/39 "2022-08-13T01:14:40Z")

</div>

> [@michelf](#):
>
> The other possibility is that it ends the lifetime of variable `x` immediately after the call stops borrowing it, which I think is what `move x` would have done in the earlier proposal. I think the distinction is important.

Yes, that design is the most straightforward and consistent with the source compatibility goals. For copyable types, that _will_ end up passing a copy of the variable to the callee. Some have objected to this behavior though because it probably wasn't the author's intention.

In the simplest interpretation, `take` just means that this expression takes ownership of the named variable. It works just as well with operators or as a stand-alone statement, which is an important use case. That's why "give" doesn't make sense. You're not naming a recipient.

---

<div class="post-metadata">

**Author:** ![toph42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toph42/32/7807_2.png) [@toph42](https://forums.swift.org/u/toph42)\
**Post date:** [August 13, 2022, 5:27am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/40 "2022-08-13T05:27:19Z")

</div>

> [@Andrew\_Trick](#):
>
> That's why "give" doesn't make sense. You're not naming a recipient.

In the case of decorating a parameter at the call site, isn’t the caller giving to the callee that’s taking it?

```swift
//in the case of give/take
func foo(take x: Int) { … }

var bar = 0

foo(x: give bar)

```

You are giving `bar` to `foo`. That’s the recipient, right?

I feel like the `give`/`take` dichotomy (and the `lend`/`borrow` one) make a lot more sense. (Though again it makes `inout` and `&x` look really out of place.)

---

<div class="post-metadata">

**Author:** ![rdemarest](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rdemarest/32/3057_2.png) [@rdemarest](https://forums.swift.org/u/rdemarest)\
**Post date:** [August 13, 2022, 7:03am UTC](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581/41 "2022-08-13T07:03:43Z")

</div>

How come you can conform to a protocol but use different take/borrow ownership than what the protocol says? You can't do that with the existing inout as far as I know.

[Previous page](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581.md?page=1)

[Next page](https://forums.swift.org/t/borrow-and-take-parameter-ownership-modifiers/59581.md?page=3)
