gpt4 book ai didi

swift - 我怎样才能符合 _BuiltInIntegerLiteralConvertible 协议(protocol)?

转载 作者:行者123 更新时间:2023-11-28 13:19:44 26 4
gpt4 key购买 nike

我正在尝试创建一个符合 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) }

最佳答案

尝试添加这个:

init(_builtinIntegerLiteral value: _MaxBuiltinIntegerType) {
int = Int(_builtinIntegerLiteral: value)
}
我们完全无法访问

Builtin 命名空间。但是,Builtin.Int2048 似乎是 Swift._MaxBuiltinIntegerType 的别名。

其实,

let i:MyInt = 42

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

关于swift - 我怎样才能符合 _BuiltInIntegerLiteralConvertible 协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26936706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com