Created
January 15, 2024 03:15
-
-
Save neon-sunset/ceef3d13cbab1a86dcb3aa7e99d858a5 to your computer and use it in GitHub Desktop.
We have DUs at home!
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
#pragma warning disable IDE0072, CS8509 | |
using System; | |
using static Result<string, System.Exception>; | |
var result = Result.Wrap(Console.ReadLine); | |
Console.WriteLine(result switch | |
{ | |
Ok(var value) => $"You entered: {value}", | |
Err(var error) => $"Error: {error.Message}" | |
}); | |
abstract record Result<T, E> | |
{ | |
private Result() { } | |
public record Ok(T Value) : Result<T, E>; | |
public record Err(E Error) : Result<T, E>; | |
} | |
static class Result | |
{ | |
public static Result<T, Exception> Wrap<T>(Func<T> source) | |
{ | |
try | |
{ | |
return new Result<T, Exception>.Ok(source()); | |
} | |
catch (Exception e) | |
{ | |
return new Result<T, Exception>.Err(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment