Very interesting, and somewhat unexpected. Example:
struct OnesComplement: ExpressibleByIntegerLiteral {
init(integerLiteral value: Int) {
self.value = value
}
init(value: Int) {
self.value = value
}
var value: Int
static prefix func - (a: Self) -> Self {
return Self(value: ~a.value)
}
}
let one: OnesComplement = 1
print("1 ==", one.value) // 1 == 1
let a: OnesComplement = -one
print("-2 ==", a.value) // -2 == -2
let b: OnesComplement = -(1 as OnesComplement)
print("-2 ==", b.value) // -2 == -2
let c: OnesComplement = -1
print("-2 ==", c.value) // -2 == -1 🤔?!
print()
Edit: the workaround is simple of course.
init(integerLiteral value: Int) {
self.value = value < 0 ? ~(-value) : value
}