Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment