[swift-evolution] Making `.self` After `Type` Optional

Jordan Rose jordan_rose at apple.com
Wed Mar 16 13:28:29 CDT 2016

> On Mar 15, 2016, at 12:32, Tanner Nelson via swift-evolution <swift-evolution at swift.org> wrote:
>> On Mar 15, 2016, at 1:54 AM, Charles Constant <charles at charlesism.com <mailto:charles at charlesism.com>> wrote:
>> +1 for junking the .self requirement
>> On Wed, Mar 9, 2016 at 11:14 PM, David Hart via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> I strongly agree for the removal of .self. I remember it being a great source of confusion when I first learned Swift.
>> I had the same experience. It was very disorienting. 
>> Now that Dave and Joe have explained its origin as a safety feature, it makes sense. The problem is that there's no way to convey that to the end-user, so it seems like it has some deeper meaning. Then you start second-guessing yourself if there's an error in your code, etc.
> I agree. Swift's type-checking engine is strong enough to merit not needing the redundant `.self` safety check. It feels like I'm doing something wrong when I need to use `.self`.

I do want to note one additional case that will become ambiguous:

let x = [NSURL] // value of type 'Array<NSURL.Type>', or 'Array<NSURL>.Type' ?
let y = [NSURL.self] // definitely the former
let z = [NSURL]() // definitely the latter

I think we have to treat this as the former, and I think the rule is just "if the close bracket is immediately followed by an open paren, it's a type; otherwise it's a value". This definitely affects what `[NSURL].foo` means, too (is it a static or dynamic property?).

We could also restrict the array/dictionary shorthand syntax to type position, but it seems like it's fairly conventional to use "let x = [NSURL]()".


P.S. We actually have this problem today: does `NSURL?.self` have type 'Optional<NSURL.Type>' or 'Optional<NSURL>.Type' ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160316/60c6dd9c/attachment.html>

More information about the swift-evolution mailing list