[swift-evolution] [Review] SE-0065 A New Model for Collections and Indices

Dave Abrahams dabrahams at apple.com
Tue Apr 19 13:37:26 CDT 2016


on Tue Apr 19 2016, Thorsten Seitz <tseitz42-AT-icloud.com> wrote:

> Yup, I just tested on scastie.org, extending my earlier example:
>
> class Coll(val elements: List[Int]) {
> case class Index(val value: Int)
> def firstIndex: Index = Index(0)
> def get(index: Index) = elements(index.value)
> def slice(start: Index) = new Slice(start.value)
> class Slice(val start: Int) {
> def firstIndex: Index = Index(start)
> def get(index: Index) = elements(index.value)
> }
> }
> object Main extends App {
> val a = new Coll(List(1, 2, 3))
> val b = new Coll(List(1, 2, 3))
> val i = a.firstIndex
> a.get(i)
> // b.get(i) // type error
> val s = a.slice(a.firstIndex)
> s.get(a.firstIndex) // allowed!
> val s2 = b.slice(b.firstIndex)
> s2.get(b.firstIndex)
> // s2.get(a.firstIndex) // type error
> val indices: List[a.Index] = List(a.firstIndex)
> }
>
> Note the last line declaring a list of indices of collection 'a'.

That's pretty cool.

> -Thorsten 
>
> Am 19.04.2016 um 10:01 schrieb Thorsten Seitz via swift-evolution
> <swift-evolution at swift.org>:
>
>         Am 18.04.2016 um 23:54 schrieb Dave Abrahams via swift-evolution
>         <swift-evolution at swift.org>:
>
>     on Sat Apr 16 2016, Thorsten Seitz <swift-evolution at swift.org> wrote:
>
>                         Am 15.04.2016 um 23:19 schrieb Dmitri Gribenko via
>                 swift-evolution <swift-evolution at swift.org>:
>
>                 On Fri, Apr 15, 2016 at 1:30 PM, Stephan Tolksdorf
>                     <st at quanttec.com> wrote:
>
>                                     On 2016-04-12 Dmitri Gribenko via
>                         swift-evolution wrote:
>
>                                     Not even to mention that
>
>                                     indices are valid only in context of a
>                         particular collection instance,
>
>                                     so in this model you could validate an index
>                         against one collection
>
>                                     and use it with another one.
>
>                 The proposal requires Index values to be Comparable. Does that
>                     mean that
>
>                 indices from different collection instances should be comparable
>                     i.e. have a
>
>                 strict total order?
>
>                         No, comparing indices from unrelated instances produces
>                 unspecified
>
>                         results (incl. traps).
>
>             Path dependent types as used in Scala would allow making this
>
>             distinction type safe (see
>
>             http://docs.scala-lang.org/tutorials/tour/inner-classes or
>
>             http://danielwestheide.com/blog/2013/02/13/the-neophytes-guide-to-scala-part-13-path-dependent-types.html
>            )
>
>             by allowing the index type to be rooted at the instance.
>
>     Wouldn't that also rule out useful designs, as in those where indices
>
>     into one collection are stored in another?
>
>     This should still be possible:
>
>     val a: Collection<T> = ...
>     val indices: List<a.Index> = ...
>
>     As 'a.Index' is just a normal type it should be possible to declare a
>     collection containing elements of just that type (I haven't tried it out,
>     yet, though, as I currently have no access to a development machine).
>
>     -Thorsten 
>     _______________________________________________
>     swift-evolution mailing list
>     swift-evolution at swift.org
>     https://lists.swift.org/mailman/listinfo/swift-evolution
>

-- 
Dave


More information about the swift-evolution mailing list