[swift-evolution] Subscripts assignable to closure vars

John McCall rjmccall at apple.com
Fri Sep 15 15:00:06 CDT 2017


> On Sep 15, 2017, at 3:45 PM, Joanna Carter via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Just came across this.
> 
> I want to be able to hold onto the reference to a subscript "method" for later use.
> 
> Assigning the subscript to a var in the init of a type raises a segmentation fault.
> 
> Should this - could this - be allowed?

It really shouldn't be allowed.  I think KeyPaths are the intended language solution here.

Please file a bug about the crash, though.

John.

> 
> protocol DataProvider
> {
>  associatedtype ItemType
> 
>  subscript(index: Int) -> ItemType { get }
> }
> 
> struct AnyDataProvider<providerType: DataProvider> : DataProvider
> {
>  private var _subscript: (Int) -> providerType.ItemType
> 
>  subscript(index: Int) -> providerType.ItemType
>  {
>    return _subscript(index)
>  }
> 
>  init<P : DataProvider>(_ base: P) where P.ItemType == providerType.ItemType
>  {
>    _subscript = base.subscript
>  }
> }
> 
> Joanna
> 
> --
> Joanna Carter
> Carter Consulting
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list