This file contains 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
public int factorial(int n) | |
{ | |
if (n < 1) | |
{ | |
error("n less than 1. <" + n + ">"); | |
} | |
return n == 1 ? 1 : n * factorial(n - 1); | |
} |
This file contains 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
public MyObject myMethod(MyParameter myParameter) | |
{ | |
return undefined(); | |
} | |
// vs. | |
public MyObject myMethod(MyParameter myParameter) | |
{ | |
undefined(); |
This file contains 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
public static <T> T undefined() | |
{ | |
throw new RuntimeException(); | |
} |
This file contains 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
Map<Key, Value> map = ...; | |
Key key = ...; | |
Value value = map.get(key); |
This file contains 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
public interface Option<T> | |
{ | |
T get(); | |
} | |
public class Some<T> implements Option<T> | |
{ | |
private final T value; | |
public Some(T value) |
This file contains 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
val employee = map.get(employeeId) | |
if (employee == null) return; | |
val workAddress = employee.workAddress | |
if (workAddress == null) return; | |
println(workAddress.state.name) | |
// vs. | |
map.get(employeeId).flatMap(_.workAddress).map(_.state.name).foreach(println) |
This file contains 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
def undefined: Nothing = throw new RuntimeException | |
def undefined: Nothing = undefined | |
def undefined: Nothing = new Object().asInstanceOf[Nothing] |
This file contains 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
val i: Int = undefined | |
def undefined: Nothing // works | |
def undefined: Null // doesn't work since a primitive may not be null on the JVM |
This file contains 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
undefined :: a | |
error :: [Char] -> a |
This file contains 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
public enum None implements Option<Nothing> | |
{ | |
INSTANCE; | |
public Nothing get() | |
{ | |
throw new UnsupportedOperationException("None.get()"); | |
} | |
} |