# Protocol Arrays Adding vs. Instantiating

**URL:** <https://forums.swift.org/t/protocol-arrays-adding-vs-instantiating/3537>\
**Category:** Using Swift\
**Created:** [July 25, 2016, 8:02pm UTC](https://forums.swift.org/t/protocol-arrays-adding-vs-instantiating/3537 "2016-07-25T20:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 25, 2016, 8:02pm UTC](https://forums.swift.org/t/protocol-arrays-adding-vs-instantiating/3537/1 "2016-07-25T20:02:51Z")

</div>

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 {

&nbsp;&nbsp;&nbsp;&nbsp;var v : Int { get set }

&nbsp;&nbsp;&nbsp;&nbsp;func hello()

}

struct J : H {

&nbsp;&nbsp;&nbsp;&nbsp;var v : Int

&nbsp;&nbsp;&nbsp;&nbsp;func hello() {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("j")

&nbsp;&nbsp;&nbsp;&nbsp;}

}

struct K : H {

&nbsp;&nbsp;&nbsp;&nbsp;var v : Int

&nbsp;&nbsp;&nbsp;&nbsp;func hello() {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("k")

&nbsp;&nbsp;&nbsp;&nbsp;}

}

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()

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [July 25, 2016, 9:11pm UTC](https://forums.swift.org/t/protocol-arrays-adding-vs-instantiating/3537/2 "2016-07-25T21:11:30Z")

</div>

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 +=

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@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 {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var v : Int { get set }
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func hello()
> > 
> > }
> > 
> > struct J : H {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var v : Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func hello() {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("j")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > }
> > 
> > struct K : H {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var v : Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func hello() {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("k")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > }
> > 
> > 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()

---

<div class="post-metadata">

**Author:** ![Team\_Nera](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@Team\_Nera](https://forums.swift.org/u/Team_Nera)\
**Post date:** [July 26, 2016, 12:04am UTC](https://forums.swift.org/t/protocol-arrays-adding-vs-instantiating/3537/3 "2016-07-26T00:04:12Z")

</div>

Hi,  
here are a few other ways that will compile:

af = af + [J(v:3)]  
af += [J(v:4)] as [H]  
af += [K(v:5)].map{ $0 as H }  
af.append(K(v:6))

It does seem that "+=" does not trigger the inference of the type of the array literal the way "+" does. I don't know if that's a bug or one of those situations where you need to help the type system, the error seems a little bit too cryptic though.

It is by design that swift does not convert an array of concrete type to an array of protocol (it can take a long time), whereas single values are (like "w" and "x"). As I understand it, it's only because of the type inference on the array literal that its items are converted in the first two examples.

Nera

> **···**
>
> Le 25/07/2016 à 23:11, Paul Ossenbruggen via swift-users a écrit :
> 
> > 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 +=
> > 
> > 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@gmail.com \> \<mailto:possen@gmail.com\>\> wrote:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;In swift 3.0 beta 3, I defined a fairly simple protocol and two  
> > &nbsp;&nbsp;&nbsp;&nbsp;structs that implement it, if I initialize the array when creating  
> > &nbsp;&nbsp;&nbsp;&nbsp;the objects, it works, but if I try to add elements I get an error:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Cannot convert value of type '[H]' to expected argument type 'inout \_'
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Shouldn't this work?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;protocol H {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var v : Int { get set }
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func hello()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct J : H {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var v : Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func hello() {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("j")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;struct K : H {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var v : Int
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func hello() {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("k")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;let ag:[H] = [K(v:3), J(v:4)]
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;ag[0].hello()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;ag[1].hello() //works
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var af:[H] =
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;af += [K(v:3)] // does not work
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;af += [J(v:4)]
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;af[0].hello()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;af[1].hello()
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
