Skip to content

Instantly share code, notes, and snippets.

@twh270
Last active May 15, 2020 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twh270/79b2f62d3866f43d0c480189cdbad832 to your computer and use it in GitHub Desktop.
Save twh270/79b2f62d3866f43d0c480189cdbad832 to your computer and use it in GitHub Desktop.
Trouble with generics
import java.util.function.*;
/*
Problem solved -- thanks to https://www.reddit.com/user/Yggval for identifying the trouble
(see https://www.reddit.com/r/javahelp/comments/gkid8w/trouble_with_generics/)
Working solution follows: essentially, Sequence<T> needs to be Sequence<T, U> so the
Compose<IdentifierNode, IdentifierNode, TypeExpressionNode> which extends NodeParseRule<IdentifierNode, TypeExpressionNode>
can map to the first argument of the Sequence constructor.
*/
class ParseContext<T> {
// ...
}
class NodeList {
// ...
}
class Node {
// ...
}
class IdentifierNode extends Node {
// ...
}
class TypeExpressionNode extends Node {
// ...
}
abstract class NodeParseRule<T, R> implements Function<ParseContext<T>, R> {
// ...
}
class Compose<T, U, R> extends NodeParseRule<T, R> {
public R apply(final ParseContext<T> context) { return null; }
}
class Sequence<T, U> extends NodeParseRule<T, NodeList> {
private final NodeParseRule<T, U> elementRule;
private final Predicate<ParseContext<T>> terminationCondition;
public Sequence(final NodeParseRule<T, U> elementRule, final Predicate<ParseContext<T>> terminationCondition) {
this.elementRule = elementRule;
this.terminationCondition = terminationCondition;
}
@Override
public NodeList apply(final ParseContext<T> parseContext) {
terminationCondition.test(parseContext);
return new NodeList();
}
}
class Trouble {
private Compose<IdentifierNode, IdentifierNode, TypeExpressionNode> parameterTypeParser = null; // ...
private Sequence<IdentifierNode, TypeExpressionNode> sequence = new Sequence<>(parameterTypeParser,
(ParseContext<IdentifierNode> pc) -> false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment