Created
October 10, 2014 08:14
-
-
Save TimDumol/684ba857e9b3ff1b68d7 to your computer and use it in GitHub Desktop.
Option<T>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Option<T> { | |
private T val; | |
private bool hasVal; | |
public Option<T>() { | |
hasVal = false; | |
} | |
public Option<T>(T val) { | |
this.val = val; | |
hasVal = true; | |
} | |
public bool isSome() { | |
return hasVal; | |
} | |
public bool get() { | |
if (!hasVal) { | |
throw new EntityNotFoundException(); | |
} | |
return val; | |
} | |
} | |
class MyObject { | |
public static Option<MyObject> findBy(int id) { | |
if (!found) { | |
return new Option(); | |
} | |
return new Option(foundObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment