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

Haravikk swift-evolution at haravikk.me
Thu Jun 9 13:05:27 CDT 2016


> On 9 Jun 2016, at 17:11, Dave Abrahams <dabrahams at apple.com> wrote:
> 
> 
> on Thu Jun 09 2016, Haravikk <swift-evolution-AT-haravikk.me <http://swift-evolution-at-haravikk.me/>> wrote:
> 
>>> On 8 Jun 2016, at 20:53, Dave Abrahams via swift-evolution
>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>>> on Wed Jun 08 2016, Haravikk <swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> <mailto: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?

Unfortunately I can’t actually seem to get the above protocol to work, Swift won’t accept the associated type on the generic constraints for the methods, it complains of it being a non-class/non-protocol type. These kinds of tricky generics are very much not a strength of my Swift programming abilities =)

The advantage of being able to declare a base type however is that it helps to disambiguate overloads. For example, consider unwrapping on AnyIndex using the following method:

	public struct AnyIndex : Comparable {
		public func unsafeUnwrap<T:Comparable>() -> T
	}

I then get myself an AnyIndex and decide I’d like to use it in a subscript like so:

	let value = myDictionary[myIndex.unsafeUnwrap()]

Since the unwrapped type is known to at least be of type Comparable, it’s obvious to the compiler which subscript I mean. If however no minimum conformance is provided (i.e- the above method is unsafeUnwrap<T>() -> T) then there are three possibilities (Index, Range and Key, since it’s a dictionary) so it produces an error.
It can still be used by assigning the unwrapped value to a variable with explicit type, but that’s a bit more verbose than I was hoping for.
Of course it can still fail if my AnyIndex isn’t wrapping a DictionaryIndex, but the extra type information at least prevents me from using the unwrapping somewhere that a Comparable can’t possibly be used.

So in my code this so far means I’m having to stick with adding the methods directly to my AnyIndex substitute rather than to a protocol, as I can put the restriction on it this way.

Like I say though, complex generic conformance is something I’m still ropey on (yet I seem to keep finding myself cases that need it) so it’s possible there’s a way to do this that I just can’t think of, or perhaps there are some features on their way that would enable this to work as I’m hoping?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160609/3ecf3cd7/attachment.html>


More information about the swift-evolution mailing list