Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Here's an example of some of the new features that **PolySharp** can enable down
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771))
- Union types (needed for [C# 15 union types](https://devblogs.microsoft.com/dotnet/csharp-15-union-types/))
- `[Union]`
- `IUnion`

To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <auto-generated/>
#pragma warning disable
#nullable enable annotations

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Defines the contract for union types, providing access to the underlying value.
/// </summary>
internal interface IUnion
{
/// <summary>
/// Gets the underlying value of the union.
/// </summary>
object? Value { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <auto-generated/>
#pragma warning disable
#nullable enable annotations

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that a type is a union type, allowing the C# compiler to recognize it for union type features.
/// </summary>
[global::System.AttributeUsage(
global::System.AttributeTargets.Class |
global::System.AttributeTargets.Struct,
AllowMultiple = false)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class UnionAttribute : global::System.Attribute
{
}
}
19 changes: 19 additions & 0 deletions tests/PolySharp.Tests/LanguageFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,22 @@ public static void AnotherCpuIntrinsic([ConstantExpected(Min = 0, Max = 8)] int
{
}
}

// UnionAttribute, IUnion
[Union]
internal sealed class TestUnion : IUnion
{
private readonly object? value;

public TestUnion(int value)
{
this.value = value;
}

public TestUnion(string value)
{
this.value = value;
}

public object? Value => this.value;
}