Swift 4 から導入される Protocol Oriented Integers の概要を整理してみた資料です。ここで紹介したもの以外にも詳細な機能があったりするので、それは不足するものの、プロトコル指向整数で何ができるのかを感じ取ってもらえる資料になってるかなって思います。2017/06/03 のカジュアル Swift 勉強会で発表しました。
35. // 任意の整数型からの全幅変換
init<T>(_ source: T) where T : BinaryInteger
// ビットパターンを使った内部表現そのままの変換
init<T>(extendingOrTruncating source: T) where T : BinaryInteger
// 強制的に範囲内に収める変換
init<T>(clamping source: T) where T : BinaryInteger
36. // 浮動小数点数型からの丸め変換(0 方向へ丸める)
init<T>(_ source: T) where T : FloatingPoint
// 浮動小数点数型からの Exact 変換(値を丸めない)
init?<T>(exactly source: T) where T : FloatingPoint
44. static func &>> <Other>(lhs: Self, rhs: Other) -> Self
where Other : BinaryInteger
static func &>>= <Other>(lhs: inout Self, rhs: Other)
where Other : BinaryInteger
static func &<< <Other>(lhs: Self, rhs: Other) -> Self
where Other : BinaryInteger
static func &<<= <Other>(lhs: inout Self, rhs: Other)
where Other : BinaryInteger
45.
46. var words: [UInt] {
var result = [UInt]()
result.reserveCapacity(countRepresentedWords)
for i in 0 ..< countRepresentedWords {
result.append(_word(at: i))
}
return result
}
47.
48.
49.
50.
51.
52.
53.
54.
55. init(bigEndian value: Int)
init(littleEndian value: Int)
// 内部データを序列を加味して解釈する
var bigEndian: Int { get }
var littleEndian: Int { get }
// 内部データの序列を交換して解釈する
var byteSwapped: Int { get }