Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Updates
  • Loading branch information
cston committed Feb 22, 2021
commit e7d050899b0858c3ff89160e22466f15cda5501e
13 changes: 10 additions & 3 deletions proposals/csharp-10.0/using-alias-improvements.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ using MyType2<T, U> = T; // ok

Type parameter references in an alias target have the same restrictions as type parameter references in other generic contexts.
```C#
using MyType3<T, U> = T.U; // error: reported at use site only?
using MyType3<T, U> = T<U>; // error: reported at use site only?
using MyType3<T, U> = T.U; // error: type or namespace 'T' not found
using MyType3<T, U> = T<U>; // error: type or namespace 'T<>' not found
```

The same name may be used for multiple aliases with distinct arity.
Expand All @@ -31,11 +31,12 @@ using MyDictionary<U> = System.Collections.Generic.Dictionary<string, U>; // err
```

A partially bound type is not valid.
The `using` aliases below are both valid, and the use of `MyList<>` is valid because the expansion `List<>` is an unbound type, but the use of `MyDictionary<>` invalid because `Dictionary<string,>` is a partially bound type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an example of a valid use of MyDictionary. This section is still starting with the words "A partially bound type is not valid.", which doesn't really address the confusion.

```C#
using MyList<T> = System.Collections.Generic.List<T>;
using MyDictionary<T> = System.Collections.Generic.Dictionary<string, T>;

_ = typef(MyList<>); // ok: 'List<>'
_ = typeof(MyList<>); // ok: 'List<>'
_ = typeof(MyDictionary<>); // error: 'Dictionary<string,>' is not valid
```

Expand All @@ -54,6 +55,12 @@ using MaybeNull<T> = T? where T : class;
using Option<T> = System.Nullable<T> where T : struct;
```

Explicit constraints would also allow restricting the use of the target type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```C#
using ValueTypeList<T> = List<T> where T : struct;
static void F<T>(ValueTypeList<T> list) { ... } // error: 'T' must be a value type
```

### Variance
Type parameter variance cannot be specified explicitly in the alias declaration, because any resulting variance constraints would not be enforced at the public boundary of the assembly.

Expand Down