diff --git a/samples/AStar.Dev.ConsoleSample/Program.cs b/samples/AStar.Dev.ConsoleSample/Program.cs index 001ea74..1f1d3a0 100644 --- a/samples/AStar.Dev.ConsoleSample/Program.cs +++ b/samples/AStar.Dev.ConsoleSample/Program.cs @@ -49,6 +49,8 @@ Option.Some("Jason"); Option.None(); var (isSome, value) = Option.Some("hello"); +Console.WriteLine(isSome); +Console.WriteLine(value); return; diff --git a/samples/AStar.Dev.SampleApi/Program.cs b/samples/AStar.Dev.SampleApi/Program.cs index ff01cb7..d77c5b6 100644 --- a/samples/AStar.Dev.SampleApi/Program.cs +++ b/samples/AStar.Dev.SampleApi/Program.cs @@ -34,4 +34,4 @@ ex => Results.BadRequest(new { Error = ex.Message })); }); -app.Run(); +await app.RunAsync(); diff --git a/samples/AStar.Dev.SampleBlazor/Components/Pages/OptionDemo.razor.cs b/samples/AStar.Dev.SampleBlazor/Components/Pages/OptionDemo.razor.cs index 24a2954..af2eed9 100644 --- a/samples/AStar.Dev.SampleBlazor/Components/Pages/OptionDemo.razor.cs +++ b/samples/AStar.Dev.SampleBlazor/Components/Pages/OptionDemo.razor.cs @@ -19,7 +19,7 @@ private async Task CheckUsernameAsync() Timestamp = DateTime.Now }; - var validated = await UserService.TryValidateAsync(userInput); + var validated = await UsernameService.TryValidateAsync(userInput); pipelineSteps.Validated = validated; var mapped = validated.Map(name => name.ToUpper()); @@ -54,7 +54,7 @@ private void ToggleDebug() debugVisible = !debugVisible; } - private class PipelineLog + private sealed class PipelineLog { public string Input { get; set; } = ""; public Option? Validated { get; set; } diff --git a/samples/AStar.Dev.SampleBlazor/Components/Pages/UsernameService.cs b/samples/AStar.Dev.SampleBlazor/Components/Pages/UsernameService.cs index 2572d7e..cb4bbb9 100644 --- a/samples/AStar.Dev.SampleBlazor/Components/Pages/UsernameService.cs +++ b/samples/AStar.Dev.SampleBlazor/Components/Pages/UsernameService.cs @@ -4,11 +4,14 @@ namespace AStar.Dev.SampleBlazor.Components.Pages; public class UsernameService { - public Task> TryValidateAsync(string input) + protected UsernameService() { - if (string.IsNullOrWhiteSpace(input)) - return Task.FromResult(Option.None()); + } - return Task.FromResult(Option.Some(input.Trim())); + public static Task> TryValidateAsync(string input) + { + return Task.FromResult(string.IsNullOrWhiteSpace(input) + ? Option.None() + : Option.Some(input.Trim())); } } diff --git a/samples/AStar.Dev.SampleBlazor/Components/Pages/Weather.razor b/samples/AStar.Dev.SampleBlazor/Components/Pages/Weather.razor index cb730d3..fd74002 100644 --- a/samples/AStar.Dev.SampleBlazor/Components/Pages/Weather.razor +++ b/samples/AStar.Dev.SampleBlazor/Components/Pages/Weather.razor @@ -56,7 +56,7 @@ else }).ToArray(); } - private class WeatherForecast + private sealed class WeatherForecast { public DateOnly Date { get; set; } public int TemperatureC { get; set; } @@ -64,4 +64,4 @@ else public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); } -} \ No newline at end of file +} diff --git a/samples/AStar.Dev.SampleBlazor/Program.cs b/samples/AStar.Dev.SampleBlazor/Program.cs index 8ebbf56..fc7e6a2 100644 --- a/samples/AStar.Dev.SampleBlazor/Program.cs +++ b/samples/AStar.Dev.SampleBlazor/Program.cs @@ -31,4 +31,4 @@ app.MapRazorComponents() .AddInteractiveServerRenderMode(); -app.Run(); +await app.RunAsync(); diff --git a/src/AStar.Dev.Functional.Extensions/OptionExtensions.cs b/src/AStar.Dev.Functional.Extensions/OptionExtensions.cs index 7a41823..17d90eb 100644 --- a/src/AStar.Dev.Functional.Extensions/OptionExtensions.cs +++ b/src/AStar.Dev.Functional.Extensions/OptionExtensions.cs @@ -35,19 +35,6 @@ public static Option ToOption(this T value) : new Option.Some(value); } - /// - /// Enables deconstruction of an option into a boolean and value pair. - /// - /// - /// - /// - /// - public static void Deconstruct(this Option option, out bool isSome, out T? value) - { - isSome = option is Option.Some; - value = isSome ? ((Option.Some)option).Value : default; - } - /// /// Converts a value to an if it satisfies the predicate. /// @@ -143,4 +130,17 @@ public static T OrThrow(this Option option, Exception? ex = null) { return option is Option.Some some ? some.Value : throw ex ?? new InvalidOperationException("No value present"); } + + /// + /// Enables deconstruction of an option into a boolean and value pair. + /// + /// + /// + /// + /// + public static void Deconstruct(this Option option, out bool isSome, out T? value) + { + isSome = option is Option.Some; + value = isSome ? ((Option.Some)option).Value : default; + } } diff --git a/src/AStar.Dev.Functional.Extensions/Option{T}.cs b/src/AStar.Dev.Functional.Extensions/Option{T}.cs index 652a18b..8490180 100644 --- a/src/AStar.Dev.Functional.Extensions/Option{T}.cs +++ b/src/AStar.Dev.Functional.Extensions/Option{T}.cs @@ -12,27 +12,15 @@ private Option() { } - /// - /// Overrides the ToString method to return both the type and, if present, the value. - /// - /// - public override string ToString() - { - return this switch - { - Some some => $"Some({some.Value})", - None => "None", - _ => "Invalid" - }; - } - /// /// Implicitly converts a value to an . /// /// The value to wrap. Null becomes . public static implicit operator Option(T value) { - return value != null ? new Some(value) : None.Instance; + return value != null + ? new Some(value) + : None.Instance; } /// @@ -73,6 +61,15 @@ public Some(T value) /// The wrapped value. /// public T Value { get; } + + /// + /// Overrides the ToString method to return both the type and the value. + /// + /// The overridden ToString + public override string ToString() + { + return $"Some({Value})"; + } } /// @@ -81,12 +78,21 @@ public Some(T value) public sealed class None : Option { /// - /// A helper method to create an instance of + /// A helper method to create an instance of /// public static readonly None Instance = new (); private None() { } + + /// + /// Overrides the ToString method to return the type as a simple string. + /// + /// The overridden ToString + public override string ToString() + { + return "None"; + } } }