Swift's struct initialisers look like functions but don't behave as such
| Originator: | weissismail | ||
| Number: | rdar://19849369 | Date Originated: | 16-Feb-2015 07:55 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.1.1 (6A2008a) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
Swift's struct initialisers look like functions but don't behave as such.
Example:
Given struct S and a function sConstructorFun
struct S {
let a : String
let b : Int
let c : Bool
}
func sConstructorFun(a a:String, b b:Int, c c:Bool) -> S {
return S(a:a, b:b, c:c)
}
I can use both sConstructorFun(a:"", b:1, c:false) and S(a:"", b:1, c:false) to get the following S value (as the REPL outputs it)
S = {
a = ""
b = 1
c = false
}
So, S and sConstructorFun have the very same interface and unsurprisingly return the same result.
However, a sFactory function defined as follows
func sFactory(f:(String, Int, Bool) -> S) -> S {
return f("foo", 42, false)
}
can only be used with the sConstructorFun but not with S directly:
REPL> sFactory(sConstructorFun)
$R2: S = {
a = "foo"
b = 42
c = false
}
but
REPL> sFactory(S)
repl.swift:18:1: error: cannot invoke 'sFactory' with no arguments
sFactory(S)
^
repl.swift:18:9: note: expected an argument list of type '((String, Int, Bool) -> S)'
sFactory(S)
^
Steps to Reproduce:
- Save the attached file main.swift
- Run `xcrun swift main.swift`
Expected Results:
compiles all fine
Actual Results:
main.swift:18:9: error: cannot invoke 'sFactory' with no arguments
let d = sFactory(S) /* compile error */
^
main.swift:18:17: note: expected an argument list of type '((String, Int, Bool) -> S)'
let d = sFactory(S) /* compile error */
^
Regression:
no
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!
For OpenRadar only: main.swift
http://lpaste.net/120616