Skip to content

Champion: using aliases for any types (VS 17.6, .NET 8) #4284

Closed
@agocke

Description

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

  1. 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 marked unsafe. 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 form using unsafe X = T*;

    1. Using a pointer type in an alias not marked with unsafe will be an error.
    2. It will be an error if using unsafe X = T*; is written and T is not a type that is valid to take a pointer to. Regardless if 'X' is not referenced anywhere in the code.
    3. 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.
  2. 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:

    1. It is an error to have an alias to a top-level NRT type. e.g. using X = string?; is not legal.
    2. using X = List<string?>; remains legal as there is no top-level NRT, just an interior NRT.
    3. using X = int?; remains legal as this is a top-level value type, not a reference type.
  3. For unsafe code, should the syntax be using unsafe X = int*; or unsafe using X = int*;. i.e. does the unsafe modifier come before or after the using keyword.

    Answer (LDM meeting 2/1/23):

    1. The syntax will be using unsafe .... This keeps using blocks consistent with using always being the first token.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions