Shortening array initializer with optional values ​​in Swift

I tried to initialize an array containing optional values ​​using the repeatValues ​​initializer, and I was surprised to find that the following code does not compile

let a: Int?[] = Int?[](count: 10, repeatedValue:nil)
// error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'?

Interestingly, the signature type Int?[]?, for example. optional Arrayoptional Int. This seems like a mistake, but maybe there is something missing for me in the grammar. I looked at the link to the language, but have not yet found the answer.

A more explicit type initializer Array<Int?>works as expected

let b: Int?[] = Array<Int?>(count: 10, repeatedValue:nil)
// compiles fine

Does anyone else come across this and can shed some light?

EDIT

A couple of additional working examples with optional types to highlight failure

let c: Int[] = Int[](count: 10, repeatedValue:0)
// non-optional shorthand works fine

class D { var foo = 1 }
let d: D[] = D[](count:10, repeatedValue:D())
// custom class works fine using the shorthand too

enum E { case a, b, c, d, e }
let e: E[] = E[](count:10, repeatedValue:.e)
// enums work too
+4
2

Swift 3:

   let pageViews = [UIImageView?](repeating: nil, count: pageCount)

Swift 2:

    let pageViews = [UIImageView?](count: pageCount, repeatedValue: nil)
+5

let a: Int?[] = Array(count: 10, repeatedValue:nil)

let a: Int?[] = Int?[](count: 10, repeatedValue:nil)

, Int?[] ?[] Int (, a?[1]) , , . .

let a: Int?[] = Optional<Int>[](count: 10, repeatedValue:nil)
0

Source: https://habr.com/ru/post/1543937/


All Articles