Created
October 15, 2017 06:54
-
-
Save jbgi/1a3697d380f2071eb8c2fd3ad259c011 to your computer and use it in GitHub Desktop.
Using existentials to implement abstract type alias in 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
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
interface Person { | |
int age(); | |
} | |
interface People<T> { | |
T fromList(List<Person> ps); | |
List<Integer> getAges(T people); | |
People<?> module = new People<List<Person>>() { | |
public List<Person> fromList(List<Person> ps) { | |
return ps; | |
} | |
public List<Integer> getAges(List<Person> people) { | |
return people.stream().map(Person::age).collect(Collectors.toList()); | |
} | |
}; | |
} | |
// run with: | |
// javac Program.java | |
// java -cp . Program | |
public class Program<T> { | |
People<T> P; | |
Program(People<T> P) {this.P = P;} | |
void run() { | |
T people = P.fromList(Arrays.asList(() -> 21, () -> 37)); | |
System.out.println(P.getAges(people)); // print "[21, 37]" | |
} | |
public static void main(String[] args) { | |
new Program<>(People.module).run(); | |
} | |
} |
Ah, true. The run
method is indeed more elegant.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
but the cast-free alternative is only one more method call:
so I personally would not do it.