Skip to content

Commit fdb099d

Browse files
cursoragentAArnott
andcommitted
Make the polytype-middleware sample compile on every TFM
Use a downlevel null check, place the extension block in a top-level static class, and give the witness overload its own message. Co-authored-by: Andrew Arnott <andrew@mysideoftheweb.com>
1 parent ff824dd commit fdb099d

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

‎skills/polytype-middleware/SKILL.md‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ The SDK defines `NET` for .NET 5 and later. `net472` and `netstandard2.0` are `!
3838

3939
Friendly overloads only obtain a shape and forward. Real work takes `ITypeShape<T>` or `ITypeShapeProvider` and exists on every TFM, with no `#if`.
4040

41-
The sample is one method, `ShapeMiddleware.Process`. Copy this split for each operation. `IShapeable<T>`, `ITypeShape<T>`, and `TypeShapeResolver` come from PolyType. `PreferDotNetAlternativeApi` is an attribute the library defines so an analyzer can see the dynamic member.
41+
The sample is one method, `ShapeMiddleware.Process`. Copy this split for each operation. `IShapeable<T>`, `ITypeShape<T>`, and `TypeShapeResolver` come from PolyType. `PreferDotNetAlternativeApi` is an attribute the library defines so an analyzer can see the dynamic member. Null checks throw `ArgumentNullException` directly. `ArgumentNullException.ThrowIfNull` is not in the `net472` or `netstandard2.0` reference assemblies. The `extension` block sits in a top-level static class. C# rejects that block at namespace scope.
4242

4343
```csharp
4444
public sealed class ShapeMiddleware
4545
{
4646
// Real work. Every TFM.
4747
public void Process<T>(T? value, ITypeShape<T> shape)
4848
{
49-
ArgumentNullException.ThrowIfNull(shape);
49+
if (shape is null)
50+
{
51+
throw new ArgumentNullException(nameof(shape));
52+
}
53+
5054
// use shape
5155
}
5256

@@ -61,39 +65,50 @@ public sealed class ShapeMiddleware
6165
#endif
6266
}
6367

64-
extension(ShapeMiddleware self)
68+
public static class ShapeMiddlewareExtensions
6569
{
70+
extension(ShapeMiddleware self)
71+
{
6672
#if NET
67-
[PreferDotNetAlternativeApi("Use the method that constrains T : IShapeable<T>.")]
68-
[EditorBrowsable(EditorBrowsableState.Never)]
69-
[Obsolete("T must implement IShapeable<T>, or pass a witness type.", error: true)] // omit Obsolete when this fallback must still compile
73+
[PreferDotNetAlternativeApi("Use the method that constrains T : IShapeable<T>.")]
74+
[EditorBrowsable(EditorBrowsableState.Never)]
75+
[Obsolete("T must implement IShapeable<T>, or pass a witness type.", error: true)] // omit Obsolete when this fallback must still compile
7076
#endif
71-
public void Process<T>(T? value)
72-
{
73-
ArgumentNullException.ThrowIfNull(self);
74-
self.Process(value, TypeShapeResolver.ResolveDynamicOrThrow<T>());
75-
}
77+
public void Process<T>(T? value)
78+
{
79+
if (self is null)
80+
{
81+
throw new ArgumentNullException(nameof(self));
82+
}
83+
84+
self.Process(value, TypeShapeResolver.ResolveDynamicOrThrow<T>());
85+
}
7686

7787
#if NET
78-
[PreferDotNetAlternativeApi("Use the method that constrains T : IShapeable<T>.")]
79-
[EditorBrowsable(EditorBrowsableState.Never)]
80-
[Obsolete("T must implement IShapeable<T>, or pass a witness type.", error: true)] // omit Obsolete when this fallback must still compile
88+
[PreferDotNetAlternativeApi("Use the method that constrains TProvider : IShapeable<T>.")]
89+
[EditorBrowsable(EditorBrowsableState.Never)]
90+
[Obsolete("TProvider must implement IShapeable<T>.", error: true)] // omit Obsolete when this fallback must still compile
8191
#endif
82-
public void Process<T, TProvider>(T? value)
83-
{
84-
ArgumentNullException.ThrowIfNull(self);
85-
self.Process(value, TypeShapeResolver.ResolveDynamicOrThrow<T, TProvider>());
92+
public void Process<T, TProvider>(T? value)
93+
{
94+
if (self is null)
95+
{
96+
throw new ArgumentNullException(nameof(self));
97+
}
98+
99+
self.Process(value, TypeShapeResolver.ResolveDynamicOrThrow<T, TProvider>());
100+
}
86101
}
87102
}
88103
```
89104

90-
`where T : IShapeable<T>` calls `T.GetTypeShape()`. That is the witness-free API for `[GenerateShape]` on `T`. `where TProvider : IShapeable<T>` calls `TProvider.GetTypeShape()`. That is the witness API for `[GenerateShapeFor<T>]`. `T` itself is not constrained on the witness member. The witness extension takes `TProvider` as a type argument only. It does not repeat `where TProvider : IShapeable<T>`.
105+
`where T : IShapeable<T>` calls `T.GetTypeShape()`. That is the witness-free API for `[GenerateShape]` on `T`. `where TProvider : IShapeable<T>` calls `TProvider.GetTypeShape()`. That is the witness API for `[GenerateShapeFor<T>]`. `T` itself is not constrained on the witness member. The witness extension takes `TProvider` as a type argument only. It does not repeat `where TProvider : IShapeable<T>`. Its `PreferDotNetAlternativeApi` and `Obsolete` text names `TProvider`, the type that must provide the shape.
91106

92107
A caller whose type has `[GenerateShape]` writes `middleware.Process(value)` on modern .NET. A type the caller does not own, including a top-level array, needs a witness: `middleware.Process<ExternalType, Witness>(value)`. Passing an `ITypeShape<T>` or `ITypeShapeProvider` explicitly works on every TFM with no source generator: `middleware.Process(value, shape)`.
93108

94109
The first two calls compile on every TFM. On modern .NET they bind to the members declared on the type. On downlevel they bind to the extension, so the file needs the namespace import. State that in the remarks on the type.
95110

96-
A static operation uses the same split. Declare the constrained method on the type under `#if NET`. Put the unconstrained member in the extension block, on every TFM. Consumers need C# 14 (`LangVersion` 14) to call extension members, including when the project targets .NET Framework or .NET Standard. The runtime does not need to be modern. Guard the extension block from a documentation tool only when that tool cannot parse it.
111+
A static operation uses the same split. Declare the constrained method on the type under `#if NET`. Put the unconstrained member in an `extension` block inside a top-level static class, on every TFM. Consumers need C# 14 (`LangVersion` 14) to call extension members, including when the project targets .NET Framework or .NET Standard. The runtime does not need to be modern. Guard the extension block from a documentation tool only when that tool cannot parse it.
97112

98113
When the dynamic member cannot be an extension because the type already has an instance method with those parameters, give the dynamic member a different name. A second instance method that differs only by the constraint is illegal. Keep the constrained member's name under `#if NET`, and compile the distinctly named dynamic member for every TFM with the same parameters and the same `#if NET` attributes.
99114

0 commit comments

Comments
 (0)