Why does RangeReplaceableCollection require an empty initialiser?


(Tim Vermeulen) #1

RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new) different protocol?


`init(capacity:)` on RangeReplaceableCollection
(Zachary Waldowski) #2

I have the same misgivings. The other day, I was wanted to add a piece
of metadata to a Slice type conforming to RangeReplaceableCollection
(coming from the containing collection) but couldn't figure out a way to
make it safe with the empty initializer. It's bugged me a few times
similarly.

Cheers!
Zachary Waldowski
zach@waldowski.me

路路路

On Wed, Jul 6, 2016, at 04:09 AM, Tim Vermeulen via swift-users wrote:

RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new) different
protocol?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Dave Abrahams) #3

RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile:
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.

This is an excellent point, and I think it may have been an oversight
that we made this a requirement. Please open a bug and/or file a radar.
https://bugs.swift.org/secure/Dashboard.jspa

Thanks!

路路路

on Wed Jul 06 2016, Tim Vermeulen <swift-users-AT-swift.org> wrote:

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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new)
different protocol?

--
Dave


(Zhao Xin) #4

According to the document of Swift 3, Array has already conformed
protocol RangeReplaceableCollection.

Zhaoxin

路路路

On Wed, Jul 6, 2016 at 7:09 PM, Tim Vermeulen via swift-users < swift-users@swift.org> wrote:

RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new) different
protocol?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Tino) #5

Hi there,

The requirement of the empty initialiser makes it impossible to have a collection conform to this protocol that needs additional data for its initialisation.

But if there is no guarantee for an empty initializer, you always need a piece of data to create an instance 鈥 and when you do heavy "metaprogramming" (generics, protocols鈥), it can become very hard to supply this additional data.
In those situations, it is very valuable when you can create an object out of thin air鈥

Tino


(Tim Vermeulen) #6

According to the document of Swift 3, Array has already conformed protocol RangeReplaceableCollection.

That鈥檚 exactly why I also want to conform my wrapper to that protocol? I think there鈥檚 a misunderstanding. I鈥檓 making a collection that can be subscripted with any index (that conforms to Strideable), but behaves like an array otherwise.

路路路

On 6 Jul 2016, at 14:03, Zhao Xin <owenzx@gmail.com> wrote:

Zhaoxin

On Wed, Jul 6, 2016 at 7:09 PM, Tim Vermeulen via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new) different protocol?
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Austin Zheng) #7

Would this require a swift-evolution review, since it's technically an API
change? (removing the initializer requirement is something I am also
interested in seeing...)

Austin

路路路

On Wed, Jul 6, 2016 at 7:05 PM, Dave Abrahams via swift-users < swift-users@swift.org> wrote:

on Wed Jul 06 2016, Tim Vermeulen <swift-users-AT-swift.org> wrote:

> RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile:
> 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.

This is an excellent point, and I think it may have been an oversight
that we made this a requirement. Please open a bug and/or file a radar.
https://bugs.swift.org/secure/Dashboard.jspa

Thanks!

> 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鈥檛 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鈥檚 three initialisers.
>
> Perhaps these initialisers should be moved to a (possible new)
> different protocol?

--
Dave

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Tim Vermeulen) #8

I thought this was it, but none of the default implementations of RangeReplaceableCollection seem to use this empty initialiser (except for the two other initialisers and `removeAll(keepingCapacity:)`, the latter of which can be implemented using `removeSubrange(_:)` instead). This makes me wonder whether this is the right protocol to put an empty initialiser.

路路路

On 6 Jul 2016, at 20:12, Tino Heth <2th@gmx.de> wrote:

Hi there,

The requirement of the empty initialiser makes it impossible to have a collection conform to this protocol that needs additional data for its initialisation.

But if there is no guarantee for an empty initializer, you always need a piece of data to create an instance 鈥 and when you do heavy "metaprogramming" (generics, protocols鈥), it can become very hard to supply this additional data.
In those situations, it is very valuable when you can create an object out of thin air鈥

Tino


(Zhao Xin) #9

Then how you defined the index to conform to Strideable? 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@me.com> wrote:

On 6 Jul 2016, at 14:03, Zhao Xin <owenzx@gmail.com> wrote:

According to the document of Swift 3, Array has already conformed
protocol RangeReplaceableCollection.

That鈥檚 exactly why I also want to conform my wrapper to that protocol? I
think there鈥檚 a misunderstanding. I鈥檓 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@swift.org> wrote:

RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.

Perhaps these initialisers should be moved to a (possible new) different
protocol?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Tim Vermeulen) #10

I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com(mailto:tvermeulen@me.com)>wrote:
>
> > On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com(mailto:owenzx@gmail.com)>wrote:
> > According to the document of Swift 3, Array has already conformed protocolRangeReplaceableCollection.
>
> That鈥檚 exactly why I also want to conform my wrapper to that protocol? I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org(mailto:swift-users@swift.org)>wrote:
> > > RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.
> > >
> > > Perhaps these initialisers should be moved to a (possible new) different protocol?
> > > _______________________________________________
> > > swift-users mailing list
> > > swift-users@swift.org(mailto:swift-users@swift.org)
> > > https://lists.swift.org/mailman/listinfo/swift-users
> >
>


