Instantiate associatedtype from outside of protocol

You can't nest types inside of protocols. But don't let that force you into associated types if the equivalent of them would do, when the general case that Slava went over doesn't apply.

protocol Game {
  init()

  typealias Options = MyLibrary.Options<Self>
  typealias Configuration = MyLibrary.Configuration<Self>
}

struct Options<Game: MyLibrary.Game> { }

struct Configuration<Game: MyLibrary.Game> {
  func setUp() {
    let game = Game()
    let options = Game.Options()
  }
}

Do you even need a Game protocol?

struct Game {
  struct Options { }
  struct Configuration {
    func setUp() {
      let game = Game()
      let options = Options()
    }
  }
}
1 Like