Skip to content

Commit

Permalink
Change tag default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
2A5F committed Feb 23, 2025
1 parent 5087f71 commit 44cfe92
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 0.9.0
## 0.10.0
- Breaking Changes
- Tags are now no longer in explicit assignment order, and are no longer 1 if the first item is void

## 0.9.0
- Breaking Changes
- Switch to the source package, and change the access rights of related classes from public to internal

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
Expand All @@ -19,7 +20,7 @@ public enum UnionCaseTypeKind
Class
}

public record struct UnionCase(string Name, string Tag, string Type, UnionCaseTypeKind Kind, bool IsGeneric);
public record struct UnionCase(string Name, string? Tag, string Type, UnionCaseTypeKind Kind, bool IsGeneric);

public record struct UnionAttr(string TagsName, bool ExternalTags, string ExternalTagsName, string? TagsUnderlying)
{
Expand Down Expand Up @@ -196,7 +197,9 @@ private void GenTags(string name, string spaces = " ")
sb.AppendLine($"{spaces}{{");
foreach (var @case in Cases)
{
sb.AppendLine($"{spaces} {@case.Name} = {@case.Tag},");
sb.Append($"{spaces} {@case.Name}");
if (@case.Tag != null) sb.Append($" = {@case.Tag}");
sb.AppendLine(",");
}
sb.AppendLine($"{spaces}}}");
}
Expand Down
9 changes: 7 additions & 2 deletions Coplt.Union.Analyzers/Generators/UnionGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using Coplt.Analyzers.Generators.Templates;
using Coplt.Union.Analyzers.Generators.Templates;
Expand Down Expand Up @@ -91,15 +92,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (member is MethodDeclarationSyntax mds)
{
var case_name = mds.Identifier.ToString();
var tag = $"{i + 1}";
var ret_type = mds.ReturnType.ToString();
var kind = UnionCaseTypeKind.None;
var member_symbol = (IMethodSymbol)semanticModel.GetDeclaredSymbol(mds)!;
var tag_attr = member_symbol.GetAttributes().FirstOrDefault(a =>
a.AttributeClass?.ToDisplayString() == "Coplt.Union.UnionTagAttribute");
string? tag = null;
if (tag_attr != null)
{
tag = tag_attr.ConstructorArguments.First().Value?.ToString() ?? tag;
tag = tag_attr.ConstructorArguments.First().Value?.ToString();
}
if (i == 0 && tag == null && ret_type != "void")
{
tag = "1";
}
var ret_type_symbol = member_symbol.ReturnType;
var is_generic = ret_type_symbol.IsNotInstGenericType();
Expand Down
6 changes: 5 additions & 1 deletion Coplt.Union.Source/Coplt.Union.Source.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<PackageId>Coplt.Union</PackageId>
<Version>0.9.0</Version>
<Version>0.10.0</Version>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
Expand Down Expand Up @@ -63,6 +63,10 @@
<Pack>True</Pack>
<PackagePath>/</PackagePath>
</None>
<None Include="..\CHANGELOG.md">
<Pack>True</Pack>
<PackagePath>/</PackagePath>
</None>
<None Include=".\Coplt.Union.props">
<Pack>True</Pack>
<PackagePath>build/</PackagePath>
Expand Down
2 changes: 1 addition & 1 deletion Coplt.Union.Utilities/Coplt.Union.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<RootNamespace>Coplt.Union.Utilities</RootNamespace>
<Version>0.9.0</Version>
<Version>0.10.0</Version>
<Description>Tagged union for c#</Description>
<PackageProjectUrl>https://github.com/coplt/Coplt.Union</PackageProjectUrl>
<RepositoryUrl>https://github.com/coplt/Coplt.Union</RepositoryUrl>
Expand Down
6 changes: 2 additions & 4 deletions Coplt.Union.Utilities/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public readonly partial struct Option<T>
[UnionTemplate]
private interface Template
{
T Some();

[UnionTag(0)]
void None();
T Some();
}

public Option() => this = MakeNone();
Expand All @@ -25,6 +23,6 @@ private interface Template
public T Value => Some;

public static implicit operator Option<T>(T value) => new(value);

public static explicit operator T(Option<T> value) => value.Value;
}

0 comments on commit 44cfe92

Please sign in to comment.