Last active
August 29, 2015 14:24
-
-
Save jazzychad/68c12d4fce65e543ede4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// why does this not compile? | |
// Book implements BookRenderable | |
// so why does the compiler complain when Library.books is a [Book] and LibraryRenderable is looking for [BookRenderable] ?? | |
protocol LibraryRenderable { | |
var address : String? { get } | |
var books : [BookRenderable]? { get } | |
} | |
protocol BookRenderable { | |
var title : String? { get } | |
var author : String? { get } | |
} | |
struct MockLibrary : LibraryRenderable { | |
let address = "123 Main Street" | |
let books = [MockBook(), MockBook()] | |
} | |
struct MockBook : BookRenderable { | |
let title : String? = "Ender's Game" | |
let author : String? = "Orson Scott Card" | |
} | |
class Library : LibraryRenderable { | |
var address : String? | |
var books : [Book]? // <---- compiler complains "does not conform to LibraryRenderable" | |
} | |
class Book : BookRenderable { | |
var title : String? | |
var author : String? | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// making this work with protocol typealiases | |
// the following compiles cleanly | |
protocol LibraryRenderable { | |
typealias BookType : BookRenderable | |
var address : String? { get } | |
var books : [BookType]? { get } | |
} | |
protocol BookRenderable { | |
var title : String? { get } | |
var author : String? { get } | |
} | |
struct MockLibrary : LibraryRenderable { | |
typealias BookType = MockBook | |
let address : String? = "123 Main Street" | |
let books : [MockBook]? = [MockBook(), MockBook()] | |
} | |
struct MockBook : BookRenderable { | |
let title : String? = "Ender's Game" | |
let author : String? = "Orson Scott Card" | |
} | |
class Library : LibraryRenderable { | |
typealias BookType = Book | |
var address : String? | |
var books : [Book]? | |
} | |
class Book : BookRenderable { | |
var title : String? | |
var author : String? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've just confirmed that they did make the same design decision in Java too: