Skip to content

Instantly share code, notes, and snippets.

@neon-sunset
Created January 15, 2024 03:15
Show Gist options
  • Save neon-sunset/ceef3d13cbab1a86dcb3aa7e99d858a5 to your computer and use it in GitHub Desktop.
Save neon-sunset/ceef3d13cbab1a86dcb3aa7e99d858a5 to your computer and use it in GitHub Desktop.
We have DUs at home!
#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