[swift-users] Why does RangeReplaceableCollection require an empty initialiser?

Zhao Xin owenzx at gmail.com
Wed Jul 6 12:10:56 CDT 2016


I am not a software scientist. I have to explain things with examples. For
example, in Framework headers.

extension Array : RangeReplaceableCollection {
>     /// Creates a new, empty array.
>     ///
>     /// This is equivalent to initializing with an empty array literal.
>     /// For example:
>     ///
>     ///     var emptyArray = Array<Int>()
>     ///     print(emptyArray.isEmpty)
>     ///     // Prints "true"
>     ///
>     ///     emptyArray = []
>     ///     print(emptyArray.isEmpty)
>     ///     // Prints "true"
>     public init()


In Swift source code.

extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
>   /// Creates a new, empty array.
>   ///
>   /// This is equivalent to initializing with an empty array literal.
>   /// For example:
>   ///
>   ///     var emptyArray = Array<Int>()
>   ///     print(emptyArray.isEmpty)
>   ///     // Prints "true"
>   ///
>   ///     emptyArray = []
>   ///     print(emptyArray.isEmpty)
>   ///     // Prints "true"
>   @_semantics("array.init")
>   public init() {
>     _buffer = _Buffer()
>   }


​So I am confident to say, if there is no protocol limits on init() to be
empty. You can do what ever with Array<Int>(), maybe

l
> ​et foo = Array<Int>() // means [1] is OK​, as there is no limitation
> prohibits you to do so.
>

​So there always must be a protocol saying init() to be an empty
collection. We just don't know why it is put in ​
RangeReplaceableCollection.
​at the moment.​

Conforming chains(collection part only):

Array:
MutableCollection
​:
Collection:
​
Sequence
​
Array:
RandomAccessCollection:
​​
BidirectionalCollection:
​​
Collection
​
:
​
Sequence
​
A​rray:
RangeReplaceableCollection:
​
Collection​:
​
Sequence
​

For Sequence protocol, there is no mutating functions.
​
For Collection protocol, the mutating functions are removeFirst(),
removeFirst(Int), split(_:_:_) and  popFirst(). As you can imaging, an
empty collection can call none of the functions.

For MutableCollection, BidirectionalCollection, RandomAccessCollection, the
situations are similar. They just don't have extra mutating functions nor
an empty collection can't call these mutating functions.

For RangeReplaceableCollection protocol, below mutating function works in
an empty collection

func append<S>(contentsOf: S)

func removeAll(keepingCapacity: Bool)


Maybe that is why the must have init() is put here.

Zhaoxin​


On Thu, Jul 7, 2016 at 12:10 AM, Tim Vermeulen <tvermeulen at me.com> wrote:

> I never said a RangeReplaceableCollection shouldn’t be empty. I just think
> it’s strange that it requires an empty initialiser (while the Collection
> protocol doesn’t).
>
> On 6 Jul 2016, at 16:33, Zhao Xin <owenzx at gmail.com> wrote:
>
> N
> ​o. You didn't catch what I meant. I meant it should be like an equation.
> ​If foo is a
> ​RangeReplaceableCollection,
>> foo
>  minus
> foo
>  equates zero, zero means an empty collection. Both side of the equation
> should be with the same unit, the unit is
> RangeReplaceableCollection.
> ​ Below code also shows init() is useful in
> RangeReplaceableCollection.
> ​​
>
> var foo = Array<Int>()
>>
>> foo.append(contentsOf: [2,4,6,8])
>>
>
> ​Zhaoxin
>
> On Wed, Jul 6, 2016 at 10:07 PM, Tim Vermeulen <tvermeulen at me.com> wrote:
>
>> You wouldn’t need an empty initialiser to remove all elements from a
>> collection, right? You could just use `replaceRange` instead.
>>
>> > Now I understood you concerns. Have you ever thought of if a non-empty
>> RangeReplaceableCollection being removed all of its elements, which makes
>> the collection to be an empty collection. That shouldn't change
>> theRangeReplaceableCollection to be a non-RangeReplaceableCollection. Sothe
>> empty collection must also be aRangeReplaceableCollection.
>> >
>> > > init()
>> > (
>> file:///Users/zhaoxin/Library/Application%20Support/Dash/DocSets/Apple_API_Reference/Apple_API_Reference.docset/Contents/Resources/Documents/
>> developer.apple.com/reference/swift/rangereplaceablecollection/1641467-init.html)>
>> Creates a new, empty collection.
>> >
>> > Zhaoxin
>> >
>> > On Wed, Jul 6, 2016 at 9:09 PM, Tim Vermeulen<tvermeulen at me.com(mailto:
>> tvermeulen at me.com)>wrote:
>> > > I’m not allowing generic subscripts. The collection is declared as
>> `AnyIndexArray<Index: Strideable, Element where Index.Stride == Int>` and
>> it can be subscripted with type `Index`.
>> > >
>> > > Either way, it’s not really important. I’m mostly wondering why
>> RangeReplaceableCollection needs an empty initialiser.
>> > >
>> > > >Then how you defined the index to conform toStrideable? Below code
>> does work as it seams that you can't use generics in subscripts.
>> > > >
>> > > >
>> > > >subscript<T:Strideable>(index:T) ->Element
>> > > >
>> > > >
>> > > >
>> > > >
>> > > >
>> > > >
>> > > >Zhaoxin
>> > > >
>> > > >
>> > > >
>> > > >
>> > > >On Wed, Jul 6, 2016 at 8:32 PM, Tim Vermeulen<tvermeulen at me.com
>> (mailto:tvermeulen at me.com)(mailto:tvermeulen at me.com)>wrote:
>> > > >>
>> > > >>>On 6 Jul 2016, at 14:03, Zhao Xin<owenzx at gmail.com(mailto:
>> owenzx at gmail.com)(mailto:owenzx at gmail.com)>wrote:
>> > > >>>According to the document of Swift 3, Array has already conformed
>> protocolRangeReplaceableCollection.
>> > > >>
>> > > >>That’s exactly why I also want to conform my wrapper to that
>> protocol? I think there’s a misunderstanding. I’m making a collection that
>> can be subscripted with any index (that conforms to Strideable), but
>> behaves like an array otherwise.
>> > > >>
>> > > >>>
>> > > >>>Zhaoxin
>> > > >>>
>> > > >>>On Wed, Jul 6, 2016 at 7:09 PM, Tim Vermeulen via swift-users<
>> swift-users at swift.org(mailto:swift-users at swift.org)(mailto:
>> swift-users at swift.org)>wrote:
>> > > >>>>RangeReplaceableCollection has three initialisers: init(),
>> init(_:) and init(repeating:count:). The latter two are implemented using
>> the empty initialiser. But why are these initialisers part of this
>> particular protocol? As far as I can tell, no other methods of this
>> protocol depend on these initialisers. The requirement of the empty
>> initialiser makes it impossible to have a collection conform to this
>> protocol that needs additional data for its initialisation.
>> > > >>>>
>> > > >>>>For instance, I was making an array that works with any
>> Strideable indices, not just integers. A startIndex is needed for its
>> initialisation, so I can’t really conform it to RangeReplaceableCollection.
>> If I do it anyways (with a fatalError() in the required empty initialiser)
>> everything seems to work just fine, except for the protocol’s three
>> initialisers.
>> > > >>>>
>> > > >>>>Perhaps these initialisers should be moved to a (possible new)
>> different protocol?
>> > > >>>>_______________________________________________
>> > > >>>>swift-users mailing list
>> > > >>>>swift-users at swift.org(mailto:swift-users at swift.org)(mailto:
>> swift-users at swift.org)
>> > > >>>>https://lists.swift.org/mailman/listinfo/swift-users
>> > > >>>
>> > > >>
>> > > >
>> > > >
>> > > >
>> >
>> >
>> >
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160707/fff41624/attachment.html>


More information about the swift-users mailing list