Champion: using aliases for any types (VS 17.6, .NET 8) #4284
Description
- Proposed
- Prototype: Add support for
using alias = any_type
. roslyn#50167 - Implementation: Done
- Specification: Proposal: Create using-alias-types.md #5174
Summary
Simply put,
using T = System.Collections.Generic.List<(string, int)>;
is legal. While
using T = (string, int);
is not. I suspect this was simply an oversight, but either way there seems no good reason that it shouldn't work (and it parses terribly and does not produce a good error message either).
Motivation
Language consistency, completing things we started. Without this syntax you cannot give tuple names either, which is a big downside.
Language Change
The grammar will be changed like so:
using_alias_directive
- : 'using' identifier '=' namespace_or_type_name ';'
+ : 'using' 'unsafe'? identifier '=' namespace_or_type';'
;
Concluded Questions
-
Similarly, what are the semantics of a pointer type in an alias. e.g.
using XPtr = X*;
. Using aliases are in a location that generally cannot be markedunsafe
. So we'd likely want this to be legal, with an error if you then use XPtr in an safe context.Answer:
Aliases to pointers are allowed. However, they must be written in the formusing unsafe X = T*;
- Using a pointer type in an alias not marked with
unsafe
will be an error. - It will be an error if
using unsafe X = T*;
is written andT
is not a type that is valid to take a pointer to. Regardless if 'X' is not referenced anywhere in the code. - It will be an error if
using unsafe X = T*;
would be ok, but the user has not passed the/unsafe
flag to the compiler.
- Using a pointer type in an alias not marked with
-
What are the semantics of a NRT nullable type with an alias. e.g.
using X = string?;
. Specifically, does the alias have to be an a#nullable enable
region? Or can it be located anywhere. Then, what does it mean if that alias is used in a context that doesn't allow nullable? Is that an error, a warning, or is the nullable annotation silently ignored?Answer:
- It is an error to have an alias to a top-level NRT type. e.g.
using X = string?;
is not legal. using X = List<string?>;
remains legal as there is no top-level NRT, just an interior NRT.using X = int?;
remains legal as this is a top-level value type, not a reference type.
- It is an error to have an alias to a top-level NRT type. e.g.
-
For unsafe code, should the syntax be
using unsafe X = int*;
orunsafe using X = int*;
. i.e. does theunsafe
modifier come before or after theusing
keyword.Answer (LDM meeting 2/1/23):
- The syntax will be
using unsafe ...
. This keepsusing
blocks consistent withusing
always being the first token.
- The syntax will be
LDM Discussions
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-09-20.md#type-alias-improvements
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#using-aliases-for-any-type
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-28.md#ungrouped
https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-01-11.md#using-aliases-for-any-types
Activity