Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active August 23, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbrandonw/9b6416091466d9cad7a4035809734ea7 to your computer and use it in GitHub Desktop.
Save mbrandonw/9b6416091466d9cad7a4035809734ea7 to your computer and use it in GitHub Desktop.
// 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