Skip to content

Instantly share code, notes, and snippets.

@pofat
Last active October 19, 2019 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pofat/70e190aede0e786421ec87157e7408d8 to your computer and use it in GitHub Desktop.
Save pofat/70e190aede0e786421ec87157e7408d8 to your computer and use it in GitHub Desktop.
PAT 好讀版
// 有 associatedtype 的 protocol
public protocol IteratorProtocol {
associatedtype Element
mutating func next() -> Element?
}
// 有 Self 的 protocol, Hashable 的 Self 來自所繼承的 Equatable
public protocol Hashable: Equatable {
var hashValue: Int { get }
func hash(into hasher: inout Hasher)
func _rawHashValue(seed: Int) -> Int
}
public protocol Equatable {
static func == (lhs: Self, rhs: Self) -> Bool
}
// 以下寫法都會被 compiler 抱怨
// Protocol XXX can only be used as a generic constraint because it has Self or associated type requirements
func generateDictionaryKey() -> Hashable {
return 8
}
func getIterator() -> IteratorProtocol {
return ["1", "2", "3"].makeIterator()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment