Cast of non-existing Combination wrongly return the item with lowest value
- Dominant language
- C#
- Stars
- 2.4k
- Forks
- 184
- PR merge metrics
- No merged PRs in 30d
Description
The cast of non-existing Combination `value1 | value2` to `TestFlagEnumWithoutCombination` wrongly returns the item with the lowest internal value (i.e. `value1`).
This is totally misleading and error-prone, moreover is not consistent with .net enums' behavior.
I think it should probably return a special runtime object (similarly to the "collection" of enum values) or just throw an exception.
```
using Ardalis.SmartEnum;
{
var value1and2_smartEnum = (TestSmartFlagEnum)(TestSmartFlagEnum.Value1 | TestSmartFlagEnum.Value2);
var value1and2_dotnetEnum = TestFlagEnum.Value1 | TestFlagEnum.Value2;
Console.WriteLine($"{value1and2_smartEnum} == {value1and2_dotnetEnum}");
// prints Value1and2 == Value1and2
}
{
var value1and2_smartEnum = (TestSmartFlagEnumWithoutCombination)(TestSmartFlagEnumWithoutCombination.Value1 | TestSmartFlagEnumWithoutCombination.Value2);
var value1and2_dotnetEnum = TestFlagEnumWithoutCombination.Value1 | TestFlagEnumWithoutCombination.Value2;
Console.WriteLine($"{value1and2_smartEnum} != {value1and2_dotnetEnum}");
// prints Value1 != Value1, Value2
}
[Flags]
public enum TestFlagEnum
{
Value1 = 1,
Value2 = 2,
// Combinations
Value1and2 = 3
}
public class TestSmartFlagEnum : SmartFlagEnum
{
public static readonly TestSmartFlagEnum Value1 = new(nameof(Value1), 1);
public static readonly TestSmartFlagEnum Value2 = new(nameof(Value2), 2);
// Combinations
public static readonly TestSmartFlagEnum Value1and2 = new(nameof(Value1and2), 3);
public TestSmartFlagEnum(string name, int value) : base(name, value) { }
}
[Flags]
public enum TestFlagEnumWithoutCombination
{
Value1 = 1,
Value2 = 2
}
public class TestSmartFlagEnumWithoutCombination : SmartFlagEnum
{
public static readonly TestSmartFlagEnumWithoutCombination Value1 = new(nameof(Value1), 1);
public static readonly TestSmartFlagEnumWithoutCombination Value2 = new(nameof(Value2), 2);
public TestSmartFlagEnumWithoutCombination(string name, int value) : base(name, value) { }
}
```
Contributor guide
Research direction
Start at the SmartFlagEnum cast behavior shown in the issue and reproduce the non-existing Value1 | Value2 case for TestSmartFlagEnumWithoutCombination. Inspect the existing SmartFlagEnum implementation and tests, then define and cover behavior that does not return the lowest-valued item for an unrepresented combination.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100