What does Collection.subscript._read do in Swift 5?

I'm trying out Swift 5 (beta) with Xcode 10.2 beta. The fill-in code when you start a member prototype for a Collection subscript give me:

public subscript(position: Base.Index) -> Base.Element {
    _read {
        return base[position]
    }
}

Where the "return base[position]" is the code I added. I got an error on that line: Cannot subscript a value of type 'Base' with an index of type 'Base.Index'. I got rid of that code and put the return statement in directly, which still seems to be an implicit get.

What is the difference between get and _read? Why are the requirements for _read different? Does it have to do with a modify tag I occasionally see in the library code?

3 Likes

I don't know if this is still up-to-date, as in this thread John McCall and others mentioned that __shared is slightly different than originally described in the manifesto, but the idea behind read is summed up there nicely:

Since read is not officially proposed yet, it's still prefixed with an underscore.


So instead of return you must yield, but I don't know if it's available in the beta or maybe as _yield, which is also not officially proposed yet.

It should not be suggesting _read at this time, and possibly ever.

1 Like
Terms of Service

Privacy Policy

Cookie Policy