Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT committed Jun 24, 2024
1 parent 65e6c87 commit 3eccef8
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions docs/design/features/byreflike-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,39 @@ With the above IL composition implemented, the following C# describes the follow

```csharp
struct S {}
struct S<T> {}
ref struct RS {}
ref struct RS<T> {}
interface I {}
class C {}
class C<T> {}

// Not currently valid C#
void M<T, U>(T t) where T: allows ref struct
{
if (t is int i) // Valid
if (t is S vc) // Valid
if (t is RS rs) // Valid
if (t is Span<char> s) // Valid
if (t is string str) // Valid
if (t is I itf) // Valid
if (t is C c) // Valid
if (t is C<I> ci) // Valid
if (t is C<U> cu) // Invalid
if (t is U u) // Invalid
if (t is Span<U> su) // Invalid
// Valid
if (t is int i)

if (t is S s)
if (t is S<char> sc)
if (t is S<U> su)

if (t is RS rs)
if (t is RS<char> rsc)
if (t is RS<U> rsu)

if (t is string str)
if (t is C c)
if (t is C<I> ci)
if (t is C<U> cu)

// Can be made to work in IL.
if (t is I itf) // A new local "I" would not be used for ByRefLike scenarios.
// The local would be the ByRefLike type, not "I".
// Invalid
if (t is object o) // ByRefLike types evaluate "true" for object.
if (t is U u)
}
```

Expand All @@ -179,6 +193,10 @@ namespace System.Runtime.CompilerServices
where TTo: allows ref struct;

// Replacement for the [box; isinst; unbox.any] sequence.
// Would throw InvalidCastException for invalid use at run-time.
// For example:
// TFrom: RS, TTo: object => always throws
// TFrom: RS, TTo: <interface> => always throws
public static TTo CastTo<TFrom, TTo>(TFrom source)
where TFrom: allows ref struct
where TTo: allows ref struct;
Expand Down

0 comments on commit 3eccef8

Please sign in to comment.