Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidelettieri/visitor-generator
https://github.com/davidelettieri/visitor-generator
Last synced: about 9 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/davidelettieri/visitor-generator
- Owner: davidelettieri
- Created: 2022-12-13T19:15:43.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-28T07:02:14.000Z (5 months ago)
- Last Synced: 2024-06-29T08:10:33.376Z (5 months ago)
- Language: C#
- Size: 63.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# visitor-generator
The source generator allows to easily implement the visitor pattern. It is a proof of concepts and it requires the usage of partial interfaces and partial classes to implements the required methods.
Starting point is an interface that all the nodes that we want to visit have to implement. For example
```csharp
[VisitorNode]
public partial interface INode
{
}
```After this we need to define partial classes for all the nodes. For example
```csharp
public partial class Success : INode { }
public partial class Failure : INode { }
```With these sample the generator will produce the following code
```csharp
// Augment the base interface with the required Accept method
public partial interface INode
{
T Accept(INodeVisitor visitor);
}// Definine the visitor interface
public interface INodeVisitor
{
T Visit(Success node);
T Visit(Failure node);
}// Implement the Accept method on the nodes
public partial class Success
{
public T Accept(INodeVisitor visitor) => visitor.Visit(this);
}public partial class Failure
{
public T Accept(INodeVisitor visitor) => visitor.Visit(this);
}
```## Using the package
The package is published here on github. Please follow the documentation to add the nuget source https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry