Skip to content

Instantly share code, notes, and snippets.

@owensd
Created August 22, 2014 18:56
Embed
What would you like to do?
//
// Failable.swift
// MemoryInvestigation
//
// Created by David Owens II on 8/22/14.
// Copyright (c) 2014 Kiad Software. All rights reserved.
//
public enum FailableOf<T> {
case Success(FailableValueWrapper<T>)
case Failure(Error)
public init(_ value: T) {
self = .Success(FailableValueWrapper(value))
}
public init(_ error: Error) {
self = .Failure(error)
}
public var failed: Bool {
switch self {
case .Failure(let error):
return true
default:
return false
}
}
public var error: Error? {
switch self {
case .Failure(let error):
return error
default:
return nil
}
}
public var value: T? {
switch self {
case .Success(let wrapper):
return wrapper.value
default:
return nil
}
}
}
/// This is a workaround-wrapper class for a bug in the Swift compiler. DO NOT USE THIS CLASS!!
public class FailableValueWrapper<T> {
public let value: T
public init(_ value: T) { self.value = value }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment