-
-
Save Microtribute/d6596421becbc69d841d52e920b1e87b to your computer and use it in GitHub Desktop.
Phantom Type Java
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
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