我如何符合_BuiltInIntegerLiteralConvertible协议?

时间:2014-11-14 18:38:43

标签: swift

我正在尝试创建一个符合IntegerType的类型,但是当我实现所有可见内容时,我仍然会收到一个错误,我的类型不符合_BuiltInIntegerLiteralConvertible。内置Int类型定义了这个:

init(_builtinIntegerLiteral value: Builtin.Int2048)

看起来像我想要的初始化程序,但编译器无法识别Builtin.Int2048。我可以用什么呢?

这是我的类型的(长)代码:

struct MyInt : Printable, Comparable, Hashable, IntegerArithmeticType, RandomAccessIndexType, BitwiseOperationsType, IntegerType {

    var int: Int = 0

    init(_ value: Int) {
        int = value
    }

    init(integerLiteral value: IntegerLiteralType) {
        int = value
    }

    // MARK: Printable

    var description: String {
        return "\(int)"
    }

    // MARK: Hashable

    var hashValue: Int {
        return int.hashValue
    }

    // MARK: IntegerArithmeticType

    func toIntMax() -> IntMax {
        return IntMax(int)
    }

    static func addWithOverflow(lhs: MyInt, _ rhs: MyInt) -> (MyInt, overflow: Bool) {
        let result = Int.addWithOverflow(lhs.int, rhs.int)
        return (MyInt(result.0), result.overflow)
    }

    static func subtractWithOverflow(lhs: MyInt, _ rhs: MyInt) -> (MyInt, overflow: Bool) {
        let result = Int.subtractWithOverflow(lhs.int, rhs.int)
        return (MyInt(result.0), result.overflow)
    }

    static func multiplyWithOverflow(lhs: MyInt, _ rhs: MyInt) -> (MyInt, overflow: Bool) {
        let result = Int.multiplyWithOverflow(lhs.int, rhs.int)
        return (MyInt(result.0), result.overflow)
    }

    static func divideWithOverflow(lhs: MyInt, _ rhs: MyInt) -> (MyInt, overflow: Bool) {
        let result = Int.divideWithOverflow(lhs.int, rhs.int)
        return (MyInt(result.0), result.overflow)
    }

    static func remainderWithOverflow(lhs: MyInt, _ rhs: MyInt) -> (MyInt, overflow: Bool) {
        let result = Int.remainderWithOverflow(lhs.int, rhs.int)
        return (MyInt(result.0), result.overflow)
    }

    // MARK: BitwiseOperationsType

    static var allZeros: MyInt {
        return MyInt(0)
    }

    // MARK: Strideable, RandomAccessIndexType

    typealias Distance = Int
    typealias Stride = Int

    func predecessor() -> MyInt {
        return MyInt(int - 1)
    }

    func successor() -> MyInt {
        return MyInt(int + 1)
    }

    func distanceTo(other: MyInt) -> Stride {
        return other.int - int
    }

    func advancedBy(n: Stride) -> MyInt {
        return MyInt(int + n)
    }
}

// MARK: Equatable

func ==(lhs: MyInt, rhs: MyInt) -> Bool { return lhs.int == rhs.int }

// MARK: Comparable

func <(lhs: MyInt, rhs: MyInt) -> Bool { return lhs.int < rhs.int }

// MARK: IntegerArithmeticType, ctd

func %(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int % rhs.int) }

func *(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int * rhs.int) }

func +(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int + rhs.int) }

func -(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int - rhs.int) }

func /(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int / rhs.int) }

// MARK: BitwiseOperationsType, ctd

func &(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int & rhs.int) }

func ^(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int ^ rhs.int) }

prefix func ~(x: MyInt) -> MyInt { return MyInt(~x.int) }

func |(lhs: MyInt, rhs: MyInt) -> MyInt { return MyInt(lhs.int | rhs.int) }

1 个答案:

答案 0 :(得分:2)

尝试添加此内容:

init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType) {
    int = Int(_builtinIntegerLiteral: value)
}

Builtin名称空间完全无法访问我们。但是,Builtin.Int2048似乎是Swift._MaxBuiltinIntegerType的别名。

实际上,

let i:MyInt = 42

这会调用init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType)初始化程序!