c# - Implementing monadic bind for Nullable *and* reference types -


I have this function:

  Public stable u? If the National lieutenant; T, U & gt; (This t? Self, funk? T, u? & Gt; fancy) where T: straight where u: straight {return} is the value? Throw (self. Value): faucet; }   

Example:

  int? Probably = 42; Maybe.IfNotNull (n => 2 * n); // 84 probably = faucet; Maybe.IfNotNull (n => 2 * n); // null   

I call it unnecessarily blank with the type of type as well as clear faucet & lt; & Gt; This implementation will work if you want to work on types:

  Public Fixed U IfNotNull & lt; T, U & gt; (This T? Self, funk & lt; T, U> func) where T: struct where U: class {return (self. HasValue)? Throw (self. Value): faucet; }   

But of course the overload resolution does not look at the type of obstacles, so you can not have both at once. Is this the solution?

But of course the overload resolution does not see type barriers

Good does this ... but there is no shortage of method but it looks like type deficiency on parameter types.

In C # 4 (which are optional parameters) you can do this ... but I 'd actually suggest you do that Do not:

  public class MustBeStruct & lt; T & gt; Where T: Structure {} Public Squares MustBeClass & lt; T & gt; Where T: class {} public stable u? If the National lieutenant; T, U & gt; (It's not self, fun & lt; t, u? & Gt; funk, musbstread & lieutenant; u & gt; undiscovered = default (absolute lieutenant; u & gt;)) where t: strait where u: struct { Returns (self.HasValue)? Throw (self. Value): faucet; } Public static U IfNotNull & lt; T, U & gt; (This t? Self, fun  T, U> func, mustbeclass  ignored = null) where T: struct where U: class {return (self.HasValue)? Throw (self. Value): faucet; }   

For more information on this awesome, awesome hack, see.

Personally, I may name two ways differently so that it does not need overloaded resolution to work very hard - nor do the readers of your code do it too.

Comments