Last active
May 15, 2020 23:53
-
-
Save twh270/79b2f62d3866f43d0c480189cdbad832 to your computer and use it in GitHub Desktop.
Trouble with generics
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.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