[swift-users] Protocol Arrays Adding vs. Instantiating
Paul Ossenbruggen
possen at gmail.com
Mon Jul 25 16:11:30 CDT 2016
Thanks to matt on stack overflow, you need to assign it to a temporary to
ensure the type is correct:
let w : H = K(v:3)
let x : H = J(v:3)
af += [w]
af += [x]
Is this worthy of a bug report? Or is it inherent in the design of the
language?
On Mon, Jul 25, 2016 at 1:02 PM, Paul Ossenbruggen <possen at gmail.com> wrote:
> In swift 3.0 beta 3, I defined a fairly simple protocol and two structs
> that implement it, if I initialize the array when creating the objects, it
> works, but if I try to add elements I get an error:
>
> Cannot convert value of type '[H]' to expected argument type 'inout _'
>
> Shouldn't this work?
>
> protocol H {
>
> var v : Int { get set }
>
> func hello()
>
> }
>
>
> struct J : H {
>
> var v : Int
>
> func hello() {
>
> print("j")
>
> }
>
> }
>
>
> struct K : H {
>
> var v : Int
>
> func hello() {
>
> print("k")
>
> }
>
> }
>
>
> let ag:[H] = [K(v:3), J(v:4)]
>
> ag[0].hello()
>
> ag[1].hello() //works
>
>
> var af:[H] = []
>
> af += [K(v:3)] // does not work
>
> af += [J(v:4)]
>
> af[0].hello()
>
> af[1].hello()
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160725/c15dc56f/attachment.html>
More information about the swift-users
mailing list