Extending generic type where generic parameter conforms to a protocol

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.

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:

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:

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:
[structA, structB].printSomeAndAnother()

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