diff --git a/csharp/Platform.Setters.Tests/SetterTests.cs b/csharp/Platform.Setters.Tests/SetterTests.cs index af0dc4c..0a142c7 100644 --- a/csharp/Platform.Setters.Tests/SetterTests.cs +++ b/csharp/Platform.Setters.Tests/SetterTests.cs @@ -45,5 +45,31 @@ public void MethodsWithIntegerReturnTypeTest() Assert.Equal(0, setter.SetFirstAndReturnFalse(new int[] { 4 })); Assert.Equal(4, setter.Result); } + + [Fact] + public void WithConstantFactoryMethodTest() + { + Setter setter = Setter.WithConstant(42); + Assert.Equal(42, setter.TrueValue); + Assert.Equal(42, setter.FalseValue); + Assert.Equal(default, setter.Result); + Assert.Equal(42, setter.SetAndReturnTrue(1)); + Assert.Equal(1, setter.Result); + Assert.Equal(42, setter.SetAndReturnFalse(2)); + Assert.Equal(2, setter.Result); + } + + [Fact] + public void WithConstantAndDefaultValueFactoryMethodTest() + { + Setter setter = Setter.WithConstant(42, 99); + Assert.Equal(42, setter.TrueValue); + Assert.Equal(42, setter.FalseValue); + Assert.Equal(99, setter.Result); + Assert.Equal(42, setter.SetAndReturnTrue(1)); + Assert.Equal(1, setter.Result); + Assert.Equal(42, setter.SetAndReturnFalse(2)); + Assert.Equal(2, setter.Result); + } } } diff --git a/csharp/Platform.Setters/Setter[TResult, TDecision].cs b/csharp/Platform.Setters/Setter[TResult, TDecision].cs index c766674..61bb435 100644 --- a/csharp/Platform.Setters/Setter[TResult, TDecision].cs +++ b/csharp/Platform.Setters/Setter[TResult, TDecision].cs @@ -87,6 +87,40 @@ public Setter(TResult defaultValue) : base(defaultValue) { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Setter() { } + /// + /// Creates a new instance of the class using passed-in as both true and false values. + /// Создает новый экземпляр класса , используя переданное значение в качестве значения для истины и лжи. + /// + /// + /// A constant value that will be used for both true and false cases. + /// Константное значение, которое будет использоваться для случаев истины и лжи. + /// + /// + /// A new instance of the class with the same constant for both true and false values. + /// Новый экземпляр класса с одинаковой константой для значений истины и лжи. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Setter WithConstant(TDecision constantValue) => new(constantValue, constantValue); + + /// + /// Creates a new instance of the class using passed-in as both true and false values with the as a result. + /// Создает новый экземпляр класса , используя переданное значение в качестве значения для истины и лжи с в качестве результата. + /// + /// + /// A constant value that will be used for both true and false cases. + /// Константное значение, которое будет использоваться для случаев истины и лжи. + /// + /// + /// A default result value. + /// Результирующее значение по умолчанию. + /// + /// + /// A new instance of the class with the same constant for both true and false values and the specified default result value. + /// Новый экземпляр класса с одинаковой константой для значений истины и лжи и указанным значением результата по умолчанию. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Setter WithConstant(TDecision constantValue, TResult defaultValue) => new(constantValue, constantValue, defaultValue); + /// /// Sets the to the and returns the value indicating true. /// Устанавливает в и возвращает значение обозначающее истину. diff --git a/experiments/SingleConstantSetterDemo.cs b/experiments/SingleConstantSetterDemo.cs new file mode 100644 index 0000000..6040fa5 --- /dev/null +++ b/experiments/SingleConstantSetterDemo.cs @@ -0,0 +1,41 @@ +using System; +using Platform.Setters; + +namespace Platform.Setters.Experiments +{ + /// + /// Demo showing the usage of Setter with single constant as requested in issue #7. + /// This addresses the case where "in real life usage only one constant is actually used". + /// + class SingleConstantSetterDemo + { + static void Main() + { + // Example usage as requested in the issue: + // "var setter = new Setter(links.Constants.Break);" + + // Before: Required specifying both true/false values + Console.WriteLine("=== Before (traditional approach) ==="); + var traditionalSetter = new Setter(1, 0); // trueValue=1, falseValue=0 + Console.WriteLine($"TrueValue: {traditionalSetter.TrueValue}, FalseValue: {traditionalSetter.FalseValue}"); + + // After: Can use single constant for both true/false cases + Console.WriteLine("\n=== After (single constant approach) ==="); + uint constantBreak = 42; // Simulating links.Constants.Break + var singleConstantSetter = Setter.WithConstant(constantBreak); + Console.WriteLine($"TrueValue: {singleConstantSetter.TrueValue}, FalseValue: {singleConstantSetter.FalseValue}"); + + // Both methods return the same constant value + Console.WriteLine("\n=== Testing functionality ==="); + Console.WriteLine($"SetAndReturnTrue result: {singleConstantSetter.SetAndReturnTrue(100)}"); // Should return 42 + Console.WriteLine($"SetAndReturnFalse result: {singleConstantSetter.SetAndReturnFalse(200)}"); // Should return 42 + Console.WriteLine($"Final result value: {singleConstantSetter.Result}"); // Should be 200 + + // Can also specify default value + Console.WriteLine("\n=== With default value ==="); + var setterWithDefault = Setter.WithConstant(constantBreak, 999); + Console.WriteLine($"TrueValue: {setterWithDefault.TrueValue}, FalseValue: {setterWithDefault.FalseValue}"); + Console.WriteLine($"Initial result: {setterWithDefault.Result}"); // Should be 999 + } + } +} \ No newline at end of file