Skip to main content

標準プロトコルに準拠

Swift

== に対応できるようにしたり、とか。

| Protocol | 使えるようになる機能 | 実装するメンバ | |:-|:-| | Equatable | ==, != | | | Comparable | >, <, >=, <= | == を implement するだけ。全メンバ比較するだけならデフォルト動作なので実装不要。 | | Numeric | +, -, *, / | それぞれの演算子を implement | | Hashable | collection に含まれる | hash(into:..) を impmlement (中で combine() 呼ぶ) すると hashValue で区別される。メンバが Hashable なら、特に implement 不要 | | CaseIterable | allCases で Enum の値が一覧できるようになる | 特にないのだが、既存(標準ライブラリとかの)のEnum につけることはできないみたい |

文字列内の式の展開をカスタマイズしたい場合, String.StringInterpolationappendInterpolation() を extend する。 使うことあるだろうか。

swift
extension String.StringInterpolation {
mutating func appendInterpolation(value v: Int) {
appendLiteral("<< \(v) >>")
}
}
print("the value is \(value: 3)") // => "the value is << 3 >>"