(Dave Abrahams) #11

Yes.

路路路

on Wed Jul 06 2016, Austin Zheng <austinzheng-AT-gmail.com> wrote:

Would this require a swift-evolution review, since it's technically an API
change? (removing the initializer requirement is something I am also
interested in seeing...)

--
Dave


(Zhao Xin) #12

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 the
RangeReplaceableCollection
to be a non-RangeReplaceableCollection. So the empty collection must also
be a RangeReplaceableCollection.

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@me.com> wrote:

I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com(mailto: > tvermeulen@me.com)>wrote:
> >
> > > On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com(mailto: > owenzx@gmail.com)>wrote:
> > > According to the document of Swift 3, Array has already conformed
protocolRangeReplaceableCollection.
> >
> > That鈥檚 exactly why I also want to conform my wrapper to that protocol?
I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org(mailto:swift-users@swift.org)>wrote:
> > > > RangeReplaceableCollection has three initialisers: init(),
init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.
> > > >
> > > > Perhaps these initialisers should be moved to a (possible new)
different protocol?
> > > > _______________________________________________
> > > > swift-users mailing list
> > > > swift-users@swift.org(mailto:swift-users@swift.org)
> > > > https://lists.swift.org/mailman/listinfo/swift-users
> > >
> >
>
>
>


(Tino) #13

I thought this was it, but none of the default implementations of RangeReplaceableCollection seem to use this empty initialiser

I haven't worked with RangeReplaceableCollection yet, but I wasn't speaking of how the protocol is used in the libraries, but by "regular users":

class Factory<T: RangeReplaceableCollectionType> {
聽聽func create() -> T{
聽聽聽聽return T()
聽聽}
}

Doing something like this can become really hard when you need parameters.


(Tim Vermeulen) #14

You wouldn鈥檛 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@me.com(mailto:tvermeulen@me.com)>wrote:
> I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com(mailto:tvermeulen@me.com)(mailto:tvermeulen@me.com)>wrote:
> >>
> >>>On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com(mailto:owenzx@gmail.com)(mailto:owenzx@gmail.com)>wrote:
> >>>According to the document of Swift 3, Array has already conformed protocolRangeReplaceableCollection.
> >>
> >>That鈥檚 exactly why I also want to conform my wrapper to that protocol? I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org(mailto:swift-users@swift.org)(mailto:swift-users@swift.org)>wrote:
> >>>>RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.
> >>>>
> >>>>Perhaps these initialisers should be moved to a (possible new) different protocol?
> >>>>_______________________________________________
> >>>>swift-users mailing list
> >>>>swift-users@swift.org(mailto:swift-users@swift.org)(mailto:swift-users@swift.org)
> >>>>https://lists.swift.org/mailman/listinfo/swift-users
> >>>
> >>
> >
> >
> >


(Zhao Xin) #15

N
鈥媜. You didn't catch what I meant. I meant it should be like an equation.
鈥婭f foo is a
鈥婻angeReplaceableCollection,

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])

鈥媄haoxin

路路路

On Wed, Jul 6, 2016 at 10:07 PM, Tim Vermeulen <tvermeulen@me.com> wrote:

You wouldn鈥檛 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@me.com(mailto: > tvermeulen@me.com)>wrote:
> > I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com > (mailto:tvermeulen@me.com)(mailto:tvermeulen@me.com)>wrote:
> > >>
> > >>>On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com(mailto: > owenzx@gmail.com)(mailto:owenzx@gmail.com)>wrote:
> > >>>According to the document of Swift 3, Array has already conformed
protocolRangeReplaceableCollection.
> > >>
> > >>That鈥檚 exactly why I also want to conform my wrapper to that
protocol? I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org(mailto:swift-users@swift.org)(mailto: > swift-users@swift.org)>wrote:
> > >>>>RangeReplaceableCollection has three initialisers: init(),
init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.
> > >>>>
> > >>>>Perhaps these initialisers should be moved to a (possible new)
different protocol?
> > >>>>_______________________________________________
> > >>>>swift-users mailing list
> > >>>>swift-users@swift.org(mailto:swift-users@swift.org)(mailto:
swift-users@swift.org)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-users
> > >>>
> > >>
> > >
> > >
> > >
>
>
>


(Tim Vermeulen) #16

The same is true for protocols such as `RandomAccessCollection` and `MutableCollection`.

路路路

On 6 Jul 2016, at 22:07, Tino Heth <2th@gmx.de> wrote:

I thought this was it, but none of the default implementations of RangeReplaceableCollection seem to use this empty initialiser

I haven't worked with RangeReplaceableCollection yet, but I wasn't speaking of how the protocol is used in the libraries, but by "regular users":

