{"id":2717,"date":"2023-12-08T21:49:13","date_gmt":"2023-12-08T21:49:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2717"},"modified":"2023-12-08T21:49:13","modified_gmt":"2023-12-08T21:49:13","slug":"c-interop-with-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/c-interop-with-f\/","title":{"rendered":"C# interop with F#"},"content":{"rendered":"<p><em>Note: I\u2019m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never.<\/em><\/p>\n<p>The intention of this post is to demonstrate how various bits of F# code are viewed from C# code. Obviously as both are .NET languages compiling to IL they can call one another&#8217;s code. <\/p>\n<p><strong>F# modules<\/strong><\/p>\n<p>Let&#8217;s start at the top, an F# module. So let&#8217;s look at a simple module<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nmodule MyModule\r\n\r\nlet squared x = x * x\r\n<\/pre>\n<p>The module will appears to C# as a static class and the function squared will become a static method, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class MyModule\r\n{\r\n  public static int squared(int x);\r\n}\r\n<\/pre>\n<p>F# inferred the type to be an int<\/p>\n<p>Ofcourse from C# we&#8217;ll call this function like any other static function<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint result = MyModule.squared(4);\r\n<\/pre>\n<p><strong>Null<\/strong><\/p>\n<p>The preference within F# is to use the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd233245.aspx\" target=\"_blank\" rel=\"noopener\">Option type<\/a>. But if you are working in C# and not wanting to include F# specific types you might prefer to still return a null. However if you are doing something like the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nmatch results with\r\n| null -&gt; null\r\n| a -&gt; new Thing(a)\r\n<\/pre>\n<p>This will fail to compiler with an error such as &#8220;The type &#8216;Thing&#8217; does not have &#8216;null&#8217; as a proper value.&#8221;<\/p>\n<p>We can solve this by marking the Thing type with the attribute AllowNullLiteral, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;AllowNullLiteral&gt;]\r\ntype Thing() =\r\n   \/\/ members etc.\r\n<\/pre>\n<p>or we might change the origina F# code to <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nmatch results with\r\n| null -&gt; Operators.Unchecked.defaultof&lt;Scale&gt;\r\n| a -&gt; new Thing(a)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: I\u2019m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never. The intention of this post is to demonstrate how various bits of F# code are viewed from C# code. Obviously as both are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-2717","post","type-post","status-publish","format-standard","hentry","category-c","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2717"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2717\/revisions"}],"predecessor-version":[{"id":10324,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2717\/revisions\/10324"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}