among those with excellent pagerank, nobody really seems to know what the consuming
keyword does , at least in the position of a function parameter.
(in contrast, consuming
in the “__consuming
” position seems to be a bit better documented .)
today, i personally did not know why this doesn’t compile:
init(prefix:consuming [String])
{
if prefix.isEmpty
{
...
}
else
{
self.init(components: prefix)
}
}
unlike __owned
, the compiler doesn’t like that prefix
was used to check prefix.isEmpty
. that is the intended behavior of SE-0377 .
the explanation and prescribed pattern is in fact hidden deep inside SE-0377, it is the third example in the fifth code block under the Using parameter bindings with ownership modifiers subsection.
func baz(a: consuming String) {
// let aa = (a, a) // ERROR: attempt to copy `a`
let b = a
let bb = (b, b) // OK, b is implicitly copyable
}
suffice to say, that is probably not the first place you would think to look to answer the question “why doesn’t this compile?”
can’t we make it easier for people to learn how the ownership modifiers work?
9 Likes
jrose
(Jordan Rose)
October 11, 2023, 1:03am
2
This just seems like a bug to me. isEmpty
should not be a consuming method on a Collection, even though it is on a Sequence.
EDIT: and the error messages should make it clear that isEmpty
is declared to be consuming.
LATER EDIT: as noted below, I got this wrong; should have verified it! Everyone agrees it’s a bug, though.
9 Likes
Joe_Groff
(Joe Groff)
October 11, 2023, 3:32pm
3
From what I can see, it looks like isEmpty
is a requirement that starts on Collection
, and it isn't declared as consuming
there. So this is a bug.
9 Likes
The Array.isEmpty.getter
ABI is correct. The "bug" is that the consuming
and borrowing
keywords are not properly enforced.
8 Likes
i’m having a hard time following what is intended behavior and what isn’t. is Array.isEmpty
supposed to be consuming
or not?
1 Like
lorentey
(Karoy Lorentey)
October 12, 2023, 10:31pm
7
All of the following property getters are expected to work by borrowing self
:
Sequence.underestimatedCount
Collection.isEmpty
Collection.count
Collection.startIndex/endIndex
(If they consumed self
, then that would render them useless.)
10 Likes
For anyone following along, here is the pull request that updates "The Swift Programming Language" to discuss 'borrowing' and 'consuming':
apple:main
← tbkka:tbkka-borrow-consuming-parameters
opened 01:14AM - 10 Aug 23 UTC
This documents the `borrowing` and `consuming` parameter modifiers introduced by… [SE-0377][].
These are comparatively niche, so I'm only documenting them in the "Reference Manual" section for now.
I've structured this by:
* Creating a new "Parameter Modifiers" section with a brief description of the modifiers concept,
* Moving the old "In-Out Parameters" description under it, and
* Adding a new subsection on Borrowing and Consuming Parameters.
I've also adjusted the "In-Out Parameters" introduction so it starts with a code example showing the `inout` syntax and usage.
[SE-0377]: https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md
Fixes: rdar://116031853
3 Likes
Joe_Groff
(Joe Groff)
October 19, 2023, 1:08am
9
And this PR should fix the example from the OP:
apple:main
← jckarter:move-only-loadable-to-address-reabstraction
opened 01:06AM - 19 Oct 23 UTC
A set of fixes that allow for noncopyable values to be used successfully as the … base of a property projection in situations where the type is loadable, but the property implementation takes its parameter indirectly.
6 Likes