[swift-evolution] Pitch: Improved Swift pointers
Taylor Swift
kelvin13ma at gmail.com
Wed Jul 19 14:11:38 CDT 2017
I don’t really have a strong preference between either `repeating:` or `
value:`, this all kind of seems like bikeshedding to me tbh. If I had to
pick one, I’d pick `repeating:` though because that’s what `
Array.init(repeating:count:)` uses, even if it makes a lot of calls sound a
little weird. `ptr.initialize(repeating: value)` isn’t wrong
<http://www.macromeme.com/cat/boneless-chicken-eggs.jpg>, it just happens
to be repeated once.
More important I think is the immutable buffer pointer overloads for `
initialize(from:)` and `assign(from:)`. Currently the compiler doesn’t
recognize `UnsafeBufferPointer` as a subtype of `UnsafeMutableBufferPointer`.
Idk if it’s worth waiting for this to get added to the compiler, or just
cover it in the standard library.
On Wed, Jul 19, 2017 at 2:47 PM, Andrew Trick <atrick at apple.com> wrote:
>
> On Jul 19, 2017, at 11:13 AM, Taylor Swift <kelvin13ma at gmail.com> wrote:
>
> What about `value:`?
>
> `ptr.initialize(value: value)`
> `ptr.initialize(value: value, count: 13)`
> `ptr.initialize(as: UInt16.self, at: 0, value: value, count: 13)`
>
>
> Works for me if it’s reasonably consistent with the rest of the stdlib. I
> don’t know why `value` was avoided in the first place.
> -Andy
>
> On Wed, Jul 19, 2017 at 12:01 PM, Andrew Trick <atrick at apple.com> wrote:
>
>>
>> On Jul 18, 2017, at 9:42 PM, Taylor Swift <kelvin13ma at gmail.com> wrote:
>>
>> How do we feel about changing the label to `repeated:`, even in cases
>> where `count:` is 1?
>>
>> This would mean that calls would read like this: `ptr.initialize(repeated:
>> value)`
>>
>>
>> Right. Given the default `count` of 1, it’s probably best to stick with
>> `to: value`.
>> -Andy
>>
>>
>> A grep through the stdlib shows that this is by far the most common use
>> case:
>>
>> swift-source/swift$ grep initialize\(to: . -r
>> ./stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift:
>> result.initialize(to: block(arg))
>> ./stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift:
>> ptr.initialize(to: x)
>> ./stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift:
>> anyPointer.initialize(to: any)
>> ./stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift:
>> fn.initialize(to: ThickFunction0(function: function))
>> ./stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift:
>> fn.initialize(to: ThickFunction1(function: function))
>> ./stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift:
>> fn.initialize(to: ThickFunction2(function: function))
>> ./stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift:
>> fn.initialize(to: ThickFunction3(function: function))
>> ./stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift: (shards +
>> i).initialize(to: 0)
>> ./stdlib/public/SDK/Foundation/NSError.swift: out.initialize(to:
>> bridged)
>> ./stdlib/public/core/UnsafeRawPointer.swift.gyb: //
>> (self.assumingMemoryBound(to: T.self) + i).initialize(to: source[i])
>> ./stdlib/public/core/UnsafeRawPointer.swift.gyb: //
>> .initialize(to: (source + i).move())
>> ./stdlib/public/core/UnsafeRawPointer.swift.gyb: //
>> (--dst).initialize(to: (--src).move())
>> ./stdlib/public/core/Arrays.swift.gyb: p.initialize(to:
>> repeatedValue)
>> ./stdlib/public/core/Arrays.swift.gyb: (_buffer.firstElementAddress +
>> oldCount).initialize(to: newElement)
>> ./stdlib/public/core/Arrays.swift.gyb: p.initialize(to: x)
>> ./stdlib/public/core/Arrays.swift.gyb: p.initialize(to:
>> newValues[q])
>> ./stdlib/public/core/Arrays.swift.gyb: (base +
>> newCount).initialize(to: next)
>> ./stdlib/public/core/AnyHashable.swift: result.initialize(to: value)
>> ./stdlib/public/core/AnyHashable.swift: result.initialize(to:
>> value)
>> ./stdlib/public/core/AnyHashable.swift: target.initialize(to:
>> AnyHashable(value))
>> ./stdlib/public/core/ArrayCast.swift: target.initialize(to:
>> _arrayForceCast(source.pointee))
>> ./stdlib/public/core/ArrayCast.swift: target.initialize(to: result)
>> ./stdlib/public/core/ContiguousArrayBuffer.swift: (resultPtr +
>> i).initialize(to: _bridgeAnythingToObjectiveC(p[i]))
>> ./stdlib/public/core/ContiguousArrayBuffer.swift: p.initialize(to:
>> source[i])
>> ./stdlib/public/core/ContiguousArrayBuffer.swift: p.initialize(to:
>> element)
>> ./stdlib/public/core/ArrayBuffer.swift: result.initialize(to:
>> result.pointee)
>> ./stdlib/public/core/HashedCollections.swift.gyb: target.initialize(to:
>> _setDownCast(source.pointee))
>> ./stdlib/public/core/HashedCollections.swift.gyb:
>> target.initialize(to: result)
>> ./stdlib/public/core/HashedCollections.swift.gyb: target.initialize(to:
>> _dictionaryDownCast(source.pointee))
>> ./stdlib/public/core/HashedCollections.swift.gyb:
>> target.initialize(to: result)
>> ./stdlib/public/core/HashedCollections.swift.gyb: (keys +
>> i).initialize(to: k)
>> ./stdlib/public/core/HashedCollections.swift.gyb: (keys +
>> toEntryAt).initialize(to: (from.keys + at).move())
>> ./stdlib/public/core/HashedCollections.swift.gyb: (keys +
>> i).initialize(to: k)
>> ./stdlib/public/core/HashedCollections.swift.gyb: (values +
>> i).initialize(to: v)
>> ./stdlib/public/core/HashedCollections.swift.gyb: (keys +
>> toEntryAt).initialize(to: (from.keys + at).move())
>> ./stdlib/public/core/HashedCollections.swift.gyb: (values +
>> toEntryAt).initialize(to: (from.values + at).move())
>> ./stdlib/public/core/ReflectionLegacy.swift: out.initialize(to:
>> String(reflecting: x))
>>
>> ./stdlib/public/core/ManagedBuffer.swift:
>> p.headerAddress.initialize(to: initHeaderVal)
>> ./stdlib/public/core/ManagedBuffer.swift: $0.initialize(to:
>> ./stdlib/public/core/ArrayBufferProtocol.swift: (elements +
>> j).initialize(to: newValues[i])
>> ./stdlib/public/core/Sequence.swift: ptr.initialize(to: x)
>> ./stdlib/public/core/HeapBuffer.swift: $0.initialize(to:
>> Header(initializer))
>> ./stdlib/public/core/UnsafeBitMap.swift: values.initialize(to: 0,
>> count: numberOfWords)
>> ./stdlib/public/core/String.swift: resultStorage.initialize(to:
>> ./test/api-digester/stdlib-stable.json: "printedName":
>> "initialize(to:count:)",
>> ./test/Generics/slice_test.swift: (newbase + i).initialize(to:
>> (base+i).move())
>> ./test/Generics/slice_test.swift: (base+length).initialize(to: elem)
>> ./test/Parse/pointer_conversion.swift.gyb: p.initialize(to: t)
>>
>> ./test/stdlib/Reflection.swift:sanePointerString.initialize(to: "Hello
>> panda")
>>
>> ./test/stdlib/Builtins.swift: (buf + 0).initialize(to: value)
>> ./test/stdlib/Builtins.swift: (buf + 1).initialize(to: value)
>> ./test/stdlib/Renames.swift: x.initialize(e) // expected-error
>> {{'initialize' has been renamed to 'initialize(to:)'}} {{5-15=initialize}}
>> {{16-16=to: }} {{none}}
>> ./test/stdlib/Renames.swift: x.initialize(with: e) // expected-error
>> {{'initialize(with:count:)' has been renamed to 'initialize(to:count:)'}}
>> {{5-15=initialize}} {{16-20=to}} {{none}}
>> ./test/stdlib/Renames.swift: ptr1.initialize(with: e, count: 1) //
>> expected-error {{'initialize(with:count:)' has been renamed to
>> 'initialize(to:count:)'}} {{8-18=initialize}} {{19-23=to}} {{none}}
>> ./test/Sanitizers/asan.swift:a.initialize(to: 5)
>> ./validation-test/stdlib/UnsafeBufferPointer.swift.gyb:
>> allocated.initialize(to: 1.0, count: count)
>> ./validation-test/stdlib/UnsafeBufferPointer.swift.gyb:
>> allocated.initialize(to: 1.0, count: count)
>> ./validation-test/stdlib/StringSlicesConcurrentAppend.swift:
>> barrierVar!.initialize(to: _stdlib_pthread_barrier_t())
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (_keyVector(layout: layout) + keyCount).initialize(to: key)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (_valueVector(layout: layout) + keyCount).initialize(to: value)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (_valueArray + i).initialize(to: value)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> keyArray.initialize(to: key0)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (keyArray + 1).initialize(to: key1)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> valueArray.initialize(to: value0)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (valueArray + 1).initialize(to: value1)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (_keyArray + i).initialize(to: key)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (_valueArray(layout: layout) + i).initialize(to: value)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (keyArray + layout.keyCount - 1).initialize(to: appendKey)
>> ./validation-test/stdlib/Prototypes/PersistentVector.swift.gyb:
>> (valueArray + layout.keyCount - 1).initialize(to: appendValue)
>> ./validation-test/compiler_crashers_2_fixed/0004-rdar20564605.swift:
>> p.initialize(to: element)
>> ./validation-test/compiler_crashers_2_fixed/0003-rdar20564378.swift:
>> p.initialize(to: element)
>> ./validation-test/compiler_crashers_2_fixed/0006-rdar20588474.swift:
>> p.initialize(to: element)
>> ./validation-test/compiler_crashers_2_fixed/0010-rdar20638881.swift:
>> p.initialize(to: element)
>>
>>
>> On Tue, Jul 18, 2017 at 4:33 PM, Andrew Trick <atrick at apple.com> wrote:
>>
>>>
>>> On Jul 18, 2017, at 11:36 AM, Taylor Swift <kelvin13ma at gmail.com> wrote:
>>>
>>> > fix the ordering of the arguments in initializeMemory<Element>(as:a
>>>> t:count:to:)
>>>>
>>>> I think this ordering was an attempt to avoid confusion with binding
>>>> memory where `to` refers to a type. However, it should be consistent
>>>> with `UnsafePointer.initialize`, so we need to pick one of those to
>>>> change.
>>>>
>>>
>>> This would be a non-issue had we just been consistent with the rest of
>>> the stdlib and named this argument `repeating:` instead of `to:`. But `ptr.initialize(repeating:
>>> 255, count: 100)` doesn’t read quite as naturally in English as `ptr.initialize(to:
>>> 255, count: 100)` which is why I left this idea out of the proposal.
>>> Now that you mention the problem with `initializeMemory<Element>(as:
>>> at:count:to:)`, it might be a good idea to add this rename back into it.
>>>
>>>
>>> I think `repeating` is much more clear.
>>>
>>> -Andy
>>>
>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170719/21947d79/attachment.html>
More information about the swift-evolution
mailing list