[swift-evolution] Subscripts assignable to closure vars

Joanna Carter joanna at carterconsulting.org.uk
Fri Sep 15 13:50:28 CDT 2017

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?

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 Carter
Carter Consulting

More information about the swift-evolution mailing list