Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Think about what the signature for <code>IMonad&lt;T&gt;</code>'s methods would have to be. In Haskell the Monad typeclass is defined as </p> <pre><code>class Monad m where (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b return :: a -&gt; m a </code></pre> <p>It's tricky to translate this directly to a C# interface because you need to be able to reference the specific implementing subtype ("m a" or <code>ISpecificMonad&lt;a&gt;</code>) within the definition of the general IMonad interface. OK, instead of trying to have (for example) <code>IEnumerable&lt;T&gt;</code> implement <code>IMonad&lt;T&gt;</code> directly, we'll try factoring the IMonad implementation out into a separate object which can be passed, along with the specific monad type instance, to whatever needs to treat it as a monad (this is "dictionary-passing style"). This will be <code>IMonad&lt;TMonad&gt;</code> and TMonad here will be not the T in <code>IEnumerable&lt;T&gt;</code>, but <code>IEnumerable&lt;T&gt;</code> itself. But wait -- this can't work either, because the signature of <code>Return&lt;T&gt;</code> for example has to get us from <em>any</em> type T to a <code>TMonad&lt;T&gt;</code>, for <em>any</em> <code>TMonad&lt;&gt;</code>. IMonad would have to be defined as something like</p> <pre><code>interface IMonad&lt;TMonad&lt;&gt;&gt; { TMonad&lt;T&gt; Unit&lt;T&gt;(T x); TMonad&lt;U&gt; SelectMany&lt;T, U&gt;(TMonad&lt;T&gt; x, Func&lt;T, TMonad&lt;U&gt;&gt; f); } </code></pre> <p>using a hypothetical C# feature that would allow us to use <em>type constructors</em> (like TMonad&lt;>) as generic type parameters. But of course C# does not have this feature <em>(higher-kinded polymorphism)</em>. You can reify type constructors at runtime (<code>typeof(IEnumerable&lt;&gt;)</code>) but can't refer to them in type signatures without giving them parameters. So besides the -100 points thing, implementing this "properly" would require not just adding another ordinary interface definition, but deep additions to the type system. </p> <p>That's why the ability to have query comprehensions over your own types is kind of hacked on (they just "magically" work if the right magic method names with the right signatures are there) instead of using the interface mechanism etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload