{"id":1149,"date":"2012-01-31T16:48:58","date_gmt":"2012-01-31T16:48:58","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1149"},"modified":"2012-01-31T17:13:25","modified_gmt":"2012-01-31T17:13:25","slug":"why-do-we-need-partial-application-part-2-of-n-simulating-type-classes-in-c-and-f","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/01\/31\/why-do-we-need-partial-application-part-2-of-n-simulating-type-classes-in-c-and-f\/","title":{"rendered":"Why do we need partial application? &ndash; Part 2 of n &#8211; Simulating type classes in C# and F#"},"content":{"rendered":"<p>This is yet another blog post in my <a href=\"http:\/\/en.wikipedia.org\/wiki\/Currying\">Currying<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Partial_application\">Partial application<\/a> series. This is what I have posted so far:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.navision-blog.de\/2012\/01\/27\/partial-application-if-f-and-c\/\">Partial application in F# and C#<\/a> <\/li>\n<li><a href=\"http:\/\/www.navision-blog.de\/2012\/01\/29\/why-do-we-need-partial-application-part-1-of-n-fluent-interfaces-and-piping\/\">Why do we need partial application? \u2013 Part 1 of n \u2013 Fluent interfaces and piping<\/a> <\/li>\n<li><a href=\"http:\/\/www.navision-blog.de\/2012\/01\/30\/currying-and-uncurrying-in-c-and-f\/\">Currying and uncurrying in C# and F#<\/a> <\/li>\n<\/ul>\n<p>In this post I want to show you a way to simulate <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type_class\">type classes<\/a> in C# and F#. Type classes are this wonderful feature in Haskell which allow you to specify constraints on your polymorphic types. We don\u2019t have this in C# nor F#.<\/p>\n<p>Let\u2019s start with the following problem: We want to compute the sum of the squares of arbitrary numbers. We want to write something like this:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Simulating-type-classes-in-C-and-F_F1D3\/image.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Simulating-type-classes-in-C-and-F_F1D3\/image_thumb.png\" width=\"489\" height=\"169\" \/><\/a><\/p>\n<p>The problem is we don\u2019t have a generic * function and of course we\u2019d also need a generic + operator and maybe a generic zero. Obviously we need a constraint on the generic parameter T since + might not be defined for any type. So let\u2019s define an interface for numbers:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1711425.js\"> <\/script>  <\/p>\n<p>Nothing special here, so let&#8217;s get straight to the implementation for integers and doubles:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1711433.js\"> <\/script>  <\/p>\n<p>So far so good. With this in our pocket we rewrite SumOfSquares() into this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1711443.js\"> <\/script>  <\/p>\n<p>The trick is that we pass the concrete implementation as the first parameter into our function. This works exactly like a type constraint or as <a href=\"http:\/\/research.microsoft.com\/en-us\/people\/simonpj\/\">Simon Peyton-Jones<\/a> would say: the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Virtual_method_table\">vtable<\/a> travels into the function. Notice that we don\u2019t have access to the definition of in nor double. There is no way for us to express that int or double implement a number interface.<\/p>\n<p>Now let\u2019s try this out:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1711474.js\"> <\/script>  <\/p>\n<p>As you can see, this is perfectly type safe. We now have a way for poor man\u2019s type classes in C#. Yay!<\/p>\n<p>Now what has this to do with partial application? Let\u2019s look at the same thing in F#:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1711508.js\"> <\/script>  <\/p>\n<p>We&#8217;re using a lot of partial application here. Exercise: Try to spot all the places.<\/p>\n<p>Ok, you\u2019re right. This post might be a little bit far away from the partial application stuff, but it\u2019s still related. Somehow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is yet another blog post in my Currying and Partial application series. This is what I have posted so far: Partial application in F# and C# Why do we need partial application? \u2013 Part 1 of n \u2013 Fluent interfaces and piping Currying and uncurrying in C# and F# In this post I want [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,448],"tags":[565,558,664,559,613],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1149"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1149"}],"version-history":[{"count":4,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1149\/revisions"}],"predecessor-version":[{"id":1153,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1149\/revisions\/1153"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1149"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}