# Extending generic type where generic parameter conforms to a protocol

**URL:** <https://forums.kodeco.com/t/extending-generic-type-where-generic-parameter-conforms-to-a-protocol/183905>\
**Category:** Expert Swift\
**Created:** [February 13, 2023, 8:07am UTC](https://forums.kodeco.com/t/extending-generic-type-where-generic-parameter-conforms-to-a-protocol/183905 "2023-02-13T08:07:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![allanspreys](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/allanspreys/32/105896_2.png) [@allanspreys](https://forums.kodeco.com/u/allanspreys)\
**Post date:** [February 13, 2023, 8:07am UTC](https://forums.kodeco.com/t/extending-generic-type-where-generic-parameter-conforms-to-a-protocol/183905/1 "2023-02-13T08:07:40Z")

</div>

Hi Team,

I’ve got a two part question. Let’s say we have two protocols and then we have two structs to conform to both of these protocols.

```auto
protocol SomeProtocol {
  func someMethod() -> String
}

protocol AnotherProtocol {
  func anotherMethod() -> String
}

struct StructA: SomeProtocol, AnotherProtocol {
  func someMethod() -> String {
    "I am struct A"
  }

  func anotherMethod() -> String {
    "I am struct A"
  }
}

struct StructB: SomeProtocol, AnotherProtocol {
  func someMethod() -> String {
    "I am struct B"
  }

  func anotherMethod() -> String {
    "I am struct A"
  }
}

```

I’d like to create an extension on the array where all elements of the array conform to both of these protocols:

```auto
extension Array where Element: SomeProtocol, Element: AnotherProtocol {
  func printSomeAndAnother() {
    self.forEach {
      print($0.someMethod())
      print($0.anotherMethod())
    }
  }
}

```

So far so good, all of the above code is valid Swift that compiles.  
Then, I’d like to create an array of elements that conform to both of these protocols and call the `printSomeAndAnother()` method on this array, I was expecting the code below to work:

```auto
let structA = StructA()
let structB = StructB()

let test: [SomeProtocol & AnotherProtocol] = [structA, structB]
test.printSomeAndAnother()

```

However, I’m getting the following error: `Type 'any AnotherProtocol & SomeProtocol' cannot conform to 'AnotherProtocol'`.

So two questions:

1. What am I doing wrong and how to get the code above to compile?
2. Is there a syntax that I’m not familiar with that would allow me to specify the type of array literal without assigning it to a constant or a variable? Ideally, I’d like to achieve the syntax below:

```auto
[structA, structB].printSomeAndAnother()

```

But type inference assumes that we are working with an array of Any, so it doesn’t work.
