Created
October 15, 2018 05:38
-
-
Save orange-in-space/8fd7fb6ad32d278135c9c2b0bd3ae51a to your computer and use it in GitHub Desktop.
RustのResult<T, E>のエラーハンドリングっぽいことをC# で出来ないのかなって思って作ったやつ><;
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CsRustResultModoki_orange | |
{ | |
// | |
// Rust Result<T, E> in C# ? | |
// | |
// RustのResult<T, E>のエラーハンドリングっぽいことを | |
// C# で出来ないのかなって思って作った><; | |
// | |
// | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
Console.WriteLine($"{i}回目!><"); | |
Test(); | |
Console.WriteLine($""); | |
} | |
Console.WriteLine("なにかキーを押すとたぶん終了します!><"); | |
Console.ReadKey(); | |
} | |
private static void Test() | |
{ | |
Console.WriteLine("関数版><"); | |
var te = TamaniErrorFunc(); | |
te.Ok = (Hoge n) => { Console.WriteLine($"中身は{n.Nakami}でした!><"); }; | |
te.Err = (Exception E) => { Console.WriteLine($"エラー>< {E.GetType()}"); }; | |
//class version | |
Console.WriteLine("クラス版><"); | |
new TamaniErrorClass() | |
{ | |
Ok = (Hoge n) => { Console.WriteLine($"中身は{n.Nakami}でした!><"); }, | |
Err = (Exception E) => { Console.WriteLine($"エラー>< {E.GetType()}"); } | |
}; | |
} | |
//関数版?><; | |
public static RustResult<Hoge, Exception> TamaniErrorFunc() | |
{ | |
Random r = new Random(); | |
int kekka = r.Next(7); | |
if (kekka == 0) | |
{ | |
//エラーだよ!>< | |
return RustResult<Hoge, Exception>.CreateWithErr(new DemoException()); | |
} | |
else | |
{ | |
//成功だよ!>< | |
return RustResult<Hoge, Exception>.CreateWithOk(new Hoge(kekka)); | |
} | |
} | |
//クラス版?><; | |
public class TamaniErrorClass : RustResult<Hoge, Exception> | |
{ | |
public TamaniErrorClass() | |
{ | |
Random r = new Random(); | |
int kekka = r.Next(7); | |
if (kekka == 0) | |
{ | |
//エラーだよ!>< | |
_err_object = new DemoException(); | |
resultState = ResultState.Err; | |
} | |
else | |
{ | |
//成功だよ!>< | |
_ok_object = new Hoge(kekka); | |
resultState = ResultState.Ok; | |
} | |
} | |
} | |
//実験用てきとうクラス>< | |
public class Hoge | |
{ | |
public Hoge(int args) | |
{ | |
Nakami = args; | |
} | |
public int Nakami { get; } = 0; | |
} | |
//実験用例外クラス | |
public class DemoException : Exception | |
{ | |
} | |
// | |
// RustのResult<T, E>もどき!>< | |
// | |
//ここから | |
public class RustResult<TResult, TException> | |
where TException : Exception | |
{ | |
public RustResult() | |
{ | |
} | |
protected enum ResultState | |
{ | |
None, | |
Ok, | |
Err | |
} | |
public static RustResult<TResult, TException> CreateWithOk(TResult arg) | |
{ | |
return new RustResult<TResult, TException>() { _ok_object = arg, resultState = ResultState.Ok }; | |
} | |
public static RustResult<TResult, TException> CreateWithErr(TException arg) | |
{ | |
return new RustResult<TResult, TException>() { _err_object = arg, resultState = ResultState.Err }; | |
} | |
protected TResult _ok_object; | |
protected TException _err_object = null; | |
protected ResultState resultState = ResultState.None; | |
//for debug | |
//public TResult Ok_object => _ok_object; | |
//public TException Err_object => _err_object; | |
public delegate void OkDelegate(TResult T); | |
public delegate void ErrDelegate(TException E); | |
public OkDelegate Ok | |
{ | |
set | |
{ | |
//OKな時はOKなdelegateのSetterが呼ばれた時に実行する>< | |
if (resultState == ResultState.Ok) | |
{ | |
value(_ok_object); | |
} | |
} | |
} | |
public ErrDelegate Err | |
{ | |
set | |
{ | |
//Errな時はErrなdelegateのSetterが呼ばれた時に実行する>< | |
if (resultState == ResultState.Err && _err_object != null) | |
{ | |
value(_err_object); | |
} | |
} | |
} | |
} | |
//end of class RustResult<TResult, TException> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment