In Swift, a struct's default initializer breaks when assigning tuples default values
Originator: | atai.barkai | ||
Number: | rdar://21173355 | Date Originated: | 30-May-2015 03:52 PM |
Status: | Open | Resolved: | |
Product: | Developer Tools | Product Version: | Xcode Version 6.3.2 (6D2105) |
Classification: | Reproducible: | Always |
// copy/paste the following into a playground: /* This playground demonstrates an apparent bug in Swift. *** The playground does not run properly due to the bug. To let the playground run, comment-out the last line. *** When creating a struct, if it has properties that have their default values set by a tuple assignment (e.g. let (a, b) = (1, 2) ), then an explicit initializer must also be provided. Below are a few examples of similar structs that do not exhibit this bug. Especially note WorksAsExpected4, which demonstrates that an empty explicit initializer solves the problem, and WorksAsExpected5, which demonstrates that a class that looks exactly like the problematic struct does not exhibit the apparent bug. */ struct WorksAsExpected1{ let a = 1 let b = 2 } struct WorksAsExpected2{ let abTuple = (1, 2) } struct WorksAsExpected3{ let (a, b): (Int, Int) init(){ (a, b) = (1, 2) } } struct WorksAsExpected4{ // almost the same as the problem, but with an empty init() included let (a, b) = (1, 2) init(){} } class WorksAsExpected5{ // identical to the problematic struct, but a class rather than a struct let (a, b) = (1, 2) } struct DoesNotWorkAsExpected{ // the problem let (a, b) = (1, 2) } let obj1 = WorksAsExpected1() // works ok let obj2 = WorksAsExpected2() // works ok let obj3 = WorksAsExpected3() // works ok let obj4 = WorksAsExpected4() // works ok let obj5 = WorksAsExpected5() // works ok // Error: //'DoesNotWorkAsExpected' cannot be constructed because it has no accessible initializers let bad = DoesNotWorkAsExpected() // comment-out to let the playground run
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!