Skip to content

Instantly share code, notes, and snippets.

@Microtribute
Forked from yllan/gist:1390622
Created March 28, 2020 12:03
Show Gist options
  • Save Microtribute/d6596421becbc69d841d52e920b1e87b to your computer and use it in GitHub Desktop.
Save Microtribute/d6596421becbc69d841d52e920b1e87b 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