[swift-evolution] [Review] SE-0025 Scoped Access Level

Nate Cook natecook at gmail.com
Sun Feb 28 11:33:08 CST 2016


> On Feb 28, 2016, at 1:18 AM, Nate Cook via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Feb 26, 2016, at 9:42 PM, Stephen Celis <stephen.celis at gmail.com> wrote:
>> 
>>> On Feb 26, 2016, at 10:21 PM, Nate Cook via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> For example, one algorithm for rotating a collection uses an "unguarded" rotate as one of its steps, which is optimized to rotate a portion of the collection without bothering to check the inputs. It makes sense to extract this bit of the full rotation algorithm into a separate function, but it's unsafe to call unless very specific requirements are met that are unlikely outside the expected context. Being able to specify a local access level for the unguarded rotation would make this safer.
>> 
>> I'm curious if you could make the example more concrete and provide actual code that would benefit? One of the tough things about evaluating both SE-0025 and SE-0026 is that the proposals themselves contain very basic examples that don't sell the concepts.
> 
> Sure. you can see the rotate code here:
> 	https://gist.github.com/natecook1000/df70dd6a1e1aa1228d42
> 
> `unguardedRotate` is the method that would benefit from being locally scoped. It was even more fragile before the last refactor, but in its current form descends into an infinite loop if the parameter is either the startIndex or endIndex of the collection.

For another example, see this comment in the standard library, which says not to use the _variantStorage property, then scroll down about twenty lines to see it used:
	https://github.com/apple/swift/blob/master/stdlib/public/core/HashedCollections.swift.gyb#L432

As a reader of this file, what should be my interpretation here? (a) The init that uses _variantStorage just got put in the wrong place, (b) the init should've been written without using _variantStorage, or (c) the comment is outdated? Marking _variantStorage as scoped and closing the initial declaration block of the type would both communicate and enforce the actual intent.

Nate


More information about the swift-evolution mailing list