Giter Club home page Giter Club logo

visitor-net's Introduction

logo

Status:

stars NuGet

Visitor.NET

First-ever acyclic generic extensible typesafe implementation of Visitor pattern for .NET without any usage of dynamic cast.

With Visitor.NET you can develop typesafe acyclic visitors even if you do not have access to source code of visitable structures.

Installation

NuGet

Install package : https://www.nuget.org/packages/Visitor.NET.

GitHub

  • Clone locally this github repository
  • Build the Visitor.NET.sln solution

Projects using Visitor.NET

Usage

Basic Example

Let's say we have some expression-tree-like hierarchy, implementing basic arithmetics, like this:

public abstract record BinaryTreeNode;

public record Operation(char Symbol, BinaryTreeNode Left, BinaryTreeNode Right) : BinaryTreeNode;

public record Number(double Value) : BinaryTreeNode;

public record Parenthesis(BinaryTreeNode Node) : BinaryTreeNode;

So we may want to traverse it in order to, for example, compute expression result.

First of all, we implement evaluator using IVisitor<,> interface:

public class BinaryTreeEvaluator : VisitorBase<BinaryTreeNode, double>,
    IVisitor<Operation, double>,
    IVisitor<Number, double>,
    IVisitor<Parenthesis, double>
{
    public double Visit(Operation visitable) =>
        visitable.Symbol switch
        {
            '+' => visitable.Left.Accept(This) + visitable.Right.Accept(This),
            _ => throw new NotImplementedException()
        };

    public double Visit(Number visitable) => visitable.Value;

    public double Visit(Parenthesis visitable) => visitable.Node.Accept(This);
}

But then we have to tell structures we visit that they are visitable.

It is done through IVisitable<> interface implementation:

public abstract record BinaryTreeNode : IVisitable<BinaryTreeNode>
{
    public abstract TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor);
}

public record Operation(
    char Symbol,
    BinaryTreeNode Left,
    BinaryTreeNode Right) : BinaryTreeNode, IVisitable<Operation>
{
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>
        Accept(visitor);

    public TReturn Accept<TReturn>(
        IVisitor<Operation, TReturn> visitor) =>
        visitor.Visit(this);
}

public record Number(double Value) : BinaryTreeNode, IVisitable<Number>
{
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>
        Accept(visitor);

    public TReturn Accept<TReturn>(
        IVisitor<Number, TReturn> visitor) =>
        visitor.Visit(this);
}

public record Parenthesis(BinaryTreeNode Node) : BinaryTreeNode, IVisitable<Parenthesis>
{
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>
        Accept(visitor);

    public TReturn Accept<TReturn>(
        IVisitor<Parenthesis, TReturn> visitor) =>
        visitor.Visit(this);
}

Basically, if you have access to source code of structure you want "visit", it's better to always have implementation:

return visitor.Visit(this);

In case you would make Visit implementation procedure (i.e. have no returning value), use VisitUnit type as return type.

So, your method would look like this:

public class SomeVisitor : IVisitor<Some>
{
    public VisitUnit Visit(Some visitable)
    {
        //...
        return default;
    }
}

Adapter Usage

Let's imagine you want to visit some structure defined outside of your project (library, dto, etc.):

public record LinkedListNode<T>(T Data, LinkedListNode<T> Next)
{
    public bool HasNext() => Next != null;
}

So we may define wrapper around instance of this type which would became visitable:

public class LinkedListToVisitableAdapter<T> :
    VisitableAdapter<LinkedListNode<T>>,
    IVisitable<LinkedListToVisitableAdapter<T>>
{
    public LinkedListToVisitableAdapter(LinkedListNode<T> data) :
        base(data)
    {
    }

    public override TReturn Accept<TReturn>(
        IVisitor<VisitableAdapter<LinkedListNode<T>>, TReturn> visitor) =>
        Accept(visitor);

    public TReturn Accept<TReturn>(
        IVisitor<LinkedListToVisitableAdapter<T>, TReturn> visitor) =>
        visitor.Visit(this);
}

This adapter can be instantiated with VisitableAdapterFactory<> implementation:

public class LinkedListToVisitableAdapterFactory<T> :
    VisitableAdapterFactory<LinkedListNode<T>>
{
    public override LinkedListToVisitableAdapter<T> Create(LinkedListNode<T> data) =>
        new(data);
}

Bringing it all together:

public class LinkedListNodePrinter<T> : VisitorNoReturnBase<VisitableAdapter<LinkedListNode<T>>>,
    IVisitor<LinkedListToVisitableAdapter<T>>
{
    private readonly StringBuilder _sb = new();
    private readonly VisitableAdapterFactory<LinkedListNode<T>> _factory;

    public LinkedListNodePrinter(VisitableAdapterFactory<LinkedListNode<T>> factory) =>
        _factory = factory;

    public VisitUnit Visit(LinkedListToVisitableAdapter<T> visitable)
    {
        var node = visitable.Data;
        _sb.Append(node.Data);
        if (node.HasNext())
        {
            var next = _factory.Create(node.Next);
            _sb.Append("->");
            next.Accept(This);
        }

        return default;
    }

    public override string ToString() => _sb.ToString();
}

Visitor.NET.AutoVisitableGen

If you do not want implement visitable manually, you can do it automatically with incremental source generator.

Install package : https://www.nuget.org/packages/Visitor.NET.AutoVisitableGen.

Then, rewrite the nodes type declarations like this:

public abstract record BinaryTreeNode : IVisitable<BinaryTreeNode>
{
    public abstract TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor);
}

[AutoVisitable<BinaryTreeNode>]
public partial record Operation(
    char Symbol,
    BinaryTreeNode Left,
    BinaryTreeNode Right) : BinaryTreeNode;

[AutoVisitable<BinaryTreeNode>]
public partial record Number(double Value) : BinaryTreeNode;

[AutoVisitable<BinaryTreeNode>]
public partial record Parenthesis(BinaryTreeNode Node) : BinaryTreeNode;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.