Skip to content

Commit

Permalink
Release v1.7.0 (#116)
Browse files Browse the repository at this point in the history
* Release v1.7.0 and update readme.
  • Loading branch information
domn1995 committed Jan 23, 2023
1 parent 3b2fe4c commit f835a5a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
74 changes: 66 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ partial record Shape
partial record Rectangle(double Length, double Width);
partial record Triangle(double Base, double Height);
}
```

```cs
// 4. Use the union members.
var shape = new Shape.Rectangle(3, 4);
var area = shape.Match(
Expand All @@ -42,7 +44,7 @@ Use generics for more advanced union types. For example, an option monad:
```cs
// 1. Import the namespace.
using Dunet;
// Optional: use aliasing for more terse code.
// Optional: use static import for more terse code.
using static Option<int>;

// 2. Add the `Union` attribute to a partial record.
Expand All @@ -53,7 +55,9 @@ partial record Option<T>
partial record Some(T Value);
partial record None();
}
```

```cs
// 4. Use the union members.
Option<int> ParseInt(string? value) =>
int.TryParse(value, out var number)
Expand All @@ -79,14 +83,14 @@ Console.WriteLine(output); // "12345".

## Implicit Conversion Support

Dunet generates implicit conversions between union member values and the union type
if your union meets all of the following conditions:
Dunet generates implicit conversions between union member values and the union
type if your union meets all of the following conditions:

- All members contain only a single parameter.
- All parameters are a different type.
- No parameters are an interface type.

For example, consider a `Result` union type that represents success
For example, consider a `Result` union type that represents success
as a `double` and failure as an `Exception`:

```cs
Expand All @@ -100,7 +104,9 @@ partial record Result
partial record Success(double Value);
partial record Failure(Exception Error);
}
```

```cs
// 3. Return union member values directly.
Result Divide(double numerator, double denominator)
{
Expand All @@ -125,7 +131,8 @@ Console.WriteLine(output); // "Cannot divide by zero!"

## Async Match Support

Dunet generates a `MatchAsync()` extension method for all `Task<T>` and `ValueTask<T>` where `T` is a union type. For example:
Dunet generates a `MatchAsync()` extension method for all `Task<T>` and
`ValueTask<T>` where `T` is a union type. For example:

```cs
// Choice.cs
Expand Down Expand Up @@ -160,17 +167,66 @@ static async Task<Choice> AskAsync()
}

// 4. Asynchronously match any union `Task` or `ValueTask`.
var response = await AskAsync().MatchAsync(yes => "Yes!!!", no => $"No, {no.Reason}");
var response = await AskAsync()
.MatchAsync(
yes => "Yes!!!",
no => $"No, {no.Reason}"
);

Console.WriteLine(response); // Prints "No, because I don't wanna!" after 1 second.
// Prints "No, because I don't wanna!" after 1 second.
Console.WriteLine(response);
```

> **Note**:
> `MatchAsync()` can only be generated for namespaced unions.
## Match on Specific Union Member

Dunet generates specific match methods for each union member. This is useful
when unwrapping a union and you only care about transforming a single variant.
For example:

```cs
[Union]
partial record Shape
{
partial record Point(int X, int Y);
partial record Line(double Length);
partial record Rectangle(double Length, double Width);
partial record Sphere(double Radius);
}
```

```cs
public static bool IsZeroDimensional(this Shape shape) =>
shape.MatchPoint(
point => true,
() => false
);

public static bool IsOneDimensional(this Shape shape) =>
shape.MatchLine(
line => true,
() => false
);

public static bool IsTwoDimensional(this Shape shape) =>
shape.MatchRectangle(
rectangle => true,
() => false
);

public static bool IsThreeDimensional(this Shape shape) =>
shape.MatchSphere(
sphere => true,
() => false
);
```

## Nested Union Support

To declare a union nested within a class or record, the class or record must be `partial`. For example:
To declare a union nested within a class or record, the class or record must
be `partial`. For example:

```cs
// This type declaration must be partial.
Expand All @@ -188,7 +244,9 @@ public partial class Parent1
}
}
}
```

```cs
// Access union members like any other nested type.
var member1 = new Parent1.Parent2.Nested.Member1();
```
Expand Down
9 changes: 5 additions & 4 deletions src/Dunet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/domn1995/dunet</RepositoryUrl>
<PackageTags>source; generator; discriminated; union; functional; tagged;</PackageTags>
<AssemblyVersion>1.6.0</AssemblyVersion>
<FileVersion>1.6.0</FileVersion>
<AssemblyVersion>1.7.0</AssemblyVersion>
<FileVersion>1.7.0</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>1.6.0 - Support generator cancellation.
<PackageReleaseNotes>1.7.0 - Match on specific union member.
1.6.0 - Support generator cancellation.
1.5.0 - Support async Action match functions.
1.4.2 - Disables implicit conversions when union member is an interface type.
1.4.1 - Disable CS1591 in generated code.
Expand All @@ -42,7 +43,7 @@
<RepositoryType>git</RepositoryType>
<PackageIcon>favicon.png</PackageIcon>
<SignAssembly>False</SignAssembly>
<Version>1.6.0</Version>
<Version>1.7.0</Version>
<NeutralLanguage>en</NeutralLanguage>
<DevelopmentDependency>true</DevelopmentDependency>
<NoWarn>$(NoWarn);NU5128</NoWarn>
Expand Down

0 comments on commit f835a5a

Please sign in to comment.