Constrain parameter of [Not]HaveFlag struct enums#1315
Closed
jnyrup wants to merge 1 commit intofluentassertions:developfrom
Closed
Constrain parameter of [Not]HaveFlag struct enums#1315jnyrup wants to merge 1 commit intofluentassertions:developfrom
jnyrup wants to merge 1 commit intofluentassertions:developfrom
Conversation
Member
|
What does that do to the binary and source-code compatibility? |
Member
Author
|
Currently one can pass in null to using System;
public class Program {
enum MyEnum
{
A
}
static bool Foo(Enum t)
=> MyEnum.A.HasFlag(t);
static bool Bar<TEnum>(TEnum t)
where TEnum : struct, Enum
=> MyEnum.A.HasFlag(t);
static bool Baz<TEnum>(TEnum t)
where TEnum : Enum
=> MyEnum.A.HasFlag(t);
static void Main()
{
MyEnum nativeEnum = MyEnum.A;
Enum classEnum = MyEnum.A;
Enum nullEnum = null;
Foo(nativeEnum);
Foo(classEnum);
Foo(nullEnum); // NRE
Bar(nativeEnum);
//Bar<MyEnum>(classEnum); // does not compile
//Bar<MyEnum>(nullEnum); // does not compile
Baz(nativeEnum);
Baz(classEnum);
Baz(nullEnum); // NRE
}
}Reading up on the initial implementation in #250 it seems the only/main reason this wasn't moved to a separate It has nice potential. MyEnum.A.Should().Be(OtherEnum.A)
MyEnum.A.Should().Be((object)42)will simply be turned into compile time errors. |
dennisdoomen
approved these changes
Apr 22, 2020
|
Will it be merged? Why the PR is still draft. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In C# 7.3 the enum constraint was introduced, which lets us restrict the parameter of
[Not]HaveFlagto a struct enum.This prevents an NRE if one passed in
null- as it no longer compiles.