GithubHelp home page GithubHelp logo

readonlystructgenerator's Introduction

ReadonlyStructGenerator

A source generator of readonly struct.

Usage

  1. Declare the structure with partial and add the properties with the init accessor.
  2. Add the ReadonlyStructGenerator.ReadonlyStructAttribute to the struct.
[ReadonlyStructGenerator.ReadonlyStruct]
public partial struct Point
{
    public int X { get; init; }
    public int Y { get; init; }
}

After the build, the following code will be generated.

  • Struct declaration with the readonly keyword.
  • Definition of a primary constructor.
  • Implement the IEquatable<T> interface.
  • Override object.Equals(), object.GetHashCode() and object.ToString().
  • Define operator overloading (==, !=).
#nullable enable
using System;
namespace SampleConsoleApp
{
    readonly partial struct Point : IEquatable<Point>
    {
        public Point(int x,int y) => (X,Y) = (x,y);
        public bool Equals(Point other) => (X,Y) == (other.X,other.Y);
        public override bool Equals(object? obj) => (obj is Point other) && Equals(other);
        public override int GetHashCode() => HashCode.Combine(X,Y);
        public static bool operator ==(Point left, Point right) => left.Equals(right);
        public static bool operator !=(Point left, Point right) => !(left == right);
        public override string ToString() => $"{nameof(Point)}({X},{Y})";
    }
}
  • Properties that do not have an init accessor are not subject to initialization in the constructor or to equivalence comparisons.
  • If you write a constructor, it will not be generated by the SourceGenerator.
[ReadonlyStructGenerator.ReadonlyStruct]
public partial struct Vector3
{
    public float X { get; init; }
    public float Y { get; init; }
    public float Z { get; init; }

    public float Norm { get; }

    public Vector3(float x, float y, float z)
    {
        (X, Y, Z) = (x, y, z);
        Norm = (float)Math.Sqrt(X * X + Y * Y + Z * Z);
    }
}
#nullable enable
using System;
namespace SampleConsoleApp
{
    readonly partial struct Vector3 : IEquatable<Vector3>
    {
        public bool Equals(Vector3 other) => (X,Y,Z) == (other.X,other.Y,other.Z);
        public override bool Equals(object? obj) => (obj is Vector3 other) && Equals(other);
        public override int GetHashCode() => HashCode.Combine(X,Y,Z);
        public static bool operator ==(Vector3 left, Vector3 right) => left.Equals(right);
        public static bool operator !=(Vector3 left, Vector3 right) => !(left == right);
        public override string ToString() => $"{nameof(Vector3)}({X},{Y},{Z})";
    }
}

readonlystructgenerator's People

Contributors

kurema avatar pierre3 avatar

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.