swift - How can I conform to the _BuiltInIntegerLiteralConvertible protocol? -
i'm trying create type conforms integertype
, when visible implemented, i'm still getting error type doesn't conform _builtinintegerliteralconvertible
. built-in int
type defines this:
init(_builtinintegerliteral value: builtin.int2048)
which looks initializer want, compiler doesn't recognize builtin.int2048
. can utilize instead?
here's (long) code type:
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 { homecoming "\(int)" } // mark: hashable var hashvalue: int { homecoming int.hashvalue } // mark: integerarithmetictype func tointmax() -> intmax { homecoming intmax(int) } static func addwithoverflow(lhs: myint, _ rhs: myint) -> (myint, overflow: bool) { allow result = int.addwithoverflow(lhs.int, rhs.int) homecoming (myint(result.0), result.overflow) } static func subtractwithoverflow(lhs: myint, _ rhs: myint) -> (myint, overflow: bool) { allow result = int.subtractwithoverflow(lhs.int, rhs.int) homecoming (myint(result.0), result.overflow) } static func multiplywithoverflow(lhs: myint, _ rhs: myint) -> (myint, overflow: bool) { allow result = int.multiplywithoverflow(lhs.int, rhs.int) homecoming (myint(result.0), result.overflow) } static func dividewithoverflow(lhs: myint, _ rhs: myint) -> (myint, overflow: bool) { allow result = int.dividewithoverflow(lhs.int, rhs.int) homecoming (myint(result.0), result.overflow) } static func remainderwithoverflow(lhs: myint, _ rhs: myint) -> (myint, overflow: bool) { allow result = int.remainderwithoverflow(lhs.int, rhs.int) homecoming (myint(result.0), result.overflow) } // mark: bitwiseoperationstype static var allzeros: myint { homecoming myint(0) } // mark: strideable, randomaccessindextype typealias distance = int typealias stride = int func predecessor() -> myint { homecoming myint(int - 1) } func successor() -> myint { homecoming myint(int + 1) } func distanceto(other: myint) -> stride { homecoming other.int - int } func advancedby(n: stride) -> myint { homecoming myint(int + n) } } // mark: equatable func ==(lhs: myint, rhs: myint) -> bool { homecoming lhs.int == rhs.int } // mark: comparable func <(lhs: myint, rhs: myint) -> bool { homecoming lhs.int < rhs.int } // mark: integerarithmetictype, ctd func %(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int % rhs.int) } func *(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int * rhs.int) } func +(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int + rhs.int) } func -(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int - rhs.int) } func /(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int / rhs.int) } // mark: bitwiseoperationstype, ctd func &(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int & rhs.int) } func ^(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int ^ rhs.int) } prefix func ~(x: myint) -> myint { homecoming myint(~x.int) } func |(lhs: myint, rhs: myint) -> myint { homecoming myint(lhs.int | rhs.int) }
try adding this:
init(_builtinintegerliteral value: _maxbuiltinintegertype) { int = int(_builtinintegerliteral: value) }
builtin
namespace totally unaccessible us. but, seems builtin.int2048
aliased swift._maxbuiltinintegertype
.
actually,
let i:myint = 42
this calls init(_builtinintegerliteral value: _maxbuiltinintegertype)
initializer!
swift
No comments:
Post a Comment