Skip to content

Instantly share code, notes, and snippets.

@mariofusco
Created February 15, 2018 08:59
Show Gist options
  • Select an option

  • Save mariofusco/f8af4cf230a62385a5632fca310f08e5 to your computer and use it in GitHub Desktop.

Select an option

Save mariofusco/f8af4cf230a62385a5632fca310f08e5 to your computer and use it in GitHub Desktop.
import java.util.function.Predicate;
public class TypeInference {
public static void main( String[] args ) {
// compile
Pattern<String> p1 = pattern(String.class, expr(s -> s.length() > 3, id("expr1")));
// don't compile
Pattern<String> p2 = pattern(String.class, expr(s -> s.length() > 3).id("expr1"));
}
public static <T> Pattern<T> pattern(Class<T> type, Expr<T> expr) {
return new Pattern<T>(type, expr);
}
public static <T> Expr<T> expr(Predicate<T> pred) {
return new Expr<T>(pred);
}
public static <T> Expr<T> expr(Predicate<T> pred, String id) {
return new Expr<T>(pred, id);
}
public static String id(String id) {
return id;
}
public static class Pattern<T> {
private final Class<T> type;
private final Expr<T> expr;
public Pattern( Class<T> type, Expr<T> expr ) {
this.type = type;
this.expr = expr;
}
}
public static class Expr<T> {
private final Predicate<T> pred;
private String id;
public Expr( Predicate<T> pred ) {
this.pred = pred;
}
public Expr( Predicate<T> pred, String id ) {
this.pred = pred;
}
public Expr<T> id(String id) {
this.id = id;
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment