Add exploration tutorial for unions and closed hierarchies#54627
Add exploration tutorial for unions and closed hierarchies#54627BillWagner wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds two new exploratory tutorials under the C# “What’s new” section to teach upcoming preview language features: union types and closed type hierarchies. It also introduces a shared “TelemetryMonitor / SmartHome” sample used by the tutorials via snippet-backed code files.
Changes:
- Adds new tutorials: Explore union types and Explore closed hierarchies, and links them from
docs/csharp/toc.yml. - Adds a multi-project sample (
SmartHome.Core,SmartHome.App, andSmartHome.Extensions) with snippet regions referenced by both tutorials. - Introduces shim support types (
UnionAttribute,IUnion, andIsClosedTypeAttribute) to allow the sample to compile when the SDK doesn’t yet ship the required runtime types.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/csharp/whats-new/tutorials/unions.md | New unions tutorial content, including referenced snippets and expected output. |
| docs/csharp/whats-new/tutorials/closed-hierarchies.md | New closed-hierarchies tutorial content, including referenced snippets and expected output. |
| docs/csharp/toc.yml | Adds both new tutorials to the “What’s new → Tutorials” section. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/SmartHome.Core.csproj | Adds Core sample project for snippets. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Readings.cs | Adds Reading union sample and consumption switch. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Result.cs | Adds “before” result type and Result<T> union sample + consumption. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Sample.cs | Adds generic union (Sample<T>) and numeric normalization example. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Quantity.cs | Adds custom union-like type example for non-boxing cases. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Sensors.cs | Adds closed hierarchy sample and exhaustive switch. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/Report.cs | Adds generic closed hierarchy sample + fold/count example. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Core/UnionSupport.cs | Adds temporary shim types required for preview language features. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.App/SmartHome.App.csproj | Adds console app that exercises the sample scenarios. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.App/Program.cs | Demo program used by both tutorials via snippet regions. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Extensions/SmartHome.Extensions.csproj | Adds extensions project used to demonstrate the “escape hatch” case. |
| docs/csharp/whats-new/tutorials/snippets/telemetry-monitor/SmartHome.Extensions/DoorContact.cs | Adds cross-assembly subtype for the open Contact leaf case. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
You might need to add a polyfill
| - The .NET SDK that includes the closed hierarchies preview feature. Download it from the [.NET download site](https://dotnet.microsoft.com/download/dotnet). | ||
| - An editor such as Visual Studio or Visual Studio Code with the C# Dev Kit. |
There was a problem hiding this comment.
These bullets seem to be lacking an intro statement.
|
|
||
| :::code language="csharp" source="snippets/telemetry-monitor/SmartHome.Core/Sensors.cs" id="SensorExhaustive"::: | ||
|
|
||
| 1. Build the project. In earlier previews, you need to add a polyfill for the `Closed` attribute: |
There was a problem hiding this comment.
Is "earlier previews" clear to readers?
|
|
||
| This tutorial uses preview language features. You need an SDK that supports union types and a language version set to `preview`. | ||
|
|
||
| - The .NET 11 SDK that includes the union types preview feature. Download it from the [.NET download site](https://dotnet.microsoft.com/download/dotnet). |
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
adegeo
left a comment
There was a problem hiding this comment.
Here's feedback on closed. I'll follow up with union soon.
| @@ -0,0 +1,63 @@ | |||
| using SmartHome.Core; | |||
There was a problem hiding this comment.
This folder structure is violating the snippet design we have. If it's shared by multiple articles, it should be in a shared folder. For example, move the entire telementry-monitor folder:
docs/csharp/whats-new/tutorials/snippets/shared/telemetry-monitor/
|
|
||
| This tutorial uses preview language features. You need an SDK that supports closed hierarchies and a language version set to `preview`. | ||
|
|
||
| - The .NET SDK that includes the closed hierarchies preview feature. Download it from the [.NET download site](https://dotnet.microsoft.com/download/dotnet). |
There was a problem hiding this comment.
Should this be .NET 11 Preview X? The user doesn't really know what to download from this statement.
|
|
||
| You build a class library, `SmartHome.Core`, that holds the closed hierarchy, a second library, `SmartHome.Extensions`, that extends an open case, and a console app, `SmartHome.App`, that drives them. | ||
|
|
||
| 1. Create the solution and projects: |
There was a problem hiding this comment.
Tutorials should be explicit in steps. Here the first step should be to open a terminal and run the following dotnet commands from a folder previously created to store the code of the tutorial.
| 1. In each project file, set the language version to `preview`: | ||
|
|
||
| ```xml | ||
| <PropertyGroup> | ||
| <LangVersion>preview</LangVersion> | ||
| </PropertyGroup> | ||
| ``` |
There was a problem hiding this comment.
They could probably use --langVersion preview on all of the dotnet new commands in the previous step. If so, I would keep this here for reference, as an optional step. Someone may follow the tutorial but hate creating the new projects via terminal and prefer to use an IDE. If so, they'll need to know how to set the preview version.
| --- | ||
| # Tutorial: Explore C# closed hierarchies | ||
|
|
||
| A *closed hierarchy* restricts the direct subtypes of a base type to the assembly that declares it. Because the compiler knows the full set of subtypes, it can verify that a switch handles every one without a default arm. Closed hierarchies suit domains where the set of cases is stable and you want the compiler to flag every place that needs to change when you add a case. |
There was a problem hiding this comment.
...it can verify that a switch handles every one without a default arm...
Should it be explicit with switch expression or switch expression pattern?
|
|
||
| :::code language="csharp" source="snippets/telemetry-monitor/SmartHome.Core/Sensors.cs" id="ClosedSensor"::: | ||
|
|
||
| 1. In the same file, add a method that matches every sensor with a switch: |
There was a problem hiding this comment.
| 1. In the same file, add a method that matches every sensor with a switch: | |
| 1. In the same file, add a method that matches every sensor with a switch expression: |
|
|
||
| :::code language="csharp" source="snippets/telemetry-monitor/SmartHome.Core/Sensors.cs" id="SensorExhaustive"::: | ||
|
|
||
| 1. Build the project. In earlier previews, you need to add a polyfill for the `Closed` attribute: |
There was a problem hiding this comment.
I'm not sure what the reader is supposed to do. Are they supposed to add this? Only the latest preview is supported, so we should code directly for that and not include information that can confuse the reader.
|
|
||
| ## Run the sample | ||
|
|
||
| The console app builds each sensor and report, then prints them through the exhaustive switches: |
There was a problem hiding this comment.
I think this can be more expressive. It should tell the reader what file to open, to paste in the code, then to run it.
|
|
||
| You built the sensor model of a smart-home telemetry monitor and, in the process, worked through closed-hierarchy scenarios. You: | ||
|
|
||
| - Declared a `closed` `Sensor` base type and matched its subtypes with an exhaustive switch that needs no default arm. |
There was a problem hiding this comment.
nit: is this better?
| - Declared a `closed` `Sensor` base type and matched its subtypes with an exhaustive switch that needs no default arm. | |
| - Declared a `closed Sensor` base type and matched its subtypes with an exhaustive switch that needs no default arm. |
Fixes #52322
Fixes #54015
Add two exploration tutorials for developers to learn about the
uniontypes andclosedtype hierarchies.Internal previews