Skip to content

Instantly share code, notes, and snippets.

@yllan
Created November 24, 2011 04:33
Show Gist options
  • Save yllan/1390622 to your computer and use it in GitHub Desktop.
Save yllan/1390622 to your computer and use it in GitHub Desktop.
Phantom Type Java
class Book<P> {
String title;
public Book(String s) {
title = s;
}
}
interface Biography {}
interface Comic {}
public class Test {
public static Book<Biography> isaacsonWrite(String name) {
return new Book<Biography>(name);
}
public static Book<Comic> stanLeeWrite(String name) {
return new Book<Comic>(name);
}
public static void marvelPublish(Book<Comic> c) {
System.out.println("Marvel publish " + c.title);
}
public static void main(String [] args) {
// marvelPublish(isaacsonWrite("Steve Jobs")); // compile error!
marvelPublish(stanLeeWrite("Ironman"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment