Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best mechanism I'm aware of for performing generic arithmetic is type classes, which sadly neither C#, F#, nor the .Net runtime in general support. However, you can simulate them yourself by hand, as mentioned in this blog post:</p> <p><a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2009/05/08/type-classes-are-the-secret-sauce.aspx" rel="nofollow noreferrer">Type Classes Are The Secret Sauce</a></p> <p>That technique should work in C# 2.0 or later (using anonymous delegates / lambdas).</p> <p>Often people turn to interfaces, but run into a couple problems</p> <ol> <li>You can't declare that existing type implement an interface, so you can't define instance of that interface for built in types like int.</li> <li>Interfaces can't constrain the type of other arguments to methods.</li> </ol> <p>An interface declares that, for all implementations, all the methods on that interface take the same implicit 'this' parameter type. If Foo implements some interface, then obviously the 'this' parameter must be of type Foo for that implementation. But there's no way to require that other method parameters <em>also</em> be of type Foo.</p> <p><a href="http://en.wikipedia.org/wiki/Type_class" rel="nofollow noreferrer">Type classes</a> allow you to (among other things) perform this kind of constraint on all method parameters, not just the first parameter. </p> <p>As mentioned in the article cited earlier, you can simulate type classes by passing function tables as explicit arguments.</p> <p>(Community wiki: would post an example from that article translated into C# here, but ran out of time with long-winded explaination)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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