class Factory<T: RangeReplaceableCollectionType> {
聽聽func create() -> T{
聽聽聽聽return T()
聽聽}
}

Doing something like this can become really hard when you need parameters.


(Tim Vermeulen) #17

I never said a RangeReplaceableCollection shouldn鈥檛 be empty. I just think it鈥檚 strange that it requires an empty initialiser (while the Collection protocol doesn鈥檛).

路路路

On 6 Jul 2016, at 16:33, Zhao Xin <owenzx@gmail.com> wrote:

N鈥媜. You didn't catch what I meant. I meant it should be like an equation. 鈥婭f foo is a 鈥婻angeReplaceableCollection,鈥 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])

鈥媄haoxin

On Wed, Jul 6, 2016 at 10:07 PM, Tim Vermeulen <tvermeulen@me.com <mailto:tvermeulen@me.com>> wrote:
You wouldn鈥檛 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 <http://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@me.com <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com <mailto:tvermeulen@me.com>)>wrote:
> > I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com <mailto:tvermeulen@me.com>(mailto:tvermeulen@me.com <mailto:tvermeulen@me.com>)(mailto:tvermeulen@me.com <mailto:tvermeulen@me.com>)>wrote:
> > >>
> > >>>On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com <mailto:owenzx@gmail.com>(mailto:owenzx@gmail.com <mailto:owenzx@gmail.com>)(mailto:owenzx@gmail.com <mailto:owenzx@gmail.com>)>wrote:
> > >>>According to the document of Swift 3, Array has already conformed protocolRangeReplaceableCollection.
> > >>
> > >>That鈥檚 exactly why I also want to conform my wrapper to that protocol? I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org <mailto:swift-users@swift.org>(mailto:swift-users@swift.org <mailto:swift-users@swift.org>)(mailto:swift-users@swift.org <mailto:swift-users@swift.org>)>wrote:
> > >>>>RangeReplaceableCollection has three initialisers: init(), init(_:slight_smile: 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鈥檛 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鈥檚 three initialisers.
> > >>>>
> > >>>>Perhaps these initialisers should be moved to a (possible new) different protocol?
> > >>>>_______________________________________________
> > >>>>swift-users mailing list
> > >>>>swift-users@swift.org <mailto:swift-users@swift.org>(mailto:swift-users@swift.org <mailto:swift-users@swift.org>)(mailto:swift-users@swift.org <mailto:swift-users@swift.org>)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-users
> > >>>
> > >>
> > >
> > >
> > >
>
>
>


(Zhao Xin) #18

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()
聽聽}

鈥婼o 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

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

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

Conforming chains(collection part only):

Array:
MutableCollection
鈥:
Collection:

Sequence

Array:
RandomAccessCollection:
鈥嬧
BidirectionalCollection:
鈥嬧
Collection

:

Sequence

A鈥媟ray:
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@me.com> wrote:

I never said a RangeReplaceableCollection shouldn鈥檛 be empty. I just think
it鈥檚 strange that it requires an empty initialiser (while the Collection
protocol doesn鈥檛).

On 6 Jul 2016, at 16:33, Zhao Xin <owenzx@gmail.com> wrote:

N
鈥媜. You didn't catch what I meant. I meant it should be like an equation.
鈥婭f foo is a
鈥婻angeReplaceableCollection,

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])

鈥媄haoxin

On Wed, Jul 6, 2016 at 10:07 PM, Tim Vermeulen <tvermeulen@me.com> wrote:

You wouldn鈥檛 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@me.com(mailto: >> tvermeulen@me.com)>wrote:
> > I鈥檓 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鈥檚 not really important. I鈥檓 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@me.com >> (mailto:tvermeulen@me.com)(mailto:tvermeulen@me.com)>wrote:
> > >>
> > >>>On 6 Jul 2016, at 14:03, Zhao Xin<owenzx@gmail.com(mailto: >> owenzx@gmail.com)(mailto:owenzx@gmail.com)>wrote:
> > >>>According to the document of Swift 3, Array has already conformed
protocolRangeReplaceableCollection.
> > >>
> > >>That鈥檚 exactly why I also want to conform my wrapper to that
protocol? I think there鈥檚 a misunderstanding. I鈥檓 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@swift.org(mailto:swift-users@swift.org)(mailto: >> swift-users@swift.org)>wrote:
> > >>>>RangeReplaceableCollection has three initialisers: init(),
init(_:slight_smile: 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鈥檛 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鈥檚 three
initialisers.
> > >>>>
> > >>>>Perhaps these initialisers should be moved to a (possible new)
different protocol?
> > >>>>_______________________________________________
> > >>>>swift-users mailing list
> > >>>>swift-users@swift.org(mailto:swift-users@swift.org)(mailto:
swift-users@swift.org)
> > >>>>https://lists.swift.org/mailman/listinfo/swift-users
> > >>>
> > >>
> > >
> > >
> > >
>
>
>