Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2177] Array initialization using shorthand doesn't work with nested type #44785

Closed
swift-ci opened this issue Jul 26, 2016 · 0 comments
Closed

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2177
Radar None
Original Reporter kelan (JIRA User)
Type Improvement
Status Resolved
Resolution Duplicate
Environment

Xcode v8.0 beta 3 (8S174q)
Apple Swift version 3.0 (swiftlang-800.0.34.6 clang-800.0.33)
Target: x86_64-apple-macosx10.9

Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee None
Priority Medium

md5: 1e7ba604aeb3dad259864c2898eef77d

duplicates:

  • SR-773 Using Array initializer syntax with nested types causes bad error message

Issue Description:

The shorthand of [T] for Array<T> didn't seem to work when trying to initialize the array when T is a nested type.

Details

Given a class with a nested struct

class C {
    struct S {
        let title: String
    }
}

I want to use it from another class, but initializing this array property using the shorthand didn't work like I expected.

Here were a few steps that I went through (based on the error messages and Fixits):

Step 1:
I first tries this, to make a stored property that's initialized to the empty array.

class C2 {
    var state = [C.S]()  // <-- Error: Invalid use of '()' to call a value of non-function type '[UKUploadGroupTracker.UploadGroupInfo.Type]'
                         //     Fixit: Delete the ()
}

Step 2:
So, try the suggested Fixit, deleting the ().

class C2 {
    var state = [C.S]  // <-- Error: Expected member name or constructor call after type name
                       //     Fixit: add () inside the ]
}

Step 3:
Try that Fixit, adding the () inside the ].

class C2 {
    var state = [C.S()]  // <-- Error: Cannot invoke value of type 'C.S.Type' with argument list '()'
}

Step 4:
Use a different syntax to initialize the array. This does work.

class C2 {
    var state: [C.S] = []
}

Step 5:
Use another different syntax to initialize the array. This works also.

class C2 {
    var state = Array<C.S>()
}

Expect

I expected my first attempt in Step 1 to just be shorthand for Step 5, but Step 1 didn't work, and Step 5 did.

Notes

  • It doesn't work to do var state = [C.S]() inside a func in that class either.
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant