Skip to content

Instantly share code, notes, and snippets.

@betamatt
Created June 24, 2014 03:16
Show Gist options
  • Save betamatt/66819f6cbedc0cc7c352 to your computer and use it in GitHub Desktop.
Save betamatt/66819f6cbedc0cc7c352 to your computer and use it in GitHub Desktop.
Hacking around Swift's generics bugs
class Cell<T> {
let cell: T[]
var value: T {
get { return cell[0] }
}
init (_ value:T) {
self.cell = [value]
}
}
operator prefix * {}
@prefix func * <T> (c:Cell<T>) -> T
{
return c.value
}
// Either type
enum Result<L,R> {
case Ok(Cell<L>)
case Err(Cell<R>)
static func ok(value:L) -> Result {
return Ok(Cell(value))
}
static func right(value:R) -> Result {
return Err(Cell(value))
}
}
let blah = Result<String, String>.ok("Blah!")
switch blah {
case .Ok(let x) :
*x
case .Err(let e) :
*e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment