I am recently learning the source code of the Bool type in Swift. I have defined a Coin structure (shows below) imitating the source code. Similar to Bool, there are two values of true and false. I want to assign two values to Coin, on and off. It is hoped that the following effects can be achieved during initialization:
let coin = on
However, it is obviously not possible now, what can I do to achieve a direct initialization method like
let display = false?
At the same time, I also want to know where the two reserved keywords, false and true, are defined. I didn't find any relevant content in the Bool.swift file. I hope you can guide me.
The Coin structure is defined below:
public protocol ExpressibleByCoinLiteral {
associatedtype CoinLiteralType: _ExpressibleByIntCoinLiteral
init(coinLiteral value: CoinLiteralType)
}
public protocol _ExpressibleByIntCoinLiteral {
init(_intCoinLiteral value: Int)
}
public struct Coin {
public var _value: Int
public init() {
self._value = 0
}
@_transparent
public init(_ value: Int) {
self._value = value
}
@inlinable
public init(_ value: Coin) {
self = value
}
}
extension Coin: _ExpressibleByIntCoinLiteral, ExpressibleByCoinLiteral {
public init(_intCoinLiteral value: Int) {
self._value = value
}
@_transparent
public init(coinLiteral value: Coin) {
self = value
}
}
extension Coin: LosslessStringConvertible {
public init?(_ description: String) {
if description == "on" {
self = on
} else if description == "off" {
self = off
} else {
return nil
}
}
}