Created
December 18, 2015 00:04
-
-
Save taku0/9d7e8aa752a5c2dd8743 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.Collections; | |
import java.util.List; | |
interface Apply<TypeConstructor, Param> { } | |
interface EmptyCollections<Coll> { | |
<Elem> Apply<Coll, Elem> empty(); | |
} | |
@SuppressWarnings("unchecked") | |
class ListType { | |
public static <Elem> Apply<ListType, Elem> to(List<Elem> value) { | |
return (Apply<ListType, Elem>)value; | |
} | |
public static <Elem> List<Elem> from(Apply<ListType, Elem> value) { | |
return (List<Elem>)value; | |
} | |
} | |
class EmptyLists implements EmptyCollections<ListType> { | |
public <Elem> Apply<ListType, Elem> empty() { | |
return ListType.<Elem>to(Collections.emptyList()); // wrap | |
} | |
} | |
public class HK { | |
public static void main(String... args) { | |
EmptyLists emptyLists = new EmptyLists(); | |
List<Integer> integers = ListType.from(emptyLists.<Integer>empty()); // unwrap | |
System.out.println(integers); | |
} | |
} |
This file contains hidden or 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
Exception in thread "main" java.lang.ClassCastException: java.util.Collections$EmptyList cannot be cast to Apply | |
at ListType.to(HK.java:13) | |
at EmptyLists.empty(HK.java:23) | |
at HK.main(HK.java:30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment