Skip to content

Commit 38d6c94

Browse files
committed
Initial overview of unions
This represents a first attempt at how I plan to explain discriminated unions to our customers.
1 parent 318bfca commit 38d6c94

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Conceptual overview
2+
3+
> The text that follows is the working draft for how we'll introduce this concept to customers. It's meant to address two distinct challenges:
4+
>
5+
> - Many of the scenarios for unions can also be handled by inheritance. That prompts the question on the value of adding unions to C#.
6+
>
7+
> - Unions have different meanings in different computer languages. There are tagged unions, type unions, C-style (unsafe) unions. Which do we mean?
8+
>
9+
> The language is intended to be less formal than the specification and serve as an introduction for customers.
10+
11+
*Discriminated unions* provide a new way to express the concept that *an expression may be one of many known types*.
12+
13+
C# already provides syntax for special cases of discriminated unions. Nullable types express one example: A `int?` is either an `int`, or nothing. A `string?` is either a `string` value or nothing. Another example is `enum` types. An `enum` type represents a closed set of values. The expression is limited to one of those values.
14+
15+
## Closed set of possibilities
16+
17+
The `enum` example highlights one limitation in C# now: An `enum` is represented by an underlying integral type. The compiler issues a warning if an invalid integral value is assigned to a variable of an `enum` type. But, that's the limit of the enforcement. Therefore, when you use an `enum` type in a `switch` expression, you must also check for invalid values using a discard (`_`) pattern.
18+
19+
C# developers often use *inheritance* to express that an expression is *one of many types*. You declare that an expression is of a base class, and it could be any class derived from that type. Inheritance differs from union types in two ways. Most importantly, a union represents one of a *known* set of types. An inheritance hierarchy likely includes derived classes beyond the known set of derived types. Secondly, a union doesn't require an inheritance relationship. A union can represent *one of many known `struct` types*, or even a union of some `struct` types and some `class` types. Inheritance and unions have some overlap in expressiveness, but both have unique features as well.
20+
21+
## On to examples
22+
23+
Add examples based on our existing design.

0 commit comments

Comments
 (0)