First note that Tagged
is the type from here:
https://github.com/pointfreeco/swift-prelude/blob/master/Sources/Prelude/Tagged.swift
which has ExpressibleByIntegerLiteral
when the wrapped value does.
First note that Tagged
is the type from here:
https://github.com/pointfreeco/swift-prelude/blob/master/Sources/Prelude/Tagged.swift
which has ExpressibleByIntegerLiteral
when the wrapped value does.
// This shows a problem with optional promotion: | |
import Prelude | |
enum Failure {} | |
typealias Failable<A> = Tagged<Failure, A?> | |
extension Tagged where Tag == Failure { | |
var value: A { | |
return self.unwrap | |
} | |
} | |
enum Canceled {} | |
typealias Cancelable<A> = Tagged<Canceled, A?> | |
extension Tagged where Tag == Canceled { | |
var notCanceled: A { | |
return self.unwrap | |
} | |
} | |
// This works | |
let result = Cancelable<Failable<Int>>(unwrap: Failable<Int>(unwrap: 2)) | |
// This does not. Optional promotion doesn't seem to go deep enough. | |
//let result: Cancelable<Failable<Int>> = 2 |
// This shows how to fix it: | |
// I make optionals be expressible: | |
extension Optional: ExpressibleByIntegerLiteral where Wrapped: ExpressibleByIntegerLiteral { | |
public typealias IntegerLiteralType = Wrapped.IntegerLiteralType | |
public init(integerLiteral value: Wrapped.IntegerLiteralType) { | |
self = .some(.init(integerLiteral: value)) | |
} | |
} | |
import Prelude | |
enum Failure {} | |
typealias Failable<A> = Tagged<Failure, A?> | |
extension Tagged where Tag == Failure { | |
var value: A { | |
return self.unwrap | |
} | |
} | |
enum Canceled {} | |
typealias Cancelable<A> = Tagged<Canceled, A?> | |
extension Tagged where Tag == Canceled { | |
var notCanceled: A { | |
return self.unwrap | |
} | |
} | |
// This works | |
let result = Cancelable<Failable<Int>>(unwrap: Failable<Int>(unwrap: 2)) | |
// And now this works! | |
let result: Cancelable<Failable<Int>> = 2 |