흔히 F# 같은 함수형 프로그래밍 언어들에서 널리 쓰이는 기능인 대수적 자료형 (algebaric data types), 이른바 태그된 공용체 (tagged unions)가 C# 15에 드디어 들어오게 될 것 같습니다. 거의 10년 동안 기다려 왔던 기능이라 너무 설레네요.
# Unions
Champion issue: https://github.com/dotnet/csharplang/issues/9662
## Summary
[summary]: #summary
*Unions* is a set of interlinked features, that combine to provide C# support for union types:
- *Union types*: Structs and classes that have a `[Union]` attribute are recognized as *union types*, and support the *union behaviors*.
- *Case types*: Union types have a set of *case types*, which is given by parameters to constructors and factory methods.
- *Union behaviors*: Union types support the following *union behaviors*:
- *Union conversions*: There are implicit *union conversions* from each case type to a union type.
- *Union matching*: Pattern matching against union values implicitly "unwraps" their contents, applying the pattern to the underlying value instead.
- *Union exhaustiveness*: Switch expressions over union values are exhaustive when all case types have been matched, without need for a fallback case.
- *Union nullability*: Nullability analysis has enhanced tracking of the null state of a union's contents.
- *Union patterns*: All union types follow a basic *union pattern*, but there are additional optional patterns for specific scenarios.
- *Union declarations*: A shorthand syntax allows declaration of union types directly. The implementation is "opinionated" - a struct declaration that follows the basic union pattern and stores the contents as a single reference field.
- *Union interfaces*: A few interfaces are known by the language and used in its implementation of union declarations.
This file has been truncated. show original
9개의 좋아요