Skip to content

Instantly share code, notes, and snippets.

@motlin
motlin / BottomType.java
Created June 5, 2012 03:50
Code example to illustrate bottom types
public int factorial(int n)
{
if (n < 1)
{
error("n less than 1. <" + n + ">");
}
return n == 1 ? 1 : n * factorial(n - 1);
}
@motlin
motlin / BottomType.java
Created June 5, 2012 03:55
Code example to illustrate bottom types
public MyObject myMethod(MyParameter myParameter)
{
return undefined();
}
// vs.
public MyObject myMethod(MyParameter myParameter)
{
undefined();
@motlin
motlin / BottomType.java
Created June 6, 2012 00:21
Code example to illustrate bottom types
public static <T> T undefined()
{
throw new RuntimeException();
}
@motlin
motlin / BottomType.java
Created June 6, 2012 00:27
Code example to illustrate bottom types
Map<Key, Value> map = ...;
Key key = ...;
Value value = map.get(key);
@motlin
motlin / BottomType.java
Created June 6, 2012 00:36
Code example to illustrate bottom types
public interface Option<T>
{
T get();
}
public class Some<T> implements Option<T>
{
private final T value;
public Some(T value)
@motlin
motlin / BottomType.scala
Created June 6, 2012 02:50
Code example to illustrate bottom types
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)
@motlin
motlin / BottomType.scala
Created June 6, 2012 02:54
Code example to illustrate bottom types
def undefined: Nothing = throw new RuntimeException
def undefined: Nothing = undefined
def undefined: Nothing = new Object().asInstanceOf[Nothing]
@motlin
motlin / BottomType.scala
Created June 6, 2012 02:57
Code example to illustrate bottom types
val i: Int = undefined
def undefined: Nothing // works
def undefined: Null // doesn't work since a primitive may not be null on the JVM
@motlin
motlin / BottomType.hs
Created June 6, 2012 03:08
Code example to illustrate bottom types
undefined :: a
error :: [Char] -> a
@motlin
motlin / BottomType.java
Created June 12, 2012 02:55
Code example to illustrate bottom types
public enum None implements Option<Nothing>
{
INSTANCE;
public Nothing get()
{
throw new UnsupportedOperationException("None.get()");
}
}