Created
December 23, 2021 17:09
-
-
Save ailabs-software/79e40a2753984dcc3a10d3e158ff8c15 to your computer and use it in GitHub Desktop.
Trying to figure out a way say type are in class extends a generic type (IVisitor), _without_ filling in the R type parameter of visitor from the class type arg, but instead from a method named accept(). This would then enable use of visitor pattern without coupling accept() to what visit methods exist, and therefore what node types exist. This …
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
abstract class IVisitor<R> { | |
// visitX() methods not declared here, but | |
// on implementing class, so we can add new node types, | |
// without coupling the code calling accept() to that knowledge. | |
} | |
abstract class NodeVisitor<R> implements IVisitor<R> { | |
R visitParen(Node token); | |
} | |
abstract class Node< T<R> extends IVisitor<R> > { | |
R accept<R>(T<R> visitor); | |
} | |
abstract class Element implements Node<NodeVisitor> { | |
@override | |
R accept<R>(NodeVisitor<R> visitor) | |
{ | |
} | |
} | |
void main() | |
{ | |
Node? nullableNode; | |
Node node = (nullableNode)!; | |
NodeVisitor<int>? nullableVisitor; | |
NodeVisitor<int> visitor = (nullableVisitor)!; | |
print( node.accept(visitor) + "testing that Dart knows statically that R is actually an int"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment