Skip to content

Instantly share code, notes, and snippets.

@numa08
Last active July 4, 2016 17:50
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 numa08/45d1770c374c05785522cf08d213674d to your computer and use it in GitHub Desktop.
Save numa08/45d1770c374c05785522cf08d213674d to your computer and use it in GitHub Desktop.
/**
Swift では厳密に Set<T<S>> の様な表現は不可能だと思います。
Set の場合、型パラメータ T は Hashable かつ、型パラメータ S をとりうる型である必要があります.
それは即ち T は Hashable で associatetype S が宣言された protocol であると言えます.
そこで Hashable であり associatetype S を宣言した Myp を宣言し,
Myp を実装した型パラメータ T をパラメータにとるメソッドで Set<T> を作ることで、
実質的に Set<T<S>> を実現できます.
ただ、わりとトリッキーなので使いドコロは微妙かも...
*/
protocol Myp: Hashable {
associatedtype S
}
func ms<T where T: Myp>(arg: T) -> Set<T> {
return Set(arrayLiteral: arg)
}
struct Concrete: Myp {
typealias S = Int
let value: S
var hashValue: Int {
return value
}
}
func ==(lhs: Concrete, rhs: Concrete) -> Bool {
return lhs.value == rhs.value
}
let mySet: Set<Concrete> = ms(Concrete(value: 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment