[swift-evolution] [Pre-proposal/Question] Exposing the Unboxing Capabilities of AnyIndex (and similar types)

Dave Abrahams dabrahams at apple.com
Thu Jun 9 11:11:07 CDT 2016

on Thu Jun 09 2016, Haravikk <swift-evolution-AT-haravikk.me> wrote:

>> On 8 Jun 2016, at 20:53, Dave Abrahams via swift-evolution
> <swift-evolution at swift.org> wrote:
>>> on Wed Jun 08 2016, Haravikk <swift-evolution at swift.org
> <mailto:swift-evolution at swift.org>> wrote:
>>> But those shouldn't be the public names.  Perhaps s/box/wrap/ ?
> True! So I’m thinking I’ll try to come up with a basic proposal soon,
> I’m just thinking about how this would be implemented. For example, it
> may make sense to do this as a protocol that AnyIndex (and other
> suitable types) can just conform to like so:
> 	protocol Unwrappable {
> 		associatedtype UnwrappedType
> 		func unwrap<T:UnwrappedType>() -> T?
> 		func unsafeUnwrap<T:UnwrappedType>() -> T
> 	}
> I’ve kept the ability to specify a root type that unwrapping can
> produce, i.e- Comparable in the case of AnyIndex. Not too happy with
> the name of UnwrappedType, since it’s not intended to be the exact
> type in most cases, 

Examples please?

> not sure what would be a more appropriate name. 

We have traditionally used the names “Base” and “base” to describe the
name of a type that is being adapted by a wrapper and the property that
accesses the adapted instance.

> Also I lost track of the discussion about common root types between
> value and reference types; is there a type in Swift that could be used
> when unwrapping can produce absolutely anything (struct, enum, object
> etc.)? 

It's called “Any.”

> If not it may be better to drop the associated type and just lose the
> extra type-checking benefit.
> I’m still struggling to come up with other types that definitely need
> this, as all the other AnyFoo types I can think of expose
> functionality of the underlying type that you can use, so the need to
> unwrap them doesn’t really come up. But with a protocol defining this
> the capability will be there to expand this quickly to other types
> later.


More information about the swift-evolution mailing list