Repository navigation
Open
Labels
enhancementNew feature or requestpackage/analyzersAffects CodoMetis.TypeKit.Analyzerspackage/coreAffects CodoMetis.TypeKit
Milestone
Description
What you're trying to do
Constrain a type argument to the kinds C# 15 adds. You can't write where T : union or where T : closed. The analyzer's constraint attributes (TypeArgumentConstraints.cs, checked by CMTK0010) cover abstract, sealed, record, concrete, interface, value-object, closed-set, flags-enum and readonly-struct, but nothing for a union or a closed hierarchy.
What you'd like
public static string Describe<[UnionType] T>(T value) where T : notnull { ... }
public static IReadOnlyList<Type> Cases<[ClosedHierarchy] T>() { ... }- Two new attributes. One admits only a union, compiler-made or custom. The other admits only a
closedclass.- Names: decided in the PR.
[Union]collides withSystem.Runtime.CompilerServices.UnionAttribute, which the compiler puts on a union.[Closed]reads like[ClosedSet], andclosedis now a contextual keyword.
- Names: decided in the PR.
- How the analyzer recognises them. The newest
Microsoft.CodeAnalysis.CSharpon nuget.org is 5.9.0, withoutITypeSymbol.IsUnion, which is still in Roslyn'sPublicAPI.Unshipped.txt. So the analyzer resolves the BCL types by metadata name, as it already resolvesSystem.Func1andSystem.Linq.IQueryable1:UnionAttributeandIUnionfor a union. To measure: howclosedis emitted in metadata, an attribute or a flag. - The existing attributes' answers for the new kinds. Add union and
closedrows toTypeArgumentKinds(test/CodoMetis.TypeKit.Analyzers.Tests/Variants/) and decide each attribute's answer:- A
closedclass is implicitly abstract:[Abstract]admits it and[Concrete]refuses it. - From another assembly, a
closedclass can't be derived from. Decide what[Unsealed]says about it, in line with the 3.0.0 roadmap item on abstract classes nothing can derive from. - A union is a struct: decide
[Sealed],[ReadOnlyStruct],[ValueObject]and[Record]from the emitted shape. - Decision 34 applies. Before it ships, measure what CMTK0010 answers today for the new kinds, because a change that reports more under an Error waits for 3.0.0.
- A
- Targets. The attributes go on both targets. A
net10.0library's generic method is often called from anet11.0application that passes a union. - Reach. 2.4.0 moves to Metalama 2027.0 (decision 129), so a woven
net11.0project declares and matches unions at C# 15 (KB-060). Test the attribute in a woven project.
Does it change something that compiles or works today?
No, it only adds. The one exception would be a changed answer for an existing attribute (above), which is measured first.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestpackage/analyzersAffects CodoMetis.TypeKit.Analyzerspackage/coreAffects CodoMetis.TypeKit
Activity