3

Even though I have no associated type, I get this error:

Protocol can only be used as a generic constraint because it has Self or associated type requirements

The code is as follows. I define a protocol called Board, and then, within another protocol (called Rules) I create a parameter of type Board. That's when I get the error.

I have no associated type, so I don't know what the compiler is talking about...

protocol Board : Equatable, Hashable  {
    init()

    var width  : Int {get}
    var height : Int {get}

}

protocol Rules {
    init(board: Board)
}

What on Earth am I doing wrong???

Hamish
  • 78,605
  • 19
  • 187
  • 280
cfischer
  • 24,452
  • 37
  • 131
  • 214
  • 1
    Take a look at `Equatable`'s definition ;) – Hamish Mar 24 '17 at 19:04
  • @Hamish OK, I see the Sekf there. What's the best way to solve this? Could elaborate on this whole issue as a response? I'll accept it right away. Thanks! – cfischer Mar 24 '17 at 19:09
  • 1
    As the error message suggests, you could use a generic initialiser, e.g `init(board: T)` – Hamish Mar 24 '17 at 19:11
  • OK, that gets rid of the error, although I'm not sure what on Earth is going on... I'll check the video from Alex Gallager that was recommended elsewhere. – cfischer Mar 24 '17 at 19:13
  • 1
    It's [merely a language limitation](http://stackoverflow.com/q/41695792/2976878) – you currently cannot directly talk in terms of heterogeneous types that conform to a given protocol with associated type(s), as the compiler won't know what the concrete type(s) are for those associated type(s). See also [this related Q&A](http://stackoverflow.com/q/41298464/2976878) for the specific case of needing to talk about heterogeneous types that conform to `Equatable`. – Hamish Mar 24 '17 at 19:21

0 Answers0