RSCG – Sundew.DiscriminatedUnions

RSCG – Sundew.DiscriminatedUnions
 
 

name Sundew.DiscriminatedUnions
nuget https://www.nuget.org/packages/Sundew.DiscriminatedUnions/
link https://github.com/sundews/Sundew.DiscriminatedUnions
author Kim Hugener Ohlsen

Generate tagged union

 

This is how you can use Sundew.DiscriminatedUnions .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Sundew.DiscriminatedUnions" Version="6.0.0" ></PackageReference>
	</ItemGroup>

	

</Project>


The code that you will use is


using UnionTypesDemo;

Console.WriteLine("Save or not");
ResultSave data = SaveToDatabase.Save(0);
var message= data switch
{
    ResultSave.Ok ok => $"Saved {ok.i}",
    ResultSave.NotFound => "Not found", 
};
Console.WriteLine(message);
data = SaveToDatabase.Save(1);
message = data switch
{
    ResultSave.Ok ok => $"Saved {ok.i}",
    ResultSave.NotFound => "Not found",
};
Console.WriteLine(message);




using Sundew.DiscriminatedUnions;

namespace UnionTypesDemo;


[DiscriminatedUnion]
public abstract partial record ResultSave
{

    public sealed partial record Ok(int i) : ResultSave;

    public sealed partial record NotFound():ResultSave ;
    
}




namespace UnionTypesDemo;

public class SaveToDatabase
{
    public static ResultSave Save(int i)
    {

        if (i == 0)
        {
            return ResultSave._NotFound;
        }
        return ResultSave._Ok(i); 
    }
}




 

The code that is generated is

#nullable enable

namespace UnionTypesDemo
{
#pragma warning disable SA1601
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sundew.DiscriminateUnions.Generator", "6.0.0.0")]
    public partial record ResultSave : global::Sundew.DiscriminatedUnions.IDiscriminatedUnion
#pragma warning restore SA1601
    {
        /// <summary>
        /// Gets the NotFound case.
        /// </summary>
        /// <returns>The NotFound.</returns>
        [Sundew.DiscriminatedUnions.CaseType(typeof(global::UnionTypesDemo.ResultSave.NotFound))]
        public static global::UnionTypesDemo.ResultSave _NotFound { get; }
            = new global::UnionTypesDemo.ResultSave.NotFound();

        /// <summary>
        /// Factory method for the Ok case.
        /// </summary>
        /// <param name="i">The i.</param>
        /// <returns>A new Ok.</returns>
        [Sundew.DiscriminatedUnions.CaseType(typeof(global::UnionTypesDemo.ResultSave.Ok))]
        public static global::UnionTypesDemo.ResultSave _Ok(int i)
            => new global::UnionTypesDemo.ResultSave.Ok(i);

        /// <summary>
        /// Gets all cases in the union.
        /// </summary>
        /// <returns>A readonly list of types.</returns>
        public static global::System.Collections.Generic.IReadOnlyList<global::System.Type> Cases { get; }
            = new global::System.Type[] { typeof(global::UnionTypesDemo.ResultSave.NotFound), typeof(global::UnionTypesDemo.ResultSave.Ok) };
    }
}

#nullable enable

namespace UnionTypesDemo
{
    public partial record ResultSave
    {
#pragma warning disable SA1601
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sundew.DiscriminateUnions.Generator", "6.0.0.0")]
        public partial record NotFound
#pragma warning restore SA1601
        {
            /// <summary>
            /// Gets all cases in the union.
            /// </summary>
            /// <returns>A readonly list of types.</returns>
            public new static global::System.Collections.Generic.IReadOnlyList<global::System.Type> Cases { get; }
                = new global::System.Type[] { typeof(global::UnionTypesDemo.ResultSave.NotFound) };
        }
    }
}

#nullable enable

namespace UnionTypesDemo
{
    public partial record ResultSave
    {
#pragma warning disable SA1601
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sundew.DiscriminateUnions.Generator", "6.0.0.0")]
        public partial record Ok
#pragma warning restore SA1601
        {
            /// <summary>
            /// Gets all cases in the union.
            /// </summary>
            /// <returns>A readonly list of types.</returns>
            public new static global::System.Collections.Generic.IReadOnlyList<global::System.Type> Cases { get; }
                = new global::System.Type[] { typeof(global::UnionTypesDemo.ResultSave.Ok) };
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Sundew.DiscriminatedUnions


Posted

in

, ,

by

Tags: