Skip to content

Instantly share code, notes, and snippets.

@taku0
Created December 18, 2015 00:04
Show Gist options
  • Save taku0/9d7e8aa752a5c2dd8743 to your computer and use it in GitHub Desktop.
Save taku0/9d7e8aa752a5c2dd8743 to your computer and use it in GitHub Desktop.
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);
}
}
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