-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Champion "permit t is null
for unconstrained type parameter" (16.3, Core 3)
#1284
Comments
Good change, seems that Thank to JetBrains for the new feature "heap allocator". Actually analyzer suggest a suspicious code optimization for a generic parameter which cause boxing allocations with value types. Reported to https://youtrack.jetbrains.com/issue/RIDER-13249 |
If you intend this to only be used on reference types (which would make sense given they're the only things that can be null), you can do If you do want this to work on value types as well (not sure why..., but maybe you have some generic stuff that wants to work on both, but deal with null reference types specially), then you can write it as above and know that the CLR should optimize this such that your code will not box for value types. |
@CyrusNajmabadi T v = ...
v is T /* no boxing */
v != null /* possible boxing */
v is null /* ? use non-boxing optimization [more expected for me] */
v == null /* possible boxing */ |
@CyrusNajmabadi public static bool IsNull<T>(this T o) => o == null; /* may cause boxing */
public static bool IsNull<T>(this T o) => o is T ? false : true; /* non-boxing */ I suspect that the new implementation will be equals to the second. public static bool IsNull<T>(this T o) => o is null; /* non-boxing */ What do you think about it? |
My point is that |
@CyrusNajmabadi |
@reinspi (previously known as Makeloft) Please stop posting in this repo. We are long past any interest in your equating extension methods with pattern-matching. |
Added:
|
What would be the scope of this addition? Would it be limited to numbers, or could I do |
So this is interesting... for anyone following along at home: using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using BenchmarkDotNet.Running;
namespace NullBenchmark
{
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<IsNullVersusIsT>();
}
}
static class NullCheckExtensions
{
public static bool IsNull<T>(this T t) => t == null;
public static bool IsT<T>(this T t) => t is T;
}
[RyuJitX64Job]
[LegacyJitX86Job]
[MemoryDiagnoser]
public class IsNullVersusIsT
{
[Benchmark]
public bool IsNull() => 42.IsNull();
[Benchmark]
public bool IsT() => 42.IsT();
}
} Results on .NET Framework:
Results on CoreCLR (and slower CPU):
It would appear that I have no idea why |
This is specifically for the number '3' 😉 Seriously, the proposal is to permit a constant pattern of any type when the value to be matched is an "open type". |
Fixed in dotnet/roslyn#25995 for C# 8.0. |
Is 8.0 the next expected release after 7.3? |
Should this only be allowed in C# 8? The current compiler happily compiles The problem this created for the OP is that their C# 7.2 program developed in VS 2019 does not build on a 2017 build server. |
@dasMulli It was only intended to be permitted in C# 8. I will investigate and file a bug and fix VS2019 (to not allow this) if needed. |
@dasMulli That was a bug that was fixed in dotnet/roslyn#34695. |
Thanks! I was only searching for |
t is null
for unconstrained type parameter"t is null
for unconstrained type parameter" (16.3, Core 3)
Currently it is an error to write
if (t is null) ...
when the type oft
is a type parameter that is not constrained to be a reference type. We should permit this to allow people to write that instead ofif (t == null) ...
uniformly when testing fornull
values. A code fixer to do just that has to have a special case for this and it feels quite irregular. See dotnet/roslyn#24173. See also #1261.The current spec requires that the constant expression
null
be converted to the type of the value being tested. That would have to be changed (for type parameters) to make this work.Related to that, we should also permit
t is 3
when the type oft
is a type parameter.More generally, when the object being tested is an open type, we treat it the same as if it is of type
object
for the purposes of legality of the pattern.This was implemented in dev16: dotnet/roslyn#25995
The text was updated successfully, but these errors were encountered: