RSCG – Immutype
RSCG – Immutype
name | Immutype |
nuget | https://www.nuget.org/packages/Immutype/ |
link | https://github.com/DevTeam/Immutype |
author | Nikolay Pianikov |
Immutable from constructors
This is how you can use Immutype .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Immutype" Version="1.0.14" OutputItemType="Analyzer" > <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
using ImmutypeDemo; Person p = new("Andrei","Ignat"); var p2= p.WithFirstName("Test"); Console.WriteLine(p2.LastName);
namespace ImmutypeDemo; [Immutype.Target] internal class Person { public string? FirstName; public Person() { } public Person(string? FirstName,string LastName) { this.FirstName = FirstName; this.LastName = LastName; } public int ID { get; set; } public string? LastName { get; set;} }
The code that is generated is
// ReSharper disable CheckNamespace // ReSharper disable ClassNeverInstantiated.Global namespace Immutype { using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor, Inherited = false)] public class TargetAttribute: Attribute { } }
namespace ImmutypeDemo; using System.Collections.Generic; using System.Linq; [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static partial class PersonExtensions{ [System.Runtime.CompilerServices.MethodImplAttribute((System.Runtime.CompilerServices.MethodImplOptions)256),System.Diagnostics.Contracts.PureAttribute] public static ImmutypeDemo.Person WithFirstName(this ImmutypeDemo.Person it,string? FirstName){ if( it==default(ImmutypeDemo.Person))throw new System.ArgumentNullException("it"); return new ImmutypeDemo.Person(FirstName, it.LastName );} [System.Runtime.CompilerServices.MethodImplAttribute((System.Runtime.CompilerServices.MethodImplOptions)256),System.Diagnostics.Contracts.PureAttribute] public static ImmutypeDemo.Person WithLastName(this ImmutypeDemo.Person it,string LastName){ if( it==default(ImmutypeDemo.Person))throw new System.ArgumentNullException("it"); if(LastName==default(string ))throw new System.ArgumentNullException("LastName"); return new ImmutypeDemo.Person( it.FirstName,LastName);}}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Immutype
Leave a Reply