Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with F# Options in C#. e.g. FSharpOption<Dictionary<Guid, MembershipUser>>

I'm wonder if there is a way to reduce the ugliness of dealing with option types that are returned from F# to C#. For instance:

 var result = TheOneCache.Get<Dictionary<Guid, MembershipUser>>(TheOneCache.EntryType.SQL, USERNAME_DICTIONARY);

 if (FSharpOption<Dictionary<Guid, MembershipUser>>.get_IsSome(result))
 {
     result.Value.Remove(membershipid);
 }

I'd love to not have to provide the <Dictionary<Guid, MembershipUser>> every time. Any way to improve this situation?

like image 268
jackmott Avatar asked Sep 19 '25 18:09

jackmott


1 Answers

@MiMo pointed me the right direction in the comments. If anyone else comes across this problem, the following trick from jaredpar' blog will wrap these calls up for you so you can just call FSharpOption.isSome(x)

public static class FSharpOption {
  public static FSharpOption<T> Create<T>(T value) {
    return new FSharpOption<T>(value);
  }
  public static bool IsSome<T>(this FSharpOption<T> opt) {
    return FSharpOption<T>.get_IsSome(opt);
  }
  public static bool IsNone<T>(this FSharpOption<T> opt) {
    return FSharpOption<T>.get_IsNone(opt);
  }
}
like image 122
jackmott Avatar answered Sep 22 '25 20:09

jackmott