Parsing types inside and outside of expressions


I wanted to ask why I'm able to do this:

let x = [Int].Element.self

But not this:

let x: [Int].Element = 1

Seems inconsistent to me

Also, this doesn't work either (unlike the array example), introducing even more inconsistency:

let x = Int?.Wrapped.self

Is this deliberate or a bug, and if it's deliberate, what's the rationale behind all of this?

Thank you in advance for your answers

(Nobody1707) #2

That is bizarre. Made even more so by the fact that this works:

let x: Array<Int>.Element = 1

The grammar for types outside of expressions is actually quite clear on this (

Although I agree that it should be possible, it's acceptable that it indeed isn't and only nominal types ("type identifiers") partake in the . syntax.

I just don't quite understand how types are parsed inside of expressions, since it doesn't seem to completely follow those rules.

(Adrian Zubarev) #4

This is not a bug, but a historical hole in the language, that we 'potentially can fix'.

This works because Element is an associated type from a protocol that Array conforms to which exposes the type.

That does not work because Swift currently does not allow 'qualified lookup' in this context.

let x: Optional<Int>.Wrapped = ... // will error out

Please check this discussion: Allow Member Lookup to Find Generic Parameters

(David Zarzycki) #5

Based on the diagnostics output, I feel fairly confident that this is just a bug. Have you tried filing a bug report?