<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>[Giagnocavo]Michael::Write() - FSharp</title>
    <link>http://www.atrevido.net/blog/</link>
    <description>Something about .NET.</description>
    <copyright>Michael Giagnocavo</copyright>
    <lastBuildDate>Fri, 07 May 2010 04:57:57 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mggUNSPAM@telefinity.com</managingEditor>
    <webMaster>mggUNSPAM@telefinity.com</webMaster>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=e0bf507d-5e92-4f49-b834-be3f8ae61318</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,e0bf507d-5e92-4f49-b834-be3f8ae61318.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,e0bf507d-5e92-4f49-b834-be3f8ae61318.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e0bf507d-5e92-4f49-b834-be3f8ae61318</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In Visual Studio 2010, you can now get your F# unit tests loaded by the IDE. First,
create your F# test project. This is just a normal library referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework
and having test classes and so on. The only change you need to do is go into the configuration
and set the output folder to "bin" for both debug and release (instead of bin\debug).
</p>
        <p>
Next, create a C# Test Project, and delete the code file. Then add an existing item,
and navigate to the bin directory of your test project output. Select the DLL and
Add as Link. 
</p>
        <p>
Finally, right click the solution, and change the project dependencies so the C# test
project depends on your F# test project.
</p>
        <p>
That's it. Now the IDE will pickup your F# tests and allow you to manage, run, and
debug them right from VS. You may have to restart the IDE after setting all this
up for it to work smoothly. 
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e0bf507d-5e92-4f49-b834-be3f8ae61318" />
      </body>
      <title>F# Unit Testing with Visual Studio 2010</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,e0bf507d-5e92-4f49-b834-be3f8ae61318.aspx</guid>
      <link>http://www.atrevido.net/blog/2010/05/07/F+Unit+Testing+With+Visual+Studio+2010.aspx</link>
      <pubDate>Fri, 07 May 2010 04:57:57 GMT</pubDate>
      <description>&lt;p&gt;
In Visual Studio 2010, you can now get your F# unit tests loaded by the IDE. First,
create your F# test project. This is just a normal library referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework
and having test classes and so on. The only change you need to do is go into the configuration
and set the output folder to "bin" for both debug and release (instead of bin\debug).
&lt;/p&gt;
&lt;p&gt;
Next, create a C# Test Project, and delete the code file. Then add an existing item,
and navigate to the bin directory of your test project output. Select the DLL and
Add as Link. 
&lt;/p&gt;
&lt;p&gt;
Finally, right click the solution, and change the project dependencies so the C# test
project depends on your F# test project.
&lt;/p&gt;
&lt;p&gt;
That's it. Now the IDE will pickup your F# tests and allow you to manage, run, and
debug them right from VS.&amp;nbsp;You may have to restart the IDE after setting all this
up for it to work smoothly.&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e0bf507d-5e92-4f49-b834-be3f8ae61318" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,e0bf507d-5e92-4f49-b834-be3f8ae61318.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=88dc6738-246f-4ca8-a6b2-be5b23bdf59e</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,88dc6738-246f-4ca8-a6b2-be5b23bdf59e.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,88dc6738-246f-4ca8-a6b2-be5b23bdf59e.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=88dc6738-246f-4ca8-a6b2-be5b23bdf59e</wfw:commentRss>
      <title>FluentNHibernate FSharp update for RTM</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,88dc6738-246f-4ca8-a6b2-be5b23bdf59e.aspx</guid>
      <link>http://www.atrevido.net/blog/2010/04/28/FluentNHibernate+FSharp+Update+For+RTM.aspx</link>
      <pubDate>Wed, 28 Apr 2010 21:56:22 GMT</pubDate>
      <description>I've updated the code we use for FluentNHibernate with F# - using the final versions of both products. It's just some code to fixup F# quotations to LINQ expressions that FluentNHibernate can work with, and type extensions to make it easy to consume from F#.&lt;br&gt;
&lt;br&gt;
The &lt;a href="http://fluentnhibernate.org/downloads/releases/fluentnhibernate-1.0RTM.zip"&gt;FluentNHibernate
RTM zip&lt;/a&gt; has all the other binaries you need to get started.&lt;br&gt;
&lt;br&gt;
I'm building against .NET 4, but the code should work on 2.0 as well.&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.atrevido.net/blog/content/binary/FluentNHibernate.FSharp.dll"&gt;FluentNHibernate.FSharp.dll
(38.5 KB)&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.atrevido.net/blog/content/binary/FluentNHibernate.FSharp.zip"&gt;FluentNHibernate.FSharp.zip
(6.95 KB) Source&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
And here's the FluentNHibernate sample first project using FluentNHibernate.FSharp:&lt;br&gt;
&lt;br&gt;
&lt;a href="http://www.atrevido.net/blog/content/binary/FirstProject.zip"&gt;FirstProject.zip
(7.66 KB)&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
Here's an example of what some mapping code looks like:&lt;br&gt;
&lt;br&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta name="ProgId" content="Word.Document"&gt;
&lt;meta name="Generator" content="Microsoft Word 14"&gt;
&lt;meta name="Originator" content="Microsoft Word 14"&gt;
&lt;link rel="File-List" href="file:///C:%5CUsers%5CMichael%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"&gt;
&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;o:OfficeDocumentSettings&gt;
  &lt;o:AllowPNG/&gt;
 &lt;/o:OfficeDocumentSettings&gt;
&lt;/xml&gt;&lt;![endif]--&gt;
&lt;link rel="themeData" href="file:///C:%5CUsers%5CMichael%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"&gt;
&lt;link rel="colorSchemeMapping" href="file:///C:%5CUsers%5CMichael%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml"&gt;
&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;w:WordDocument&gt;
  &lt;w:View&gt;Normal&lt;/w:View&gt;
  &lt;w:Zoom&gt;0&lt;/w:Zoom&gt;
  &lt;w:TrackMoves/&gt;
  &lt;w:TrackFormatting/&gt;
  &lt;w:PunctuationKerning/&gt;
  &lt;w:ValidateAgainstSchemas/&gt;
  &lt;w:SaveIfXMLInvalid&gt;false&lt;/w:SaveIfXMLInvalid&gt;
  &lt;w:IgnoreMixedContent&gt;false&lt;/w:IgnoreMixedContent&gt;
  &lt;w:AlwaysShowPlaceholderText&gt;false&lt;/w:AlwaysShowPlaceholderText&gt;
  &lt;w:DoNotPromoteQF/&gt;
  &lt;w:LidThemeOther&gt;EN-US&lt;/w:LidThemeOther&gt;
  &lt;w:LidThemeAsian&gt;X-NONE&lt;/w:LidThemeAsian&gt;
  &lt;w:LidThemeComplexScript&gt;X-NONE&lt;/w:LidThemeComplexScript&gt;
  &lt;w:Compatibility&gt;
   &lt;w:BreakWrappedTables/&gt;
   &lt;w:SnapToGridInCell/&gt;
   &lt;w:WrapTextWithPunct/&gt;
   &lt;w:UseAsianBreakRules/&gt;
   &lt;w:DontGrowAutofit/&gt;
   &lt;w:SplitPgBreakAndParaMark/&gt;
   &lt;w:EnableOpenTypeKerning/&gt;
   &lt;w:DontFlipMirrorIndents/&gt;
   &lt;w:OverrideTableStyleHps/&gt;
  &lt;/w:Compatibility&gt;
  &lt;m:mathPr&gt;
   &lt;m:mathFont m:val="Cambria Math"/&gt;
   &lt;m:brkBin m:val="before"/&gt;
   &lt;m:brkBinSub m:val="&amp;#45;-"/&gt;
   &lt;m:smallFrac m:val="off"/&gt;
   &lt;m:dispDef/&gt;
   &lt;m:lMargin m:val="0"/&gt;
   &lt;m:rMargin m:val="0"/&gt;
   &lt;m:defJc m:val="centerGroup"/&gt;
   &lt;m:wrapIndent m:val="1440"/&gt;
   &lt;m:intLim m:val="subSup"/&gt;
   &lt;m:naryLim m:val="undOvr"/&gt;
  &lt;/m:mathPr&gt;&lt;/w:WordDocument&gt;
&lt;/xml&gt;&lt;![endif]--&gt;
&lt;!--[if gte mso 9]&gt;&lt;xml&gt;
 &lt;w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"
  DefSemiHidden="true" DefQFormat="false" DefPriority="99"
  LatentStyleCount="267"&gt;
  &lt;w:LsdException Locked="false" Priority="0" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Normal"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="heading 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/&gt;
  &lt;w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 7"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 8"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" Name="toc 9"/&gt;
  &lt;w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/&gt;
  &lt;w:LsdException Locked="false" Priority="10" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Title"/&gt;
  &lt;w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/&gt;
  &lt;w:LsdException Locked="false" Priority="11" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/&gt;
  &lt;w:LsdException Locked="false" Priority="22" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Strong"/&gt;
  &lt;w:LsdException Locked="false" Priority="20" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/&gt;
  &lt;w:LsdException Locked="false" Priority="59" SemiHidden="false"
   UnhideWhenUsed="false" Name="Table Grid"/&gt;
  &lt;w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/&gt;
  &lt;w:LsdException Locked="false" Priority="1" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/&gt;
  &lt;w:LsdException Locked="false" Priority="34" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/&gt;
  &lt;w:LsdException Locked="false" Priority="29" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Quote"/&gt;
  &lt;w:LsdException Locked="false" Priority="30" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/&gt;
  &lt;w:LsdException Locked="false" Priority="60" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Shading Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="61" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light List Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="62" SemiHidden="false"
   UnhideWhenUsed="false" Name="Light Grid Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="63" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="64" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="65" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="66" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="67" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="68" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="69" SemiHidden="false"
   UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="70" SemiHidden="false"
   UnhideWhenUsed="false" Name="Dark List Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="71" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="72" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful List Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="73" SemiHidden="false"
   UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/&gt;
  &lt;w:LsdException Locked="false" Priority="19" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/&gt;
  &lt;w:LsdException Locked="false" Priority="21" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/&gt;
  &lt;w:LsdException Locked="false" Priority="31" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/&gt;
  &lt;w:LsdException Locked="false" Priority="32" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/&gt;
  &lt;w:LsdException Locked="false" Priority="33" SemiHidden="false"
   UnhideWhenUsed="false" QFormat="true" Name="Book Title"/&gt;
  &lt;w:LsdException Locked="false" Priority="37" Name="Bibliography"/&gt;
  &lt;w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/&gt;
 &lt;/w:LatentStyles&gt;
&lt;/xml&gt;&lt;![endif]--&gt;&lt;style&gt;
&lt;!--
 /* Font Definitions */
 @font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:-520092929 1073786111 9 0 415 0;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;
	mso-font-charset:0;
	mso-generic-font-family:modern;
	mso-font-pitch:fixed;
	mso-font-signature:-520092929 1073806591 9 0 415 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-unhide:no;
	mso-style-qformat:yes;
	mso-style-parent:"";
	margin-top:0in;
	margin-right:0in;
	margin-bottom:10.0pt;
	margin-left:0in;
	line-height:115%;
	mso-pagination:widow-orphan;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:Calibri;
	mso-fareast-theme-font:minor-latin;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	mso-bidi-font-family:"Times New Roman";
	mso-bidi-theme-font:minor-bidi;}
.MsoChpDefault
	{mso-style-type:export-only;
	mso-default-props:yes;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-fareast-font-family:Calibri;
	mso-fareast-theme-font:minor-latin;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	mso-bidi-font-family:"Times New Roman";
	mso-bidi-theme-font:minor-bidi;}
.MsoPapDefault
	{mso-style-type:export-only;
	margin-bottom:10.0pt;
	line-height:115%;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;
	mso-paper-source:0;}
div.WordSection1
	{page:WordSection1;}
--&gt;
&lt;/style&gt;
&lt;!--[if gte mso 10]&gt;
&lt;style&gt;
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-priority:99;
	mso-style-parent:"";
	mso-padding-alt:0in 5.4pt 0in 5.4pt;
	mso-para-margin-top:0in;
	mso-para-margin-right:0in;
	mso-para-margin-bottom:10.0pt;
	mso-para-margin-left:0in;
	line-height:115%;
	mso-pagination:widow-orphan;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";
	mso-ascii-font-family:Calibri;
	mso-ascii-theme-font:minor-latin;
	mso-hansi-font-family:Calibri;
	mso-hansi-theme-font:minor-latin;
	mso-bidi-font-family:"Times New Roman";
	mso-bidi-theme-font:minor-bidi;}
&lt;/style&gt;
&lt;![endif]--&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas; color: blue;"&gt;type&lt;/span&gt;&lt;span style="font-family: Consolas;"&gt; ProductMap() &lt;span style="color: blue;"&gt;as&lt;/span&gt; m
= &lt;span style="color: blue;"&gt;inherit&lt;/span&gt; ClassMapQ&amp;lt;Product&amp;gt;() &lt;span style="color: blue;"&gt;do&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; x
= m.DefaultX&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m.Not.LazyLoad()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(m.IdQ
&amp;lt;@ x.Id @&amp;gt;).Done&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(m.MapQ
&amp;lt;@ x.Name @&amp;gt;).Done&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(m.MapQ
&amp;lt;@ x.Price @&amp;gt;).Done&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(m.HasManyToManyQ
&amp;lt;@ seq x.StoresStockedIn @&amp;gt;)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.LazyLoad()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.Cascade.All()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.Inverse()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.Table(&lt;span style="color: maroon;"&gt;"StoreProduct"&lt;/span&gt;)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.Done&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;o:p&gt;
&lt;br&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=88dc6738-246f-4ca8-a6b2-be5b23bdf59e" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,88dc6738-246f-4ca8-a6b2-be5b23bdf59e.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=d5591bdc-add1-4fbc-b1d9-0c25359433a6</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d5591bdc-add1-4fbc-b1d9-0c25359433a6</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I came across a <a href="http://cs.hubfs.net/forums/thread/11076.aspx">thread on hubFS
about deserialising F# records with JSON</a>. After <a href="http://lorgonblog.spaces.live.com/">Mr.
McNamara</a> pointed out that DataContract serialization would work with F# records,
I realised we can do the same for other serialisation systems, such as ASP.NET MVC. 
</p>
        <p>
ASP.NET MVC binding doesn't work with F# records for a few reasons. First, it requires
a default constructor, and record types don't have one. Second, it needs settable
properties, and records have read-only properties. Fortunately, the backing field
for a record's property is a mutable field. The name is mangled (@ is appended), but
otherwise we're ok to set that field.
</p>
        <p>
With this, we can subclass the default model binder and add in code to construct records
as well as set their fields directly. Unlike DataContract serializers, I didn't use
FormatterServices.GetUninitializedObject to create the object, I use the F# reflection
function MakeRecord. This is because I want to attempt to initialise all fields on
the record type, to try to keep out nulls. This goes against how the rest of MVC's
null handling goes, so perhaps it's not a great idea. 
</p>
        <p>
At any rate, here's the quite short code. A lot of things probably don't work, such
as F# lists. Perhaps there should be a community project that collects F#-specific
type helpers for different frameworks to make serialization, binding, etc. easier.
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> System
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> System.Web.Mvc
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> Microsoft.FSharp.Reflection
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">type</span> RecordDefaultModelBinder() = 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">inherit</span> DefaultModelBinder()
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> isrec = FSharpType.IsRecord
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: green">/// Makes a record, trying to provide
initialised values for each field    </span></p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span><span style="COLOR: blue">rec</span> makeDefaultRecord
ty = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> defval
ty = 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">if</span> isrec
ty <span style="COLOR: blue">then</span> makeDefaultRecord ty 
</p>
          <p style="MARGIN: 0px">
                   
    <span style="COLOR: blue">else</span><span style="COLOR: blue">match</span> ty.GetConstructor(Type.EmptyTypes) <span style="COLOR: blue">with</span><span style="COLOR: blue">null</span><span style="COLOR: blue">-&gt;</span><span style="COLOR: blue">null</span> |
c <span style="COLOR: blue">-&gt;</span> c.Invoke <span style="COLOR: blue">null</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> vals =
FSharpType.GetRecordFields ty |&gt; Array.map (<span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> defval
x.PropertyType)
</p>
          <p style="MARGIN: 0px">
        FSharpValue.MakeRecord(ty, vals)
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.CreateModel(cc,
bc, ty) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// We have to avoid
them calling Activator.CreateInstance on records</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec ty <span style="COLOR: blue">then</span> makeDefaultRecord(ty) <span style="COLOR: blue">else</span><span style="COLOR: blue">base</span>.CreateModel(cc,
bc, ty)
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.GetModelProperties(cc,
bc) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// Default one filters
out read-only, but we own the field</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec bc.ModelType <span style="COLOR: blue">then</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">let</span> props
= ComponentModel.TypeDescriptor.GetProperties(bc.ModelType)
</p>
          <p style="MARGIN: 0px">
                   
    |&gt; Seq.cast&lt;ComponentModel.PropertyDescriptor&gt; <span style="COLOR: green">//
BCLFail</span></p>
          <p style="MARGIN: 0px">
                   
    |&gt; Seq.filter(<span style="COLOR: blue">fun</span> p <span style="COLOR: blue">-&gt;</span> bc.PropertyFilter.Invoke(p.Name))
</p>
          <p style="MARGIN: 0px">
            ComponentModel.PropertyDescriptorCollection(Seq.to_array
props)
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">else</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">base</span>.GetModelProperties(cc,
bc) 
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> this.SetProperty(cc,
bc, propDesc, value) = 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: green">// To set a record
property, set the mangled field</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> isrec bc.ModelType <span style="COLOR: blue">then</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">let</span> field
= bc.ModelType.GetField(propDesc.Name + <span style="COLOR: maroon">"@"</span>, Reflection.BindingFlags.Instance
||| Reflection.BindingFlags.NonPublic)
</p>
          <p style="MARGIN: 0px">
            field.SetValue(bc.Model,
value)
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">else</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">base</span>.SetProperty(cc,
bc, propDesc, value)
</p>
        </div>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d5591bdc-add1-4fbc-b1d9-0c25359433a6" />
      </body>
      <title>F# Records and ASP.NET MVC Binding</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/07/16/F+Records+And+ASPNET+MVC+Binding.aspx</link>
      <pubDate>Thu, 16 Jul 2009 09:35:53 GMT</pubDate>
      <description>&lt;p&gt;
I came across a &lt;a href="http://cs.hubfs.net/forums/thread/11076.aspx"&gt;thread on hubFS
about deserialising F# records with JSON&lt;/a&gt;. After&amp;nbsp;&lt;a href="http://lorgonblog.spaces.live.com/"&gt;Mr.
McNamara&lt;/a&gt;&amp;nbsp;pointed out that DataContract serialization would work with F# records,
I realised we can do the same for other serialisation systems, such as ASP.NET MVC. 
&lt;/p&gt;
&lt;p&gt;
ASP.NET MVC binding doesn't work with F# records for a few reasons. First, it requires
a default constructor, and record types don't have one. Second, it needs settable
properties, and records have read-only properties. Fortunately, the backing field
for a record's property is a mutable field. The name is mangled (@ is appended), but
otherwise we're ok to set that field.
&lt;/p&gt;
&lt;p&gt;
With this, we can subclass the default model binder and add in code to construct records
as well as set their fields directly. Unlike DataContract serializers, I didn't use
FormatterServices.GetUninitializedObject to create the object, I use the F# reflection
function MakeRecord. This is because I want to attempt to initialise all fields on
the record type, to try to keep out nulls. This goes against how the rest of MVC's
null handling goes, so perhaps it's not a great idea. 
&lt;/p&gt;
&lt;p&gt;
At any rate, here's the quite short code. A lot of things probably don't work, such
as F# lists. Perhaps there should be a community project that collects F#-specific
type helpers for different frameworks to make serialization, binding, etc. easier.
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Web.Mvc
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; Microsoft.FSharp.Reflection
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; RecordDefaultModelBinder() = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;inherit&lt;/span&gt; DefaultModelBinder()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; isrec = FSharpType.IsRecord
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;/// Makes a record, trying to provide
initialised values for each field&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; &lt;span style="COLOR: blue"&gt;rec&lt;/span&gt; makeDefaultRecord
ty = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; defval
ty = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec
ty &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; makeDefaultRecord ty 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: blue"&gt;match&lt;/span&gt; ty.GetConstructor(Type.EmptyTypes) &lt;span style="COLOR: blue"&gt;with&lt;/span&gt; &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; |
c &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; c.Invoke &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; vals =
FSharpType.GetRecordFields ty |&amp;gt; Array.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; defval
x.PropertyType)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; FSharpValue.MakeRecord(ty, vals)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.CreateModel(cc,
bc, ty) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// We have to avoid
them calling Activator.CreateInstance on records&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec ty &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; makeDefaultRecord(ty) &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.CreateModel(cc,
bc, ty)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.GetModelProperties(cc,
bc) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// Default one filters
out read-only, but we own the field&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec bc.ModelType &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; props
= ComponentModel.TypeDescriptor.GetProperties(bc.ModelType)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.cast&amp;lt;ComponentModel.PropertyDescriptor&amp;gt; &lt;span style="COLOR: green"&gt;//
BCLFail&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.filter(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; p &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; bc.PropertyFilter.Invoke(p.Name))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ComponentModel.PropertyDescriptorCollection(Seq.to_array
props)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.GetModelProperties(cc,
bc) 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; this.SetProperty(cc,
bc, propDesc, value) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: green"&gt;// To set a&amp;nbsp;record
property, set the mangled field&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; isrec bc.ModelType &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; field
= bc.ModelType.GetField(propDesc.Name + &lt;span style="COLOR: maroon"&gt;"@"&lt;/span&gt;, Reflection.BindingFlags.Instance
||| Reflection.BindingFlags.NonPublic)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; field.SetValue(bc.Model,
value)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.SetProperty(cc,
bc, propDesc, value)
&lt;/p&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d5591bdc-add1-4fbc-b1d9-0c25359433a6" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,d5591bdc-add1-4fbc-b1d9-0c25359433a6.aspx</comments>
      <category>ASP.NET</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=cc0a8d5c-102f-452b-927c-59b2ca2017ca</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=cc0a8d5c-102f-452b-927c-59b2ca2017ca</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you use ASP.NET MVC with F# CTP (Monday brings Beta 1 and probably many changes),
you might run into an issue of how to scope certain things. For example, an IDisposable
is used to create blocks, for things like an HTML form. Example in C#:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
          <p style="MARGIN: 0px">
    <span style="BACKGROUND: #ffee62">&lt;%</span><span style="COLOR: blue">using</span> (Html.BeginForm())
{<span style="BACKGROUND: #ffee62">%&gt;</span></p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">fieldset</span><span style="COLOR: blue">&gt;</span><span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">legend</span><span style="COLOR: blue">&gt;</span>Fields<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">legend</span><span style="COLOR: blue">&gt;...</span><span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">fieldset</span><span style="COLOR: blue">&gt;</span></p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="BACKGROUND: #ffee62">&lt;%</span> } <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          <p style="MARGIN: 0px">
            <span style="BACKGROUND: #ffee62">
            </span> 
</p>
        </div>
        <p>
          <!--EndFragment-->This is needed because VB and C# didn't have any easy function/block
syntax (VB 10 should fix this), and many C# developers are still wary of higher order
functions. How does this play out in F#? First off, whitespace is important. This
can get really messy with the current F# ASP.NET integration. Basically, always open
the script blocks on a separate line, and indent from the first column. Example:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> let i = 0
</p>
            <p style="MARGIN: 0px">
   this.Response.Write(sprintf "i = %d" i) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
(Note that the 'this' variable is bound to the current page.) This fails:
</p>
        <p>
          <strong>Compiler Error Message: </strong>
          <font face="Arial">FS0010: Unexpected keyword
'let' or 'use' in expression. Expected incomplete structured construct at or before
this point or other token<br /><br /></font>
          <b>Source Error:</b>
          <br />
          <br />
          <table width="100%" bgcolor="#ffffcc">
            <tbody>
              <tr>
                <td>
                </td>
              </tr>
              <tr>
                <td>
                  <code>
                    <pre>Line 111:                let mutable parameterContainer = parameterContainer
Line 112:                __w.Write("\r\n") |&gt; ignore
<font color="red">Line
113: let i = 0 </font>Line 114: this.Response.Write(sprintf "i = %d" i) Line 115:
__w.Write("\r\n &lt;h2&gt;Create&lt;/h2&gt;\r\n\r\n ") |&gt; ignore</pre>
                  </code>
                </td>
              </tr>
            </tbody>
          </table>
        </p>
        <p>
Note how the &lt;% is counted as space, so the let starts off indented 3 spaces. Instead,
we need to write it so:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
          <p style="MARGIN: 0px">
            <span style="BACKGROUND: #ffee62">&lt;%</span>
          </p>
          <p style="MARGIN: 0px">
let i = 0
</p>
          <p style="MARGIN: 0px">
this.Response.Write(sprintf "i = %d" i) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
        </div>
        <!--EndFragment-->
        <p>
This works fine. You can also put the <span style="BACKGROUND: #ffee62">%&gt;</span> on
the next line if you like. Now, on to ASP.NET MVC's IDisposable usage. A straightforward
use of the F# using function won't work:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Demo<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span>
            </p>
            <p style="MARGIN: 0px">
using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&gt; <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
    <span style="COLOR: blue">&lt;</span><span style="COLOR: #a31515">p</span><span style="COLOR: blue">&gt;</span>Inside
a form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">p</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> ) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
          <strong>Compiler Error Message: </strong>
          <font face="Arial">FS0191: The mutable variable
'__w' is used in an invalid way. Mutable variables may not be captured by closures.
Consider eliminating this use of mutation or using a heap-allocated mutable reference
cell via 'ref' and '!'.<br /><br /></font>
          <b>Source Error:</b>
          <br />
          <br />
        </p>
        <p>
          <table width="100%" bgcolor="#ffffcc">
            <tbody>
              <tr>
                <td>
                </td>
              </tr>
              <tr>
                <td>
                  <code>
                    <pre>Line 114:                __w.Write("\r\n\r\n&lt;b&gt;Demo&lt;/b&gt;\r\n\r\n") |&gt; ignore
Line 115:                  
<font color="red">Line
116: using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&gt; </font>Line
117: __w.Write("\r\n\r\n &lt;p&gt;Inside a form&lt;/p&gt;\r\n\r\n") |&gt; ignore Line
118: ) </pre>
                  </code>
                </td>
              </tr>
            </tbody>
          </table>
        </p>
        <p>
As the error says, this is because the __w variable is mutable, so we can't play with
it inside a lambda. I'm not sure if this will be worked around -- they'd have to change
the codegen quite a bit, I'd think. As a side note, the F# CTP does not support C#
extension methods (hence the verbose calling of BeginForm), but F# will eventually
-- maybe in the Beta.
</p>
        <p>
The way we must scope is with a use binding. There's no way I see to accomplish this
with whitespace alone. Due to the ASPX translation process, this would probably be
very error prone. Instead, we can simply put the use binding inside a do expression:
</p>
        <blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
          <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt">
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Demo<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span>
            </p>
            <p style="MARGIN: 0px">
do (use form = Mvc.Html.FormExtensions.BeginForm this.Html <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Inside
the form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="BACKGROUND: #ffee62">&lt;%</span> ) <span style="BACKGROUND: #ffee62">%&gt;</span></p>
            <p style="MARGIN: 0px">
 
</p>
            <p style="MARGIN: 0px">
              <span style="COLOR: blue">&lt;</span>
              <span style="COLOR: #a31515">b</span>
              <span style="COLOR: blue">&gt;</span>Outside
of the form<span style="COLOR: blue">&lt;/</span><span style="COLOR: #a31515">b</span><span style="COLOR: blue">&gt;</span></p>
          </div>
        </blockquote>
        <!--EndFragment-->
        <p>
The parentheses setup the scope exactly how we want it.
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=cc0a8d5c-102f-452b-927c-59b2ca2017ca" />
      </body>
      <title>F# CTP and ASP.NET MVC ASPX: Scoping disposables</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/05/15/F+CTP+And+ASPNET+MVC+ASPX+Scoping+Disposables.aspx</link>
      <pubDate>Fri, 15 May 2009 20:46:39 GMT</pubDate>
      <description>&lt;p&gt;
If you use ASP.NET MVC with F# CTP (Monday brings Beta 1 and probably many changes),
you might run into an issue of how to scope certain things. For example, an IDisposable
is used to create blocks, for things like an HTML form. Example in C#:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="COLOR: blue"&gt;using&lt;/span&gt; (Html.BeginForm())
{&lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;legend&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Fields&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;legend&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;...&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;!--EndFragment--&gt;This is needed because VB and C# didn't have any easy function/block
syntax (VB 10 should fix this), and many C# developers are still wary of higher order
functions. How does this play out in F#? First off, whitespace is important. This
can get really messy with the current F# ASP.NET integration. Basically, always open
the script blocks on a separate line, and indent from the first column. Example:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; let i = 0
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp; this.Response.Write(sprintf "i = %d" i) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
(Note that the 'this' variable is bound to the current page.)&amp;nbsp;This fails:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Compiler Error Message: &lt;/strong&gt;&lt;font face=Arial&gt;FS0010: Unexpected keyword
'let' or 'use' in expression. Expected incomplete structured construct at or before
this point or other token&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;&lt;b&gt;Source Error:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;table width="100%" bgcolor=#ffffcc&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;&lt;pre&gt;Line 111:                let mutable parameterContainer = parameterContainer
Line 112:                __w.Write("\r\n") |&amp;gt; ignore
&lt;font color=red&gt;Line
113: let i = 0 &lt;/font&gt;Line 114: this.Response.Write(sprintf "i = %d" i) Line 115:
__w.Write("\r\n &amp;lt;h2&amp;gt;Create&amp;lt;/h2&amp;gt;\r\n\r\n ") |&amp;gt; ignore&lt;/pre&gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
Note how the &amp;lt;% is counted as space, so the let starts off indented&amp;nbsp;3 spaces.&amp;nbsp;Instead,
we need to write it so:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
let i = 0
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
this.Response.Write(sprintf "i = %d" i) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
This works fine. You can also put the &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt; on
the next line if you like.&amp;nbsp;Now, on to ASP.NET MVC's IDisposable usage. A straightforward
use of the F# using function won't work:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Demo&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&amp;gt; &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;p&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Inside
a form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;p&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; ) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
&lt;strong&gt;Compiler Error Message: &lt;/strong&gt;&lt;font face=Arial&gt;FS0191: The mutable variable
'__w' is used in an invalid way. Mutable variables may not be captured by closures.
Consider eliminating this use of mutation or using a heap-allocated mutable reference
cell via 'ref' and '!'.&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;&lt;b&gt;Source Error:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table width="100%" bgcolor=#ffffcc&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;&lt;pre&gt;Line 114:                __w.Write("\r\n\r\n&amp;lt;b&amp;gt;Demo&amp;lt;/b&amp;gt;\r\n\r\n") |&amp;gt; ignore
Line 115:                  
&lt;font color=red&gt;Line
116: using (Mvc.Html.FormExtensions.BeginForm this.Html) (fun _ -&amp;gt; &lt;/font&gt;Line
117: __w.Write("\r\n\r\n &amp;lt;p&amp;gt;Inside a form&amp;lt;/p&amp;gt;\r\n\r\n") |&amp;gt; ignore Line
118: ) &lt;/pre&gt;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
As the error says, this is because the __w variable is mutable, so we can't play with
it inside a lambda. I'm not sure if this will be worked around -- they'd have to change
the codegen quite a bit, I'd think. As a side note, the F# CTP does not support C#
extension methods (hence the verbose calling of BeginForm), but F# will eventually
-- maybe in the Beta.
&lt;/p&gt;
&lt;p&gt;
The way we must scope is with a use binding. There's no way I see to accomplish this
with whitespace alone. Due to the ASPX translation process, this would probably be
very error prone. Instead, we can simply put the use binding inside a do expression:
&lt;/p&gt;
&lt;blockquote style="MARGIN-RIGHT: 0px" dir=ltr&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 14pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Demo&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
do (use form = Mvc.Html.FormExtensions.BeginForm this.Html &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Inside
the form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/span&gt; ) &lt;span style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Outside
of the form&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;b&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
The parentheses setup the scope exactly how we want it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=cc0a8d5c-102f-452b-927c-59b2ca2017ca" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,cc0a8d5c-102f-452b-927c-59b2ca2017ca.aspx</comments>
      <category>ASP.NET</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=7583965b-185a-4677-ad9e-96c1d6e8abaa</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,7583965b-185a-4677-ad9e-96c1d6e8abaa.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,7583965b-185a-4677-ad9e-96c1d6e8abaa.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=7583965b-185a-4677-ad9e-96c1d6e8abaa</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.atrevido.net/blog/2009/04/01/Using+Fluent+NHibernate+With+F.aspx">Last
time</a>, I hacked up some rudimentary support for F# and Fluent NHibernate. It really
looked ugly. I looked into what I could do to make it not look as sucky. 
</p>
        <p>
First off, having to write a full lambda for each mapping was annoying. F# quotations
don't have to be lambdas, unlike C#'s expressions. So, instead of having to write,
say, "fun x -&gt; x.Foo", we can write "x.Foo", assuming there's a local variable
x with the right type. The ClassMap subclass now expects these types of quotations
than full lambdas.
</p>
        <p>
Next, I experimented with using type extensions to overload functions like Id and
Map, however I found out that F#, at least currently, does not add in type extensions
for overload resolution. So I ended up having a new subclass of ClassMap&lt;T&gt;,
'T ClassMapQ. I used the Q suffix consistently to denote "quotations". I added type
extensions for most of the other mapping types so that quotations could be used.
</p>
        <p>
As to the question of having to tag on " |&gt; ignore " to each mapping, I decided
to write an extension propery for IMappingPart called Done, which is simply unit.
Finally, the problem of lazy loading I took care of by setting "use_proxy_validator"
to false, as mentioned <a href="http://stackoverflow.com/questions/741489/ignore-public-internal-fields-for-nhibernate-proxy">here</a>.
The end result is that this mapping code:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <pre>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">type
StoreMap() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> inherit
ClassMap&lt;Store&gt;() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">do</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">base</span>.Not.LazyLoad() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">base</span>.Id
~@@ &lt;@ fun x -&gt; x.Id @&gt; |&gt; ignore <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">base</span>.Map
~@@ &lt;@ fun x -&gt; x.Name @&gt; |&gt; ignore (<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">base</span>.HasManyX
&lt;@ fun x -&gt; upcast x.Staff @&gt;) .LazyLoad() .Inverse().Cascade.All() |&gt;
ignore (<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">base</span>.HasManyToManyX
&lt;@ fun x -&gt; upcast x.Products @&gt;) .Cascade.All() .WithTableName(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"StoreProduct"</span>)
|&gt; ignore </span>
          </pre>
        </div>
        <!--EndFragment-->
        <p>
Is now:
</p>
        <p>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">type
StoreMap() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">as</span> m <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> inherit
ClassMapQ&lt;Store&gt;() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">do</span><br />
    let x <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> m.DefaultX<br />
    (m.IdQ &lt;@ x.Id @&gt;).Done<br />
    (m.MapQ &lt;@ x.Name @&gt;).Done<br />
    (m.HasManyQ &lt;@ seq x.Staff @&gt;)<br />
        .LazyLoad()<br />
        .Inverse().Cascade.All()<br />
        .Done<br />
    (m.HasManyToManyQ &lt;@ seq x.Products @&gt;)<br />
        .LazyLoad()<br />
        .Cascade.All()<br />
        .WithTableName(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"StoreProduct"</span>)<br />
        .Done</span>
        </p>
        <p>
This is pretty enough that I'm satisfied with how well F# interops. Most of the things
I figured out here will apply to other .NET OO APIs, not just this one.
</p>
        <p>
I am hoping that VS2010 Beta 1 will ship soon, as F# is getting some interesting upgrades
then. With that, it should be easier to extend the support to other parts of NHibernate,
such as querying, and perhaps integrate in NHibernate.Linq.
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=7583965b-185a-4677-ad9e-96c1d6e8abaa" />
      </body>
      <title>Update on F# with Fluent NHibernate</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,7583965b-185a-4677-ad9e-96c1d6e8abaa.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/04/23/Update+On+F+With+Fluent+NHibernate.aspx</link>
      <pubDate>Thu, 23 Apr 2009 19:06:45 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.atrevido.net/blog/2009/04/01/Using+Fluent+NHibernate+With+F.aspx"&gt;Last
time&lt;/a&gt;, I hacked up some rudimentary support for F# and Fluent NHibernate. It really
looked ugly.&amp;nbsp;I&amp;nbsp;looked into what I could&amp;nbsp;do to make it not look as sucky. 
&lt;/p&gt;
&lt;p&gt;
First off, having to write a full lambda for each mapping was annoying. F# quotations
don't have to be lambdas, unlike C#'s expressions. So, instead of having to write,
say, "fun x -&amp;gt; x.Foo", we can write "x.Foo", assuming there's a local variable
x with the right type. The ClassMap subclass now expects these types of quotations
than full lambdas.
&lt;/p&gt;
&lt;p&gt;
Next, I experimented with using type extensions to overload functions like Id and
Map, however I found out that F#, at least currently, does not add in type extensions
for overload resolution. So I ended up having a new subclass of ClassMap&amp;lt;T&amp;gt;,
'T ClassMapQ. I used the Q suffix consistently to denote "quotations". I added type
extensions for most of the other mapping types so that quotations could be used.
&lt;/p&gt;
&lt;p&gt;
As to the question of having to tag on " |&amp;gt; ignore " to each mapping, I decided
to write an extension propery for IMappingPart&amp;nbsp;called Done, which is simply unit.
Finally, the problem of lazy loading I took care of by setting "use_proxy_validator"
to false, as mentioned &lt;a href="http://stackoverflow.com/questions/741489/ignore-public-internal-fields-for-nhibernate-proxy"&gt;here&lt;/a&gt;.
The end result is that this mapping code:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;type
StoreMap() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; inherit
ClassMap&amp;lt;Store&amp;gt;() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;do&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;base&lt;/span&gt;.Not.LazyLoad() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;base&lt;/span&gt;.Id
~@@ &amp;lt;@ fun x -&amp;gt; x.Id @&amp;gt; |&amp;gt; ignore &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;base&lt;/span&gt;.Map
~@@ &amp;lt;@ fun x -&amp;gt; x.Name @&amp;gt; |&amp;gt; ignore (&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;base&lt;/span&gt;.HasManyX
&amp;lt;@ fun x -&amp;gt; upcast x.Staff @&amp;gt;) .LazyLoad() .Inverse().Cascade.All() |&amp;gt;
ignore (&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;base&lt;/span&gt;.HasManyToManyX
&amp;lt;@ fun x -&amp;gt; upcast x.Products @&amp;gt;) .Cascade.All() .WithTableName(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"StoreProduct"&lt;/span&gt;)
|&amp;gt; ignore &lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Is now:
&lt;/p&gt;
&lt;p&gt;
&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;type
StoreMap() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;as&lt;/span&gt; m &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; inherit
ClassMapQ&amp;lt;Store&amp;gt;() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;do&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; let x &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; m.DefaultX&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (m.IdQ &amp;lt;@ x.Id @&amp;gt;).Done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (m.MapQ &amp;lt;@ x.Name @&amp;gt;).Done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (m.HasManyQ &amp;lt;@ seq x.Staff @&amp;gt;)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LazyLoad()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Inverse().Cascade.All()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Done&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (m.HasManyToManyQ &amp;lt;@ seq x.Products @&amp;gt;)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LazyLoad()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Cascade.All()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .WithTableName(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"StoreProduct"&lt;/span&gt;)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Done&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
This is pretty enough that I'm satisfied with how well F# interops. Most of the things
I figured out here will apply to other .NET OO APIs, not just this one.
&lt;/p&gt;
&lt;p&gt;
I am hoping that VS2010 Beta 1 will ship soon, as F# is getting some interesting upgrades
then. With that, it should be easier to extend the support to other parts of NHibernate,
such as querying, and perhaps integrate in NHibernate.Linq.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=7583965b-185a-4677-ad9e-96c1d6e8abaa" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,7583965b-185a-4677-ad9e-96c1d6e8abaa.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=e66f49f0-22da-4fac-b100-f2a5b0a0ca29</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,e66f49f0-22da-4fac-b100-f2a5b0a0ca29.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,e66f49f0-22da-4fac-b100-f2a5b0a0ca29.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e66f49f0-22da-4fac-b100-f2a5b0a0ca29</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Fluent NHibernate is a nice way to be able to use NHibernate without having to deal
with all that unchecked XML. This morning I decided to find out how well it works
with F#. Things went relatively smooth. I've converted some code samples from the <a href="http://wiki.fluentnhibernate.org/show/GettingStarted%3A+First+Project">Fluent
NHibernate First Project</a>. I suggest having that open to fill in any gaps. I've
also included the full project code and DB script at the end of this article.
</p>
        <p>
If you're not familiar at all with Fluent NHibernate, basically it takes advantage
of lambda expressions as Expression&lt;T&gt; to provide a somewhat strongly typechecked
reflection system. Thus, instead of having attributes with hard-coded strings, or
XML files, you have expressions that target the properties of objects you wish to
map. The Fluent NHibernate library then takes care of hooking it up to NHibernate,
and away you go. Something like that anyways.
</p>
        <p>
          <strong>Classes</strong>
        </p>
        <p>
So, first, we define the "entities" like the C# project does. Here's the first little
pain. F# doesn't really support C#'s idea of "automatic properties". You can have
vals on a class, which act like fields (although, they are implemented as properties).
Or, you can manually specify them, like you would in earlier versions of C# which
didn't have the auto-gen-a-field-for-me. 
</p>
        <p>
F# doesn't encourage uninitialized values. If you have uninitialized fields (vals),
you need to mark them with an attribute to say you know what you're doing. So, that
adds a bit more code overhead. What I do here is to alias the DefaultValueAttribute
to "DV". So the first bit of our classes looks like this:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">#light</span>
          </p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">namespace</span> FHib.Entities
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">open</span> System.Collections.Generic
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">type</span> DV = DefaultValueAttribute
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">type</span> Employee() <span style="COLOR: blue">as</span> this
=
</p>
          <p style="MARGIN: 0px">
    [&lt;DV&gt;] <span style="COLOR: blue">val</span><span style="COLOR: blue">mutable</span> Id
: int
</p>
          <p style="MARGIN: 0px">
    [&lt;DV&gt;] <span style="COLOR: blue">val</span><span style="COLOR: blue">mutable</span> FirstName
: string
</p>
          <p style="MARGIN: 0px">
    [&lt;DV&gt;] <span style="COLOR: blue">val</span><span style="COLOR: blue">mutable</span> LastName
: string
</p>
        </div>
        <!--EndFragment-->
        <p>
Now, NHibernate relies on having virtual properties so that it can dynamically create
code to do nifty things like lazy loading. In F#, creating a virtual member means
defining an abstract member and providing an implementation, in the same class. Since
we don't have automatic properties, this means we'll have to define the backing field
ourselfs. In all, the full code for the virtual property "Store" (virtual so NHibernate
can lazy-load it) is:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">abstract</span> Store : Store <span style="COLOR: blue">with</span> get,set
</p>
          <p style="MARGIN: 0px">
    [&lt;DV(<span style="COLOR: blue">false</span>)&gt;] <span style="COLOR: blue">val</span><span style="COLOR: blue">mutable</span> _store
: Store
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">override</span> x.Store <span style="COLOR: blue">with</span> get()
= x._store <span style="COLOR: blue">and</span> set(v) = x._store &lt;- v
</p>
        </div>
        <!--EndFragment-->
        <p>
Not the pinnacle of short code, but not horrible all things considered. The rest
of the entity mappings are rather straightforward so I'll skip them here. I stuck
with vals for anything that didn't have to be virtual, to keep it more concise.
</p>
        <p>
          <strong>Mappings: Easy Start</strong>
        </p>
        <p>
OK, so now to the "real" work. Fluent NHiberate looks for classes that subclass
ClassMap&lt;T&gt;. It then creates an instance of them, which allows you to call the
mapping methods in the object's constructor. From the First Project:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> EmployeeMap
: ClassMap&lt;Employee&gt; { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> EmployeeMap()
{ Id(x =&gt; x.Id); } } </span>
        </pre>
        <p>
OK, so how do we convert this to F#? It's easy... except for that lambda. The lambda
in this case compiles to an Expression&lt;Func&lt;Employee, object&gt;&gt;. F#'s compiler
does not support Expression&lt;T&gt;. So, we turn to experimental support. Referencing
the FSharp.PowerPack.Linq assembly gives us the Microsoft.FSharp.Linq.QuotationEvaluation
module. This extends the F# quotation type, Expr, with "ToLinqExpression", which returns
an untyped LINQ Expression object. 
</p>
        <p>
To get this untyped Expression (LINQ) out of an Expr (F#) and into an Expression&lt;T&gt;
(F#) suitable for Fluent NHibernate's consumption, I started off writing this tiny
helper module:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">module</span> LinqHelper =
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">open</span> Microsoft.FSharp.Quotations
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">open</span> Microsoft.FSharp.Linq.QuotationEvaluation
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">open</span> System.Linq.Expressions
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> ToLinq (exp : Expr&lt;'a <span style="COLOR: blue">-&gt;</span> 'b&gt;)
=
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> linq =
exp.ToLinqExpression()
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> call =
linq :?&gt; MethodCallExpression
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> lambda
= call.Arguments.[0] :?&gt; LambdaExpression
</p>
          <p style="MARGIN: 0px">
        Expression.Lambda&lt;Func&lt;'a, 'b&gt;&gt;(lambda.Body,
lambda.Parameters) 
</p>
        </div>
        <!--EndFragment-->
        <p>
When an F# quotation of a lambda is turned into a LINQ expression, the root node is
a useless MethodCallExpression. So, we unwrap that by taking it's argument and using
it to generate a typed lambda Expression&lt;T&gt;.  
</p>
        <p>
This is a good start. But many of the Expressions that Fluent NHibernate looks for
have a return type of object. Instead of having to write "box" or "upcast" all over,
I added another function called "ToLinqObj". This takes an Expr&lt;'a -&gt; 'b&gt;,
but returns an Expression&lt;Func&lt;'a, obj&gt;&gt;. 
</p>
        <p>
Finally, writing "ToLinq" and "ToLinqObj" seemed too verbose, so I added some operators
to the LinqHelper module:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> (~@) expr = ToLinq expr
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> (~@@) expr = ToLinqObj expr
</p>
        </div>
        <!--EndFragment-->
        <p>
Now we can start mapping:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <span style="COLOR: blue">
            <font color="#0000ff" size="3">
              <font color="#0000ff" size="3">
                <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
                  <p style="MARGIN: 0px">
                    <span style="COLOR: blue">open</span> LinqHelper
</p>
                  <p style="MARGIN: 0px">
 
</p>
                  <p style="MARGIN: 0px">
                    <span style="COLOR: blue">type</span> EmployeeMap() = <span style="COLOR: blue">inherit</span> ClassMap&lt;Employee&gt;() <span style="COLOR: blue">do</span></p>
                  <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Not.LazyLoad()
</p>
                  <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Id ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.Id
@&gt; |&gt; ignore
</p>
                  <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Map ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.FirstName
@&gt; |&gt; ignore
</p>
                  <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Map ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.LastName
@&gt; |&gt; ignore
</p>
                  <p style="MARGIN: 0px">
    (<span style="COLOR: blue">base</span>.References ~@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.Store
@&gt;).LazyLoad() |&gt; ignore
</p>
                </div>
                <!--EndFragment-->
              </font>
            </font>
          </span>
        </div>
        <!--EndFragment-->
        <p>
We start off by disabling LazyLoad because most of the properties are not virtual,
and NHibernate will fail to validate the mapping. Instead, we explicitly LazyLoad
things, like the Store reference. 
</p>
        <p>
          <strong>Mappings: Modifying the Expressions</strong>
        </p>
        <p>
Unfortunately, things didn't stay so simple. The Fluent NHibernate methods "HasMany"
and "HasManyToMany", for instance, don't work with F#'s type inference. This is because
they have several overloads, a couple of which take expressions. If we try this in
the StoreMap:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">    base</span>.HasMany ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.Staff
@&gt; |&gt; ignore
</p>
        </div>
        <!--EndFragment-->
        <p>
We get an error because F# doesn't know what x is. (error FS0055: This lookup uses
a deprecated feature, where a class type is inferred from the use of a class field
label. Consider using a type annotation to make it clear which class the field comes
from.) Using ~@ to keep it strongly typed fails as well; it can't figure out the overload.
This is nothing surprising -- overloading is the enemy of type inference.
</p>
        <p>
So, what do we do? Type annotations are not what I consider fun. So instead, we'll
add non-overloaded ClassMap&lt;'t&gt; type extensions into the LinqHelper module:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">type</span> ClassMap&lt;'t&gt; <span style="COLOR: blue">with</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">member</span> x.HasManyX
expr = (x.HasMany : Expression&lt;Func&lt;'t, seq&lt;_&gt;&gt;&gt; <span style="COLOR: blue">-&gt;</span> _)
(ToLinq expr)
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">member</span> x.HasManyToManyX
expr = (x.HasManyToMany : Expression&lt;Func&lt;'t, seq&lt;_&gt;&gt;&gt; <span style="COLOR: blue">-&gt;</span> _)
(ToLinq expr)
</p>
        </div>
        <!--EndFragment-->
        <p>
By providing these extensions that are explicit once, we enable type inference for
the rest of the time. We can now finish the StoreMap:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">type</span> StoreMap() = <span style="COLOR: blue">inherit</span> ClassMap&lt;Store&gt;() <span style="COLOR: blue">do</span></p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Not.LazyLoad()
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Id ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.Id
@&gt; |&gt; ignore
</p>
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">base</span>.Map ~@@ &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span> x.Name
@&gt; |&gt; ignore
</p>
          <p style="MARGIN: 0px">
 
</p>
          <p style="MARGIN: 0px">
    (<span style="COLOR: blue">base</span>.HasManyX &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span><span style="COLOR: blue">upcast</span> x.Staff
@&gt;)
</p>
          <p style="MARGIN: 0px">
        .LazyLoad()
</p>
          <p style="MARGIN: 0px">
        .Inverse().Cascade.All() |&gt; ignore
</p>
          <p style="MARGIN: 0px">
    (<span style="COLOR: blue">base</span>.HasManyToManyX &lt;@ <span style="COLOR: blue">fun</span> x <span style="COLOR: blue">-&gt;</span><span style="COLOR: blue">upcast</span> x.Products
@&gt;)
</p>
          <p style="MARGIN: 0px">
        .Cascade.All()
</p>
          <p style="MARGIN: 0px">
        .WithTableName(<span style="COLOR: maroon">"StoreProduct"</span>)
|&gt; ignore
</p>
        </div>
        <!--EndFragment-->
        <p>
The only type "annotation" we need here is the upcast for the HasManyX. This
is because F# forces you to be explicit. Accessing "Staff" in the first quotation
means type IList&lt;Employee&gt;, not seq&lt;Employee&gt;. The upcast will sort this
out for us.
</p>
        <p>
This compiles fine. But remember how I said using expressions was a "somewhat strongly
typed" way of doing things? The expression trees are interpreted at runtime, which
allows failures a more complex type system might prevent. This is one of the times.
If we try to execute it as-is, we get this exception:
</p>
        <p>
          <font face="Courier New"> ---&gt; FluentNHibernate.Cfg.FluentConfigurationException:
An invalid or incomplete configuration was used while creating a SessionFactory. Check
PotentialReasons collection, and InnerException for more detail.</font>
        </p>
        <p>
          <font face="Courier New"> ---&gt; System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---&gt; System.ArgumentException:
Not a member access Parameter name: member</font>
        </p>
        <p>
Ouch. Fluent NHibernate does not appreciate our expression trees. Why? Using FSI,
we can inspect what we're actually converting the F# quotations to:
</p>
        <p>
          <font face="Courier New">type SomeClass() = member x.Stuff = [|1;2;3|];;<br />
let myExpr : Expr&lt;SomeClass -&gt; seq&lt;int&gt;&gt; = &lt;@ fun x -&gt; upcast
x.Stuff @&gt;;;<br />
let myLinq = LinqHelper.ToLinq myExpr;;</font>
        </p>
        <p>
          <font face="Courier New">&gt; myLinq;;<br />
val it : Linq.Expressions.Expression&lt;Func&lt;SomeClass,seq&lt;int&gt;&gt;&gt;<br />
= x =&gt; (x.Stuff As IEnumerable`1)<br />
    {Body = (x.Stuff As IEnumerable`1);<br />
     NodeType = Lambda;<br />
     Parameters = seq [x];<br />
     Type = System.Func`2[FSI_0022+SomeClass,System.Collections.Generic.IEnumerable`1[System.Int32]];}</font>
        </p>
        <p>
          <font face="Courier New">&gt; myLinq.Body;;<br />
val it : Linq.Expressions.Expression<br />
= (x.Stuff As IEnumerable`1)<br />
    {IsLifted = false;<br />
     IsLiftedToNull = false;<br />
     Method = null;<br />
     NodeType = TypeAs;<br />
     Operand = x.Stuff;<br />
     Type = System.Collections.Generic.IEnumerable`1[System.Int32];}</font>
        </p>
        <p>
So, what's going on? The upcast is modifying the F# quotation (as it should), and
the ToLinqExpression is sticking this in as a "TypeAs" node. Fluent NHibernate does
not seem to like this. Not. One. Bit.
</p>
        <p>
But, it IS ok if we have a Convert node in the Expression tree. I imagine this is
because C# generates such nodes in its expression trees (say, accessing an int member
in a Func&lt;T, object&gt; expression). So, our last hack in making F# work right
here is adding a fixup function to our LinqHelper, and using it from ToLinq:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
    <span style="COLOR: blue">let</span> fixup (lexpr:LambdaExpression)
= 
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">if</span> lexpr.Body.NodeType
&lt;&gt; ExpressionType.TypeAs <span style="COLOR: blue">then</span> lexpr <span style="COLOR: blue">else</span></p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> typeAs
= lexpr.Body :?&gt; UnaryExpression
</p>
          <p style="MARGIN: 0px">
        <span style="COLOR: blue">let</span> newBody
= Expression.Convert(typeAs.Operand, typeAs.Type)
</p>
          <p style="MARGIN: 0px">
        Expression.Lambda(lexpr.Type, newBody, lexpr.Parameters)
</p>
        </div>
        <!--EndFragment-->
        <p>
Now it's happy with our expressions. 
</p>
        <p>
          <strong>Finally</strong>
        </p>
        <p>
The rest of the First Project is pretty straightforward in F#. For instance, creating
the Session Factory:
</p>
        <div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt">
          <p style="MARGIN: 0px">
            <span style="COLOR: blue">let</span> createSessionFactory() = 
</p>
          <p style="MARGIN: 0px">
    Fluently.Configure()
</p>
          <p style="MARGIN: 0px">
        .Database(
</p>
          <p style="MARGIN: 0px">
            MsSqlConfiguration.MsSql2005.ConnectionString(<span style="COLOR: blue">fun</span> csb <span style="COLOR: blue">-&gt;</span></p>
          <p style="MARGIN: 0px">
                csb.Is(<span style="COLOR: maroon">"server=(local);database=fhib;Integrated
Security=true"</span>) |&gt; ignore))
</p>
          <p style="MARGIN: 0px">
        .Mappings(<span style="COLOR: blue">fun</span> m <span style="COLOR: blue">-&gt;</span></p>
          <p style="MARGIN: 0px">
            m.FluentMappings.AddFromAssemblyOf&lt;Mappings.EmployeeMap&gt;()
|&gt; ignore)
</p>
          <p style="MARGIN: 0px">
        .BuildSessionFactory()
</p>
        </div>
        <!--EndFragment-->
        <p>
Everything seems to execute as it should. Fluent interfaces in F# generate a bit more
noise, because of all the ignores that have to be added. I'm thinking of creating
a type extension to obj to add Ignore as a method, to make it look a bit more uniform.
</p>
        <p>
          <strong>Future</strong>
        </p>
        <p>
It's my hope that F# has now addressed most of these issues -- 1.9.6 is 7 months old.
Expression&lt;T&gt; is growing in importance, outside of LINQ querying, so I cannot
see F# not being able to handle them easily and generating similar output to C#. It'd
be really neat if F# would auto-convert quotations to Expression&lt;T&gt; when the expr
is a syntactic argument like it does with delegates now. I'd also be surprised
if abstract/virtual members still lack the ability to define accessibility. 
</p>
        <p>
Code: <a href="http://www.atrevido.net/blog/content/binary/fhib.zip">fhib.zip (4.76
KB)</a><br />
I did not include Fluent NHibernate or any of its dependencies. The project expects
them to be in the project's lib folder, so get them <a href="http://fluentnhibernate.org/downloads">here</a> if
you don't have them and extract them to "lib".
</p>
        <p>
I welcome comments on this and suggestions for making the code more concise.
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e66f49f0-22da-4fac-b100-f2a5b0a0ca29" />
      </body>
      <title>Using Fluent NHibernate with F#</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,e66f49f0-22da-4fac-b100-f2a5b0a0ca29.aspx</guid>
      <link>http://www.atrevido.net/blog/2009/04/01/Using+Fluent+NHibernate+With+F.aspx</link>
      <pubDate>Wed, 01 Apr 2009 08:01:29 GMT</pubDate>
      <description>&lt;p&gt;
Fluent NHibernate is a nice way to be able to use NHibernate without having to deal
with all that unchecked XML. This morning I decided to find out how well it works
with F#. Things went relatively smooth. I've converted some code samples from the &lt;a href="http://wiki.fluentnhibernate.org/show/GettingStarted%3A+First+Project"&gt;Fluent
NHibernate First Project&lt;/a&gt;. I suggest having that open to fill in any gaps. I've
also included the full project code and DB script at the end of this article.
&lt;/p&gt;
&lt;p&gt;
If you're not familiar at all with Fluent NHibernate, basically it takes advantage
of lambda expressions as Expression&amp;lt;T&amp;gt; to provide a somewhat strongly typechecked
reflection system. Thus, instead of having attributes with hard-coded strings, or
XML files, you have expressions that target the properties of objects you wish to
map. The Fluent NHibernate library then takes care of hooking it up to NHibernate,
and away you go. Something like that anyways.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Classes&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So, first, we define the "entities" like the C# project does. Here's the first little
pain. F# doesn't really support C#'s idea of "automatic properties". You can have
vals on a class, which act like fields (although, they are implemented as properties).
Or, you can manually specify them, like you would in earlier versions of C# which
didn't have the auto-gen-a-field-for-me. 
&lt;/p&gt;
&lt;p&gt;
F# doesn't encourage uninitialized values. If you have uninitialized fields (vals),
you need to mark them with an attribute to say you know what you're doing. So, that
adds a bit more code overhead. What I do here is to alias the DefaultValueAttribute
to "DV". So the first bit of our classes looks like this:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;#light&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;namespace&lt;/span&gt; FHib.Entities
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Collections.Generic
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; DV = DefaultValueAttribute
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; Employee() &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; this
=
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV&amp;gt;] &lt;span style="COLOR: blue"&gt;val&lt;/span&gt; &lt;span style="COLOR: blue"&gt;mutable&lt;/span&gt; Id
: int
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV&amp;gt;] &lt;span style="COLOR: blue"&gt;val&lt;/span&gt; &lt;span style="COLOR: blue"&gt;mutable&lt;/span&gt; FirstName
: string
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV&amp;gt;] &lt;span style="COLOR: blue"&gt;val&lt;/span&gt; &lt;span style="COLOR: blue"&gt;mutable&lt;/span&gt; LastName
: string
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Now, NHibernate relies on having virtual properties so that it can dynamically create
code to do nifty things like lazy loading. In F#, creating a virtual member means
defining an abstract member and providing an implementation, in the same class. Since
we don't have automatic properties, this means we'll have to define the backing field
ourselfs. In all, the full code for the virtual property "Store" (virtual so NHibernate
can lazy-load it) is:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;abstract&lt;/span&gt; Store : Store &lt;span style="COLOR: blue"&gt;with&lt;/span&gt; get,set
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV(&lt;span style="COLOR: blue"&gt;false&lt;/span&gt;)&amp;gt;] &lt;span style="COLOR: blue"&gt;val&lt;/span&gt; &lt;span style="COLOR: blue"&gt;mutable&lt;/span&gt; _store
: Store
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;override&lt;/span&gt; x.Store &lt;span style="COLOR: blue"&gt;with&lt;/span&gt; get()
= x._store &lt;span style="COLOR: blue"&gt;and&lt;/span&gt; set(v) = x._store &amp;lt;- v
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Not the pinnacle of short code, but not horrible all things considered.&amp;nbsp;The rest
of the entity mappings are rather straightforward so I'll skip them here. I stuck
with vals for anything that didn't have to be virtual, to keep it more concise.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Mappings: Easy Start&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
OK, so now to the "real" work.&amp;nbsp;Fluent NHiberate looks for classes that subclass
ClassMap&amp;lt;T&amp;gt;. It then creates an instance of them, which allows you to call the
mapping methods in the object's constructor.&amp;nbsp;From the First Project:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; EmployeeMap
: ClassMap&amp;lt;Employee&amp;gt; { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; EmployeeMap()
{ Id(x =&amp;gt; x.Id); } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
OK, so how do we convert this to F#? It's easy... except for that lambda. The lambda
in this case compiles to an Expression&amp;lt;Func&amp;lt;Employee, object&amp;gt;&amp;gt;. F#'s compiler
does not support Expression&amp;lt;T&amp;gt;. So, we turn to experimental support. Referencing
the FSharp.PowerPack.Linq assembly gives us the Microsoft.FSharp.Linq.QuotationEvaluation
module. This extends the F# quotation type, Expr, with "ToLinqExpression", which returns
an untyped LINQ Expression object. 
&lt;/p&gt;
&lt;p&gt;
To get this untyped Expression (LINQ) out of an Expr (F#) and into an Expression&amp;lt;T&amp;gt;
(F#) suitable for Fluent NHibernate's consumption, I started off writing this tiny
helper module:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;module&lt;/span&gt; LinqHelper =
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;open&lt;/span&gt; Microsoft.FSharp.Quotations
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;open&lt;/span&gt; Microsoft.FSharp.Linq.QuotationEvaluation
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Linq.Expressions
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; ToLinq (exp : Expr&amp;lt;'a &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; 'b&amp;gt;)
=
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; linq =
exp.ToLinqExpression()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; call =
linq :?&amp;gt; MethodCallExpression
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; lambda
= call.Arguments.[0] :?&amp;gt; LambdaExpression
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Expression.Lambda&amp;lt;Func&amp;lt;'a, 'b&amp;gt;&amp;gt;(lambda.Body,
lambda.Parameters) 
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
When an F# quotation of a lambda is turned into a LINQ expression, the root node is
a useless MethodCallExpression. So, we unwrap that by taking it's argument and using
it to generate a typed lambda Expression&amp;lt;T&amp;gt;.&amp;nbsp;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
This is a good start. But many of the Expressions that Fluent NHibernate looks for
have a return type of object. Instead of having to write "box" or "upcast" all over,
I added another function called "ToLinqObj". This takes an Expr&amp;lt;'a -&amp;gt; 'b&amp;gt;,
but returns an Expression&amp;lt;Func&amp;lt;'a, obj&amp;gt;&amp;gt;. 
&lt;/p&gt;
&lt;p&gt;
Finally, writing "ToLinq" and "ToLinqObj" seemed too verbose, so I added some operators
to the LinqHelper module:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; (~@) expr = ToLinq expr
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; (~@@) expr = ToLinqObj expr
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Now we can start mapping:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;&lt;span style="COLOR: blue"&gt;&lt;font color=#0000ff size=3&gt;&lt;font color=#0000ff size=3&gt; 
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; LinqHelper
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; EmployeeMap() = &lt;span style="COLOR: blue"&gt;inherit&lt;/span&gt; ClassMap&amp;lt;Employee&amp;gt;() &lt;span style="COLOR: blue"&gt;do&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Not.LazyLoad()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Id ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.Id
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Map ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.FirstName
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Map ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.LastName
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.References ~@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.Store
@&amp;gt;).LazyLoad() |&amp;gt; ignore
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
We start off by disabling LazyLoad because most of the properties are not virtual,
and NHibernate will fail to validate the mapping. Instead, we explicitly LazyLoad
things, like the Store reference. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Mappings: Modifying the Expressions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Unfortunately, things didn't stay so simple. The Fluent NHibernate methods "HasMany"
and "HasManyToMany", for instance, don't work with F#'s type inference. This is because
they have several overloads, a couple of which take expressions. If we try this in
the StoreMap:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; base&lt;/span&gt;.HasMany ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.Staff
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
We get an error because F# doesn't know what x is. (error FS0055: This lookup uses
a deprecated feature, where a class type is inferred from the use of a class field
label. Consider using a type annotation to make it clear which class the field comes
from.) Using ~@ to keep it strongly typed fails as well; it can't figure out the overload.
This is nothing surprising -- overloading is the enemy of type inference.
&lt;/p&gt;
&lt;p&gt;
So, what do we do? Type annotations are not what I consider fun. So instead, we'll
add non-overloaded ClassMap&amp;lt;'t&amp;gt; type extensions into the LinqHelper module:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;type&lt;/span&gt; ClassMap&amp;lt;'t&amp;gt; &lt;span style="COLOR: blue"&gt;with&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;member&lt;/span&gt; x.HasManyX
expr = (x.HasMany : Expression&amp;lt;Func&amp;lt;'t, seq&amp;lt;_&amp;gt;&amp;gt;&amp;gt; &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; _)
(ToLinq expr)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;member&lt;/span&gt; x.HasManyToManyX
expr = (x.HasManyToMany : Expression&amp;lt;Func&amp;lt;'t, seq&amp;lt;_&amp;gt;&amp;gt;&amp;gt; &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; _)
(ToLinq expr)
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
By providing these extensions that are explicit once, we enable type inference for
the rest of the time. We can now finish the StoreMap:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;type&lt;/span&gt; StoreMap() = &lt;span style="COLOR: blue"&gt;inherit&lt;/span&gt; ClassMap&amp;lt;Store&amp;gt;() &lt;span style="COLOR: blue"&gt;do&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Not.LazyLoad()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Id ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.Id
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.Map ~@@ &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; x.Name
@&amp;gt; |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.HasManyX &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="COLOR: blue"&gt;upcast&lt;/span&gt; x.Staff
@&amp;gt;)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .LazyLoad()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Inverse().Cascade.All() |&amp;gt; ignore
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="COLOR: blue"&gt;base&lt;/span&gt;.HasManyToManyX &amp;lt;@ &lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="COLOR: blue"&gt;upcast&lt;/span&gt; x.Products
@&amp;gt;)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Cascade.All()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .WithTableName(&lt;span style="COLOR: maroon"&gt;"StoreProduct"&lt;/span&gt;)
|&amp;gt; ignore
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
The only type "annotation" we need here&amp;nbsp;is the upcast for the HasManyX. This
is because F# forces you to be explicit. Accessing "Staff" in the first quotation
means type IList&amp;lt;Employee&amp;gt;, not seq&amp;lt;Employee&amp;gt;. The upcast will sort this
out for us.
&lt;/p&gt;
&lt;p&gt;
This compiles fine. But remember how I said using expressions was a "somewhat strongly
typed" way of doing things? The expression trees are interpreted at runtime, which
allows failures a more complex type system might prevent. This is one of the times.
If we try to execute it as-is, we get this exception:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;---&amp;gt; FluentNHibernate.Cfg.FluentConfigurationException:
An invalid or incomplete configuration was used while creating a SessionFactory. Check
PotentialReasons collection, and InnerException for more detail.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;---&amp;gt; System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---&amp;gt; System.ArgumentException:
Not a member access Parameter name: member&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Ouch. Fluent NHibernate does not appreciate our expression trees. Why? Using FSI,
we can inspect what we're actually converting the F# quotations to:
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;type SomeClass() = member x.Stuff = [|1;2;3|];;&lt;br&gt;
let myExpr : Expr&amp;lt;SomeClass -&amp;gt; seq&amp;lt;int&amp;gt;&amp;gt; = &amp;lt;@ fun x -&amp;gt; upcast
x.Stuff @&amp;gt;;;&lt;br&gt;
let myLinq = LinqHelper.ToLinq myExpr;;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;gt; myLinq;;&lt;br&gt;
val it : Linq.Expressions.Expression&amp;lt;Func&amp;lt;SomeClass,seq&amp;lt;int&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;
= x =&amp;gt; (x.Stuff As IEnumerable`1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {Body = (x.Stuff As IEnumerable`1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NodeType = Lambda;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Parameters = seq [x];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type = System.Func`2[FSI_0022+SomeClass,System.Collections.Generic.IEnumerable`1[System.Int32]];}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;gt; myLinq.Body;;&lt;br&gt;
val it : Linq.Expressions.Expression&lt;br&gt;
= (x.Stuff As IEnumerable`1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {IsLifted = false;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IsLiftedToNull = false;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method = null;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NodeType = TypeAs;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Operand = x.Stuff;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type = System.Collections.Generic.IEnumerable`1[System.Int32];}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
So, what's going on? The upcast is modifying the F# quotation (as it should), and
the ToLinqExpression is sticking this in as a "TypeAs" node. Fluent NHibernate does
not seem to like this. Not. One. Bit.
&lt;/p&gt;
&lt;p&gt;
But, it IS ok if we have a Convert node in the Expression tree. I imagine this is
because C# generates such nodes in its expression trees (say, accessing an int member
in a Func&amp;lt;T, object&amp;gt; expression). So, our last hack in making F# work right
here is adding a fixup function to our LinqHelper, and using it from ToLinq:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; fixup (lexpr:LambdaExpression)
= 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; lexpr.Body.NodeType
&amp;lt;&amp;gt; ExpressionType.TypeAs &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; lexpr &lt;span style="COLOR: blue"&gt;else&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; typeAs
= lexpr.Body :?&amp;gt; UnaryExpression
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; newBody
= Expression.Convert(typeAs.Operand, typeAs.Type)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Expression.Lambda(lexpr.Type, newBody, lexpr.Parameters)
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Now it's happy with our expressions. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Finally&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The rest of the First Project is pretty straightforward in F#. For instance, creating
the Session Factory:
&lt;/p&gt;
&lt;div style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 11pt"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; createSessionFactory() = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Fluently.Configure()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Database(
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; MsSqlConfiguration.MsSql2005.ConnectionString(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; csb &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; csb.Is(&lt;span style="COLOR: maroon"&gt;"server=(local);database=fhib;Integrated
Security=true"&lt;/span&gt;) |&amp;gt; ignore))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Mappings(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; m &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m.FluentMappings.AddFromAssemblyOf&amp;lt;Mappings.EmployeeMap&amp;gt;()
|&amp;gt; ignore)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .BuildSessionFactory()
&lt;/p&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;
&lt;p&gt;
Everything seems to execute as it should. Fluent interfaces in F# generate a bit more
noise, because of all the ignores that have to be added. I'm thinking of creating
a type extension to obj to add Ignore as a method, to make it look a bit more uniform.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Future&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
It's my hope that F# has now addressed most of these issues -- 1.9.6 is 7 months old.
Expression&amp;lt;T&amp;gt; is growing in importance, outside of LINQ querying, so I cannot
see F# not being able to handle them easily and generating similar output to C#. It'd
be really neat if F# would auto-convert quotations to Expression&amp;lt;T&amp;gt; when the&amp;nbsp;expr
is a syntactic argument like it does with delegates now.&amp;nbsp;I'd also be surprised
if abstract/virtual members still lack the ability to define accessibility. 
&lt;/p&gt;
&lt;p&gt;
Code: &lt;a href="http://www.atrevido.net/blog/content/binary/fhib.zip"&gt;fhib.zip (4.76
KB)&lt;/a&gt;
&lt;br&gt;
I did not include Fluent NHibernate or any of its dependencies. The project expects
them to be in the project's lib folder, so get them &lt;a href="http://fluentnhibernate.org/downloads"&gt;here&lt;/a&gt; if
you don't have them and extract them to "lib".
&lt;/p&gt;
&lt;p&gt;
I welcome comments on this and suggestions for making the code more concise.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e66f49f0-22da-4fac-b100-f2a5b0a0ca29" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,e66f49f0-22da-4fac-b100-f2a5b0a0ca29.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=3a590b35-9327-44c6-bd61-bff9119c094f</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,3a590b35-9327-44c6-bd61-bff9119c094f.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,3a590b35-9327-44c6-bd61-bff9119c094f.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=3a590b35-9327-44c6-bd61-bff9119c094f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">On a current project, I'm using F# with
ASMX Web Services (Mono, so no WCF). I started off declaring some types like this:<br /><br /><font face="Courier New">type Account() =<br />
    [&lt;DefaultValue&gt;]<br />
    val mutable Name : string<br />
    [&lt;DefaultValue&gt;]<br />
    val mutable Data : byte array</font><br /><br />
We have to tag "default value" to tell F# it should go generate the default (Unchecked.defaultof&lt;'a&gt;)
-- in this case, null.<br /><br />
Unfortunately, the serialized XML is not what we want. Currently F# generates both
a public field ("_Name") and a get/set property ("Name") to access it. I'm not sure
why this is, but there's probably some interesting reason (the part of the spec is
10.2.7 Explicit Fields, but it doesn't mention the implementation). The workaround
is simple: tag the field with "XmlIgnore".<br /><br />
As of F# 1.9.3.7, you can choose to target an attribute at the property or the field
for a val in a class type. See: <a href="http://blogs.msdn.com/dsyme/archive/2007/11/30/full-release-notes-for-f-1-9-3-7.aspx">http://blogs.msdn.com/dsyme/archive/2007/11/30/full-release-notes-for-f-1-9-3-7.aspx</a> (I
didn't find this part in the spec.)<br /><br />
So, the revised declaration for the XmlSerialization-friendly type is:<br /><br /><font face="Courier New">type Account() =<br />
    [&lt;DefaultValue; field: XmlIgnore&gt;]<br />
    val mutable Name : string<br />
    [&lt;DefaultValue; field: XmlIgnore&gt;]<br />
    val mutable Data : byte array</font><br /><br />
I found that to add a lot of noise, so I aliased them:<br /><br /><font face="Courier New">type DV = DefaultValueAttribute<br />
type XI = XmlIgnoreAttribute<br /><br />
type Account() =<br />
    [&lt;DV; field: XI&gt;] val mutable Name : string<br />
    [&lt;DV; field: XI&gt;] val mutable Data : byte array</font><br /><br />
Now it works just fine and creates the XML we'd expect. 
<br /><br />
One other thing might bite you: null. F# rightfully eschews null. If you have a function
that returns one of these types, you'll find it won't let you return null:<br /><br /><font face="Courier New">&gt; let test() : Account = null;;<br />
  let test() : Account = null;;<br />
  -----------------------^^^^^<br />
stdin(6,24): error FS0043: The type 'Account' does not have 'null' as a proper value.<br /></font><br />
This is generally good, but unfortunately, interop with the unenlightened world is
sometimes necessary. The right way to do this is:<br /><br /><font face="Courier New">let test() : Account = Unchecked.defaultof&lt;Account&gt;;;</font><br /><br />
That's a bit too verbose for my liking, since I have to go annotate the type. A simple
function will get around that:<br /><br /><font face="Courier New">&gt; let inline getnull() = Unchecked.defaultof&lt;_&gt;;;<br />
val inline getnull : unit -&gt; 'a<br /><br />
&gt; let test() : Account = getnull();;<br />
val test : unit -&gt; Account<br /></font><br />
Yes, I know it's not null, but default, but I think the purpose is clear enough.<br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=3a590b35-9327-44c6-bd61-bff9119c094f" /></body>
      <title>XmlSerialization for F# Types</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,3a590b35-9327-44c6-bd61-bff9119c094f.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/11/25/XmlSerialization+For+F+Types.aspx</link>
      <pubDate>Tue, 25 Nov 2008 02:16:33 GMT</pubDate>
      <description>On a current project, I'm using F# with ASMX Web Services (Mono, so no WCF). I started off declaring some types like this:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;type Account() =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DefaultValue&amp;gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; val mutable Name : string&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DefaultValue&amp;gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; val mutable Data : byte array&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
We have to tag "default value" to tell F# it should go generate the default (Unchecked.defaultof&amp;lt;'a&amp;gt;)
-- in this case, null.&lt;br&gt;
&lt;br&gt;
Unfortunately, the serialized XML is not what we want. Currently F# generates both
a public field ("_Name") and a get/set property ("Name") to access it. I'm not sure
why this is, but there's probably some interesting reason (the part of the spec is
10.2.7 Explicit Fields, but it doesn't mention the implementation). The workaround
is simple: tag the field with "XmlIgnore".&lt;br&gt;
&lt;br&gt;
As of F# 1.9.3.7, you can choose to target an attribute at the property or the field
for a val in a class type. See: &lt;a href="http://blogs.msdn.com/dsyme/archive/2007/11/30/full-release-notes-for-f-1-9-3-7.aspx"&gt;http://blogs.msdn.com/dsyme/archive/2007/11/30/full-release-notes-for-f-1-9-3-7.aspx&lt;/a&gt; (I
didn't find this part in the spec.)&lt;br&gt;
&lt;br&gt;
So, the revised declaration for the XmlSerialization-friendly type is:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;type Account() =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DefaultValue; field: XmlIgnore&amp;gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; val mutable Name : string&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DefaultValue; field: XmlIgnore&amp;gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; val mutable Data : byte array&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
I found that to add a lot of noise, so I aliased them:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;type DV = DefaultValueAttribute&lt;br&gt;
type XI = XmlIgnoreAttribute&lt;br&gt;
&lt;br&gt;
type Account() =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV; field: XI&amp;gt;] val mutable Name : string&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&amp;lt;DV; field: XI&amp;gt;] val mutable Data : byte array&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Now it works just fine and creates the XML we'd expect. 
&lt;br&gt;
&lt;br&gt;
One other thing might bite you: null. F# rightfully eschews null. If you have a function
that returns one of these types, you'll find it won't let you return null:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let test() : Account = null;;&lt;br&gt;
&amp;nbsp; let test() : Account = null;;&lt;br&gt;
&amp;nbsp; -----------------------^^^^^&lt;br&gt;
stdin(6,24): error FS0043: The type 'Account' does not have 'null' as a proper value.&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
This is generally good, but unfortunately, interop with the unenlightened world is
sometimes necessary. The right way to do this is:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;let test() : Account = Unchecked.defaultof&amp;lt;Account&amp;gt;;;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
That's a bit too verbose for my liking, since I have to go annotate the type. A simple
function will get around that:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let inline getnull() = Unchecked.defaultof&amp;lt;_&amp;gt;;;&lt;br&gt;
val inline getnull : unit -&amp;gt; 'a&lt;br&gt;
&lt;br&gt;
&amp;gt; let test() : Account = getnull();;&lt;br&gt;
val test : unit -&amp;gt; Account&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
Yes, I know it's not null, but default, but I think the purpose is clear enough.&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=3a590b35-9327-44c6-bd61-bff9119c094f" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,3a590b35-9327-44c6-bd61-bff9119c094f.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=0dfb63a3-b66c-4317-9e6c-affe59c86041</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,0dfb63a3-b66c-4317-9e6c-affe59c86041.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,0dfb63a3-b66c-4317-9e6c-affe59c86041.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0dfb63a3-b66c-4317-9e6c-affe59c86041</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">Several times I've had to explain a few
basic F# things to new users. I ran into some of these too when I first looked at
F#. If you've been looking at F#, but some things still don't click, I hope this will
help break the chains of C/imperative languages. I'm assuming you know a bit about
F#, perhaps from the <a href="http://research.microsoft.com/fsharp/manual/quicktour.aspx">Quick
Tour</a> or the F# Tutorial file that comes with the Visual Studio integration.<br /><br /><b>Functions always take and receive exactly one argument.</b><br />
Despite anything you see, in F#, there's only one argument, and only one return value.
Let's look at how this plays out (I suggest firing up F# interactive and playing around):<br /><br /><font face="Courier New">&gt; let inc x = x + 1;;<br />
val inc : int -&gt; int<br /><br />
&gt; let add x y = x + y;;<br />
val add : int -&gt; int -&gt; int<br /></font><br />
[I like to read -&gt; as "to", because it's short and sweet.] So, for the case of
"inc x", we see the signature is quite simple and expected "int to int". But for add?
We see "int to int to int". What this actually means is "a function that takes an
integer, and returns a function that takes an integer and returns an integer". 
<br /><br />
The same signature in C# would look like this: <font face="Courier New">Func&lt;int,
&lt;Func&lt;int,int&gt;&gt;&gt;</font> or "<font face="Courier New">Func&lt;int,int&gt;
Add(int x)</font>". [Some day, think how odd it is to have two ways of representing
the function.] So when we call the F# function, we're really passing in the first
argument, getting a new function, then applying the second argument. Something like
this:<br /><br />
1. add x y<br />
2. (add x) y<br />
3. closure1 y<br />
4. final-result<br /><br />
"closure1" is what we get from the "partial" application of add. I'll touch on this
in a minute.<br /><br />
So how do we _really_ pass in two arguments at once? We put two arguments into one
value, a tuple. We write it like this:<br /><font face="Courier New"><br />
&gt; let add (x, y) = x + y;;<br />
val add : int * int -&gt; int<br /></font><br />
Notice the new type signature: "int by int to int" or "int int tuple to int". In C#,
this would be:<br /><br />
int add(Tuple&lt;int,int&gt; arguments) { return Tuple.A + Tuple.B; }<br /><br />
But F# nicely provides support for tuples [via pattern matching], so we can write
them much more naturally. This syntax also works on the way out:<br /><font face="Courier New"><br />
&gt; let pow23 x = x*x, x*x*x;;<br />
val pow23 : int -&gt; int * int<br />
// "int to int int tuple"<br /><br />
&gt; let square, cube = pow23 7;;<br />
val square : int<br />
val cube : int<br /><br />
&gt; square, cube;;<br />
val it : int * int = (49, 343)</font><br /><br /><b>There's never no value</b><br />
Another common typo/misunderstanding is when creating a function with no arguments:<br /><br /><font face="Courier New">&gt; let sayHi = printfn "Hi";;<br />
val sayHi : unit<br /><br />
Hi<br />
&gt; sayHi;;<br />
val it : unit = ()<br /></font><br />
Oh, what happened here? Remember how _everything_ takes and returns exactly one value?
The same is true of functions that "don't return a value", such as printf. Instead
of "not returning a value" like C's <font face="Courier New">void</font>, functions
return a special type called <font face="Courier New">unit</font> with one value, <font face="Courier New">()</font>. 
<br /><br />
Armed with this, let's see what the <font face="Courier New">sayHi </font>definition
actually says. It says "let a value called sayHi equal to the result of printfn".
Well, since the result of <font face="Courier New">printfn </font>is <font face="Courier New">unit</font>, <font face="Courier New">sayHi </font>becomes <font face="Courier New">unit</font>.
The execution happens immediately, and subsequent uses of <font face="Courier New">sayHi </font>just
get the value, <font face="Courier New">unit</font>. 
<br /><br />
To actually do what we want, we need to take an argument. Then F# knows we're a function
value:<br /><br /><font face="Courier New">&gt; let sayHi() = printfn "Hi";;<br />
val sayHi : unit -&gt; unit<br /><br />
&gt; sayHi;;<br />
val it : (unit -&gt; unit) = &lt;fun:clo@0&gt;<br /><br />
&gt; sayHi();;<br />
Hi<br />
val it : unit = ()</font><br /><br />
By explicitly taking a <font face="Courier New">unit </font>parameter, <font face="Courier New">sayHi</font> becomes
a function (type unit to unit). Notice that if we just write "sayHi", we're just going
to get the _value_ of it (a function), not apply (execute) the function.<br /><br /><b>Partial application</b><br />
OK, so a side effect of this system of "take on param and return a function that takes
the next", is that we can compose with just parts of a function. For instance, to
write an increment function, we can do this:<br /><br /><font face="Courier New">&gt; let add x y = x + y;;<br />
val add : int -&gt; int -&gt; int<br /><br />
&gt; let inc x = add x 1;;<br />
val inc : int -&gt; int</font><br /><br />
So far, nothing interesting. However, we can also write this more effectively:<br /><font face="Courier New"><br />
&gt; let inc = add 1;;<br />
val inc : (int -&gt; int)</font><br /><br />
Aha! What's happening here is what happens "secretly" each time we call add with 2
parameters. We're just using the intermediate result, the closure of "add 1", and
assigning that function value to inc. In C#, it'd be something like this:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> Func&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>&gt;
Add(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span> x)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> y
=&gt; x <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span> y;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> Func&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>&gt;
Inc <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> Add(1); <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
and "full application" of add looks like this: Add(1)(2)</span></span></pre>[<i>BTW</i>,
this isn't really currying in F#. Currying is taking a method of type "a' * 'b -&gt;
'c" and turning it into "'a -&gt; 'b -&gt; 'c". Since F# methods are "automatically
curried", there's no need for a "curry" step (well, except perhaps when using .NET
methods, which are always "tupled", but that's another story).]<br /><br />
[<i>Side note</i>: a function like "add" is superfluous, because in F#, operators
are functions:<br /><font face="Courier New">&gt; (+);;<br />
val it : (int -&gt; int -&gt; int) = &lt;fun:clo@18&gt;<br />
&gt; let inc = (+) 1;;<br />
val inc : (int -&gt; int)</font>]<br /><b><br />
The pipeline</b><br /><br />
F# appears to have all this complicated syntax, with<font face="Courier New"> |&gt;
&lt;| &gt;&gt; &lt;&lt;</font> and so on. But, these operators are defined in F# code,
and follow some basic rules. They aren't magic or have any special compiler support.<br /><br />
The most important function operator is <font face="Courier New">|&gt;</font>. A quick
search of the F# source shows:<br /><font face="Courier New">C:\Program Files\FSharp-1.9.6.2\source\fsharp\FSharp.Core\prim-types.fs(2062):        
<br />
        let inline (|&gt;) x f = f x</font><br /><br />
(Operators are surrounded in parentheses to define them and to use them as functions
with prefix notation.)<br />
The type signature is:<br /><font face="Courier New">&gt; (|&gt;);;<br />
val it : ('a -&gt; ('a -&gt; 'b) -&gt; 'b) = &lt;fun:clo@23&gt;<br /></font><br />
This is "alpha to a function alpha to beta to beta". That probably didn't help. Perhaps
looking at the type of function application will help:<br /><br /><font face="Courier New">&gt; let apply f x = f x;;<br />
val apply : ('a -&gt; 'b) -&gt; 'a -&gt; 'b</font><br /><br />
This demonstrates that a function application is really just taking a function "alpha
to beta", giving it an alpha, and getting a beta. [I'm open to suggestions on better
ways to pronounce type arguments.]<br /><br />
So, glance back up at the pipeline operator. We can see it's really just function
application _in reverse_. What is the use of such a construct? If you've used C# 3.0's
LINQ extension methods or a Unix shell, you probably already know. By reversing the
function application, we can write things in a much more natural order. To modify
something from the F# Quick Tour:<br /><br /><font face="Courier New">&gt; let filterTypes name =<br />
-   System.AppDomain.CurrentDomain.GetAssemblies()<br />
-   |&gt; Seq.map (fun a -&gt; a.GetTypes()) |&gt; Seq.concat<br />
-   |&gt; Seq.map (fun a -&gt; a.Name)<br />
-   |&gt; Seq.filter (fun s-&gt; s.Contains name);;<br /><br />
val filterTypes : string -&gt; seq&lt;string&gt;<br /><br />
&gt; filterTypes "Coll";;<br />
val it : seq&lt;string&gt;<br />
= seq ["ICollection"; "EvidenceCollection"; "GCCollectionMode"; "CollectionBase";
...]<br />
&gt;</font><br /><br />
Now, this is a bit embarrassing, but this took me a long time to get. I stared at
this and re-read the F# manual for probably an hour. How could something so simple
do such "complex" stuff?? Once I finally got used to the idea of functions being normal
values, and the whole "one arg one value" bit, it snapped together. The other operators
(<font face="Courier New">&lt;|, &gt;&gt;, &lt;&lt;</font>) are pretty easy to follow
once the basics are understood (going through prim-types.fs is a great experience).<br /><br /><b>What else?<br /></b>I hope this all helps fit some pieces together. Feel free to use the MSN thing
on the side of my site to ask questions or give me suggestions. Thanks!<br /><br />
Edit: Also check out <a href="http://lorgonblog.spaces.live.com/blog/cns%21701679AD17B6D310%21169.entry">F#
function types: fun with tuples and currying</a> (From an F# team member's blog.)<br /><br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=0dfb63a3-b66c-4317-9e6c-affe59c86041" /></body>
      <title>Common Conceptual Issues with F# </title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,0dfb63a3-b66c-4317-9e6c-affe59c86041.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/11/03/Common+Conceptual+Issues+With+F.aspx</link>
      <pubDate>Mon, 03 Nov 2008 21:07:15 GMT</pubDate>
      <description>Several times I've had to explain a few basic F# things to new users. I ran into some of these too when I first looked at F#. If you've been looking at F#, but some things still don't click, I hope this will help break the chains of C/imperative languages. I'm assuming you know a bit about F#, perhaps from the &lt;a href="http://research.microsoft.com/fsharp/manual/quicktour.aspx"&gt;Quick
Tour&lt;/a&gt; or the F# Tutorial file that comes with the Visual Studio integration.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Functions always take and receive exactly one argument.&lt;/b&gt;
&lt;br&gt;
Despite anything you see, in F#, there's only one argument, and only one return value.
Let's look at how this plays out (I suggest firing up F# interactive and playing around):&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let inc x = x + 1;;&lt;br&gt;
val inc : int -&amp;gt; int&lt;br&gt;
&lt;br&gt;
&amp;gt; let add x y = x + y;;&lt;br&gt;
val add : int -&amp;gt; int -&amp;gt; int&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
[I like to read -&amp;gt; as "to", because it's short and sweet.] So, for the case of
"inc x", we see the signature is quite simple and expected "int to int". But for add?
We see "int to int to int". What this actually means is "a function that takes an
integer, and returns a function that takes an integer and returns an integer". 
&lt;br&gt;
&lt;br&gt;
The same signature in C# would look like this: &lt;font face="Courier New"&gt;Func&amp;lt;int,
&amp;lt;Func&amp;lt;int,int&amp;gt;&amp;gt;&amp;gt;&lt;/font&gt; or "&lt;font face="Courier New"&gt;Func&amp;lt;int,int&amp;gt;
Add(int x)&lt;/font&gt;". [Some day, think how odd it is to have two ways of representing
the function.] So when we call the F# function, we're really passing in the first
argument, getting a new function, then applying the second argument. Something like
this:&lt;br&gt;
&lt;br&gt;
1. add x y&lt;br&gt;
2. (add x) y&lt;br&gt;
3. closure1 y&lt;br&gt;
4. final-result&lt;br&gt;
&lt;br&gt;
"closure1" is what we get from the "partial" application of add. I'll touch on this
in a minute.&lt;br&gt;
&lt;br&gt;
So how do we _really_ pass in two arguments at once? We put two arguments into one
value, a tuple. We write it like this:&lt;br&gt;
&lt;font face="Courier New"&gt;
&lt;br&gt;
&amp;gt; let add (x, y) = x + y;;&lt;br&gt;
val add : int * int -&amp;gt; int&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
Notice the new type signature: "int by int to int" or "int int tuple to int". In C#,
this would be:&lt;br&gt;
&lt;br&gt;
int add(Tuple&amp;lt;int,int&amp;gt; arguments) { return Tuple.A + Tuple.B; }&lt;br&gt;
&lt;br&gt;
But F# nicely provides support for tuples [via pattern matching], so we can write
them much more naturally. This syntax also works on the way out:&lt;br&gt;
&lt;font face="Courier New"&gt;
&lt;br&gt;
&amp;gt; let pow23 x = x*x, x*x*x;;&lt;br&gt;
val pow23 : int -&amp;gt; int * int&lt;br&gt;
// "int to int int tuple"&lt;br&gt;
&lt;br&gt;
&amp;gt; let square, cube = pow23 7;;&lt;br&gt;
val square : int&lt;br&gt;
val cube : int&lt;br&gt;
&lt;br&gt;
&amp;gt; square, cube;;&lt;br&gt;
val it : int * int = (49, 343)&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;There's never no value&lt;/b&gt;
&lt;br&gt;
Another common typo/misunderstanding is when creating a function with no arguments:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let sayHi = printfn "Hi";;&lt;br&gt;
val sayHi : unit&lt;br&gt;
&lt;br&gt;
Hi&lt;br&gt;
&amp;gt; sayHi;;&lt;br&gt;
val it : unit = ()&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
Oh, what happened here? Remember how _everything_ takes and returns exactly one value?
The same is true of functions that "don't return a value", such as printf. Instead
of "not returning a value" like C's &lt;font face="Courier New"&gt;void&lt;/font&gt;, functions
return a special type called &lt;font face="Courier New"&gt;unit&lt;/font&gt; with one value, &lt;font face="Courier New"&gt;()&lt;/font&gt;. 
&lt;br&gt;
&lt;br&gt;
Armed with this, let's see what the &lt;font face="Courier New"&gt;sayHi &lt;/font&gt;definition
actually says. It says "let a value called sayHi equal to the result of printfn".
Well, since the result of &lt;font face="Courier New"&gt;printfn &lt;/font&gt;is &lt;font face="Courier New"&gt;unit&lt;/font&gt;, &lt;font face="Courier New"&gt;sayHi &lt;/font&gt;becomes &lt;font face="Courier New"&gt;unit&lt;/font&gt;.
The execution happens immediately, and subsequent uses of &lt;font face="Courier New"&gt;sayHi &lt;/font&gt;just
get the value, &lt;font face="Courier New"&gt;unit&lt;/font&gt;. 
&lt;br&gt;
&lt;br&gt;
To actually do what we want, we need to take an argument. Then F# knows we're a function
value:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let sayHi() = printfn "Hi";;&lt;br&gt;
val sayHi : unit -&amp;gt; unit&lt;br&gt;
&lt;br&gt;
&amp;gt; sayHi;;&lt;br&gt;
val it : (unit -&amp;gt; unit) = &amp;lt;fun:clo@0&amp;gt;&lt;br&gt;
&lt;br&gt;
&amp;gt; sayHi();;&lt;br&gt;
Hi&lt;br&gt;
val it : unit = ()&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
By explicitly taking a &lt;font face="Courier New"&gt;unit &lt;/font&gt;parameter, &lt;font face="Courier New"&gt;sayHi&lt;/font&gt; becomes
a function (type unit to unit). Notice that if we just write "sayHi", we're just going
to get the _value_ of it (a function), not apply (execute) the function.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Partial application&lt;/b&gt;
&lt;br&gt;
OK, so a side effect of this system of "take on param and return a function that takes
the next", is that we can compose with just parts of a function. For instance, to
write an increment function, we can do this:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let add x y = x + y;;&lt;br&gt;
val add : int -&amp;gt; int -&amp;gt; int&lt;br&gt;
&lt;br&gt;
&amp;gt; let inc x = add x 1;;&lt;br&gt;
val inc : int -&amp;gt; int&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
So far, nothing interesting. However, we can also write this more effectively:&lt;br&gt;
&lt;font face="Courier New"&gt;
&lt;br&gt;
&amp;gt; let inc = add 1;;&lt;br&gt;
val inc : (int -&amp;gt; int)&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Aha! What's happening here is what happens "secretly" each time we call add with 2
parameters. We're just using the intermediate result, the closure of "add 1", and
assigning that function value to inc. In C#, it'd be something like this:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; Func&amp;lt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;&amp;gt;
Add(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt; x)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; y
=&amp;gt; x &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;+&lt;/span&gt; y;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; Func&amp;lt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;int&lt;/span&gt;&amp;gt;
Inc &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; Add(1); &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
and "full application" of add looks like this: Add(1)(2)&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;[&lt;i&gt;BTW&lt;/i&gt;,
this isn't really currying in F#. Currying is taking a method of type "a' * 'b -&amp;gt;
'c" and turning it into "'a -&amp;gt; 'b -&amp;gt; 'c". Since F# methods are "automatically
curried", there's no need for a "curry" step (well, except perhaps when using .NET
methods, which are always "tupled", but that's another story).]&lt;br&gt;
&lt;br&gt;
[&lt;i&gt;Side note&lt;/i&gt;: a function like "add" is superfluous, because in F#, operators
are functions:&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; (+);;&lt;br&gt;
val it : (int -&amp;gt; int -&amp;gt; int) = &amp;lt;fun:clo@18&amp;gt;&lt;br&gt;
&amp;gt; let inc = (+) 1;;&lt;br&gt;
val inc : (int -&amp;gt; int)&lt;/font&gt;]&lt;br&gt;
&lt;b&gt;
&lt;br&gt;
The pipeline&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
F# appears to have all this complicated syntax, with&lt;font face="Courier New"&gt; |&amp;gt;
&amp;lt;| &amp;gt;&amp;gt; &amp;lt;&amp;lt;&lt;/font&gt; and so on. But, these operators are defined in F# code,
and follow some basic rules. They aren't magic or have any special compiler support.&lt;br&gt;
&lt;br&gt;
The most important function operator is &lt;font face="Courier New"&gt;|&amp;gt;&lt;/font&gt;. A quick
search of the F# source shows:&lt;br&gt;
&lt;font face="Courier New"&gt;C:\Program Files\FSharp-1.9.6.2\source\fsharp\FSharp.Core\prim-types.fs(2062):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; let inline (|&amp;gt;) x f = f x&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
(Operators are surrounded in parentheses to define them and to use them as functions
with prefix notation.)&lt;br&gt;
The type signature is:&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; (|&amp;gt;);;&lt;br&gt;
val it : ('a -&amp;gt; ('a -&amp;gt; 'b) -&amp;gt; 'b) = &amp;lt;fun:clo@23&amp;gt;&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
This is "alpha to a function alpha to beta to beta". That probably didn't help. Perhaps
looking at the type of function application will help:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let apply f x = f x;;&lt;br&gt;
val apply : ('a -&amp;gt; 'b) -&amp;gt; 'a -&amp;gt; 'b&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
This demonstrates that a function application is really just taking a function "alpha
to beta", giving it an alpha, and getting a beta. [I'm open to suggestions on better
ways to pronounce type arguments.]&lt;br&gt;
&lt;br&gt;
So, glance back up at the pipeline operator. We can see it's really just function
application _in reverse_. What is the use of such a construct? If you've used C# 3.0's
LINQ extension methods or a Unix shell, you probably already know. By reversing the
function application, we can write things in a much more natural order. To modify
something from the F# Quick Tour:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;&amp;gt; let filterTypes name =&lt;br&gt;
-&amp;nbsp;&amp;nbsp; System.AppDomain.CurrentDomain.GetAssemblies()&lt;br&gt;
-&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.map (fun a -&amp;gt; a.GetTypes()) |&amp;gt; Seq.concat&lt;br&gt;
-&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.map (fun a -&amp;gt; a.Name)&lt;br&gt;
-&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.filter (fun s-&amp;gt; s.Contains name);;&lt;br&gt;
&lt;br&gt;
val filterTypes : string -&amp;gt; seq&amp;lt;string&amp;gt;&lt;br&gt;
&lt;br&gt;
&amp;gt; filterTypes "Coll";;&lt;br&gt;
val it : seq&amp;lt;string&amp;gt;&lt;br&gt;
= seq ["ICollection"; "EvidenceCollection"; "GCCollectionMode"; "CollectionBase";
...]&lt;br&gt;
&amp;gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Now, this is a bit embarrassing, but this took me a long time to get. I stared at
this and re-read the F# manual for probably an hour. How could something so simple
do such "complex" stuff?? Once I finally got used to the idea of functions being normal
values, and the whole "one arg one value" bit, it snapped together. The other operators
(&lt;font face="Courier New"&gt;&amp;lt;|, &amp;gt;&amp;gt;, &amp;lt;&amp;lt;&lt;/font&gt;) are pretty easy to follow
once the basics are understood (going through prim-types.fs is a great experience).&lt;br&gt;
&lt;br&gt;
&lt;b&gt;What else?&lt;br&gt;
&lt;/b&gt;I hope this all helps fit some pieces together. Feel free to use the MSN thing
on the side of my site to ask questions or give me suggestions. Thanks!&lt;br&gt;
&lt;br&gt;
Edit: Also check out &lt;a href="http://lorgonblog.spaces.live.com/blog/cns%21701679AD17B6D310%21169.entry"&gt;F#
function types: fun with tuples and currying&lt;/a&gt; (From an F# team member's blog.)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=0dfb63a3-b66c-4317-9e6c-affe59c86041" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,0dfb63a3-b66c-4317-9e6c-affe59c86041.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=1533c44a-63b4-44b4-a055-bd82344dd464</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,1533c44a-63b4-44b4-a055-bd82344dd464.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,1533c44a-63b4-44b4-a055-bd82344dd464.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1533c44a-63b4-44b4-a055-bd82344dd464</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">Compiling for Silverlight 2 is a bit of
a pain still. It's even worse with F#, because the Silverlight project system cannot
tell when you're building your F# components correctly (using the right flags). So
you get this error:<br /><br /><font face="Courier New">---------------------------<br />
Microsoft Visual Studio<br />
---------------------------<br />
You can only add project references to other Silverlight projects in the solution.<br />
---------------------------<br />
OK   
<br />
---------------------------<br /></font><br />
This happens even if you set up the F# compiler options correctly by adding:<br /><font face="Courier New">--standalone --noframework --cliroot "C:\program Files\Microsoft
Silverlight\2.0.31005.0"</font><br /><br />
It still happens if you reference the DLL directly (FSLib1\bin\debug), you get the
same error! Apparently VS or Silverlight projects go and try to find the project relating
to the DLLs when you add a reference. So, the solution is easy: Move the DLL somewhere
else. Then you can add a file reference, and it will work just fine.<br /><br />
One more problem. I get an FSC error when I turn optimize code on:<br /><font face="Courier New">C:\test\SilverlightApplication1\FSC(0,0): error FS0193: internal
error: the module/namespace 'System' from compilation unit 'mscorlib' did not contain
the namespace, module or type 'MarshalByRefObject'</font><br /><br />
If I leave optimize code off, then it seems to work. Good luck.<br /><br /><a href="http://stackoverflow.com/questions/237044/how-does-silverlight-determine-an-assembly-is-silverlight">http://stackoverflow.com/questions/237044/how-does-silverlight-determine-an-assembly-is-silverlight</a><br /><br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=1533c44a-63b4-44b4-a055-bd82344dd464" /></body>
      <title>F# 1.9.6.2 and Silverlight 2</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,1533c44a-63b4-44b4-a055-bd82344dd464.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/10/28/F+1962+And+Silverlight+2.aspx</link>
      <pubDate>Tue, 28 Oct 2008 22:27:50 GMT</pubDate>
      <description>Compiling for Silverlight 2 is a bit of a pain still. It's even worse with F#, because the Silverlight project system cannot tell when you're building your F# components correctly (using the right flags). So you get this error:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;---------------------------&lt;br&gt;
Microsoft Visual Studio&lt;br&gt;
---------------------------&lt;br&gt;
You can only add project references to other Silverlight projects in the solution.&lt;br&gt;
---------------------------&lt;br&gt;
OK&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
---------------------------&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
This happens even if you set up the F# compiler options correctly by adding:&lt;br&gt;
&lt;font face="Courier New"&gt;--standalone --noframework --cliroot "C:\program Files\Microsoft
Silverlight\2.0.31005.0"&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
It still happens if you reference the DLL directly (FSLib1\bin\debug), you get the
same error! Apparently VS or Silverlight projects go and try to find the project relating
to the DLLs when you add a reference. So, the solution is easy: Move the DLL somewhere
else. Then you can add a file reference, and it will work just fine.&lt;br&gt;
&lt;br&gt;
One more problem. I get an FSC error when I turn optimize code on:&lt;br&gt;
&lt;font face="Courier New"&gt;C:\test\SilverlightApplication1\FSC(0,0): error FS0193: internal
error: the module/namespace 'System' from compilation unit 'mscorlib' did not contain
the namespace, module or type 'MarshalByRefObject'&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
If I leave optimize code off, then it seems to work. Good luck.&lt;br&gt;
&lt;br&gt;
&lt;a href="http://stackoverflow.com/questions/237044/how-does-silverlight-determine-an-assembly-is-silverlight"&gt;http://stackoverflow.com/questions/237044/how-does-silverlight-determine-an-assembly-is-silverlight&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=1533c44a-63b4-44b4-a055-bd82344dd464" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,1533c44a-63b4-44b4-a055-bd82344dd464.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=b97a9b39-f255-4021-95ea-3ecafd48b536</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,b97a9b39-f255-4021-95ea-3ecafd48b536.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,b97a9b39-f255-4021-95ea-3ecafd48b536.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=b97a9b39-f255-4021-95ea-3ecafd48b536</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">In a previous comment, someone mentioned
the OO mindset ("mold" -- quite appropriate). I don't want to go into it much, but
simply "quote for win" something from here <a href="http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html">http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html</a>.
It's a nice take on things and I got a kick out of it:<br /><br />
"<br />
  The venerable master Qc Na was walking with his student, Anton.  Hoping
to<br />
prompt the master into a discussion, Anton said "Master, I have heard that<br />
objects are a very good thing - is this true?"  Qc Na looked pityingly at<br />
his student and replied, "Foolish pupil - objects are merely a poor man's<br />
closures."<br /><br />
  Chastised, Anton took his leave from his master and returned to his cell,<br />
intent on studying closures.  He carefully read the entire "Lambda: The<br />
Ultimate..." series of papers and its cousins, and implemented a small<br />
Scheme interpreter with a closure-based object system.  He learned much, and<br />
looked forward to informing his master of his progress.<br /><br />
  On his next walk with Qc Na, Anton attempted to impress his master by<br />
saying "Master, I have diligently studied the matter, and now understand<br />
that objects are truly a poor man's closures."  Qc Na responded by hitting<br />
Anton with his stick, saying "When will you learn? Closures are a poor man's<br />
object."  At that moment, Anton became enlightened.<br /><br />
"<br /><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=b97a9b39-f255-4021-95ea-3ecafd48b536" /></body>
      <title>Objects versus Closures - a koan</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,b97a9b39-f255-4021-95ea-3ecafd48b536.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/09/19/Objects+Versus+Closures+A+Koan.aspx</link>
      <pubDate>Fri, 19 Sep 2008 18:00:50 GMT</pubDate>
      <description>In a previous comment, someone mentioned the OO mindset ("mold" -- quite appropriate). I don't want to go into it much, but simply "quote for win" something from here &lt;a href="http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html"&gt;http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html&lt;/a&gt;.
It's a nice take on things and I got a kick out of it:&lt;br&gt;
&lt;br&gt;
"&lt;br&gt;
&amp;nbsp; The venerable master Qc Na was walking with his student, Anton.&amp;nbsp; Hoping
to&lt;br&gt;
prompt the master into a discussion, Anton said "Master, I have heard that&lt;br&gt;
objects are a very good thing - is this true?"&amp;nbsp; Qc Na looked pityingly at&lt;br&gt;
his student and replied, "Foolish pupil - objects are merely a poor man's&lt;br&gt;
closures."&lt;br&gt;
&lt;br&gt;
&amp;nbsp; Chastised, Anton took his leave from his master and returned to his cell,&lt;br&gt;
intent on studying closures.&amp;nbsp; He carefully read the entire "Lambda: The&lt;br&gt;
Ultimate..." series of papers and its cousins, and implemented a small&lt;br&gt;
Scheme interpreter with a closure-based object system.&amp;nbsp; He learned much, and&lt;br&gt;
looked forward to informing his master of his progress.&lt;br&gt;
&lt;br&gt;
&amp;nbsp; On his next walk with Qc Na, Anton attempted to impress his master by&lt;br&gt;
saying "Master, I have diligently studied the matter, and now understand&lt;br&gt;
that objects are truly a poor man's closures."&amp;nbsp; Qc Na responded by hitting&lt;br&gt;
Anton with his stick, saying "When will you learn? Closures are a poor man's&lt;br&gt;
object."&amp;nbsp; At that moment, Anton became enlightened.&lt;br&gt;
&lt;br&gt;
"&lt;br&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=b97a9b39-f255-4021-95ea-3ecafd48b536" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,b97a9b39-f255-4021-95ea-3ecafd48b536.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
      <category>Humour</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=1a161d7d-0d6e-456d-804f-f621766f10af</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,1a161d7d-0d6e-456d-804f-f621766f10af.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,1a161d7d-0d6e-456d-804f-f621766f10af.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1a161d7d-0d6e-456d-804f-f621766f10af</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">Something I've heard often is that "F#
is too complex/functional programming is too hard". This is something that sorta came
up in the comments here: <a href="http://www.atrevido.net/blog/2008/09/16/Why+NOT+F.aspx">http://www.atrevido.net/blog/2008/09/16/Why+NOT+F.aspx</a>. 
<br /><br />
Why is this irrelevant? You only learn a language once. You pay the learning curve
cost one time; after that, you have the techniques and power at your disposal. However,
you pay the cost of code every time you read or write it. Since I'm going to read
and write a lot more code than number of languages I learn, I'd much, much, prefer
to pay this overhead once, up front, rather than in my code each time.<br /><br />
Of course, it's not necessarily this simple. It's possible to design a powerful language
that renders code even more difficult. Regular expressions are perhaps a nice example
of this; they're often called "write only" code. Every time I do a non-trivial regexp,
I'm always going back to the reference. Another example are C macros -- text-based,
they quickly let you get into trouble. The design of C# runs away from this and tries
to make it very difficult to write code that is "hard" to figure out -- if there's
any edge case where a feature might be confusing or not work, C# tends to not allow
it at all. 
<br /><br />
F# design is different. F# doesn't try to shelter users - it gives you tools and lets
you decide how to use them. It makes the assumption that if you're writing a program,
*you have some clue of what you're doing*. F#'s tools are still safe (compared to
say, C), but sure, you can go create a mess if you'd like. 
<br /><br />
Poor code quality is *not* something that should be fixed solely via technical measures.
I liken it to using web filters to make sure "employees aren't goofing off on the
Internet" -- this is a policy/management issue and should be solved via administrative
means. If one of my devs is spending all day on 4chan but gets work done and adds
value, what do I care? Similarly, if the code quality coming out is acceptable and
the solutions work correctly, I don't care if it used macros, custom operators, "difficult"
code, etc. The process to make sure code quality is high (code reviews) will take
care of anyone abusing language features in stupid ways.<br /><br />
But in truth, F# isn't actually much more complex to use. To the beginner, what seems
to be "unnecessary terseness" and a lot of complicated syntax is actually a very basic
system in action. Many of the "built-in" F# features such as the |&gt; pipeline operator
are defined right in the language itself. There's no magic going on -- you can create
your own functionality in exactly the same way. Once you understand the basic rules,
you'll see that most everything else follows them.<br /><br />
But at any rate, why is "easy to learn" a benefit? Sure, it's handy to promote a language
if people can pick it up easily, but it's not indicitive of long-term power. True,
if you have a "web developer" who's going to add a few server-side scripts, it's nice
that he doesn't have to learn much. But if you're developing an application of any
substance, I fail to see how these help, given the negative effects of having a "simple"
language.<br /><br />
P.S. On my site I'm not trying to infer that F# will take over the world or that C#
will go away. I've met too many "professional developers" to realise that anything
that requires thinking isn't going to achieve stellar adoption. I'm simply pointing
out that the reasons come down to apathy and intelligence (with respect to the learning
curve; there could be other reasons as well), regardless of how politically correct
one phrases it.<br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=1a161d7d-0d6e-456d-804f-f621766f10af" /></body>
      <title>The learning curve is irrelevant</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,1a161d7d-0d6e-456d-804f-f621766f10af.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/09/17/The+Learning+Curve+Is+Irrelevant.aspx</link>
      <pubDate>Wed, 17 Sep 2008 20:49:06 GMT</pubDate>
      <description>Something I've heard often is that "F# is too complex/functional programming is too hard". This is something that sorta came up in the comments here: &lt;a href="http://www.atrevido.net/blog/2008/09/16/Why+NOT+F.aspx"&gt;http://www.atrevido.net/blog/2008/09/16/Why+NOT+F.aspx&lt;/a&gt;. 
&lt;br&gt;
&lt;br&gt;
Why is this irrelevant? You only learn a language once. You pay the learning curve
cost one time; after that, you have the techniques and power at your disposal. However,
you pay the cost of code every time you read or write it. Since I'm going to read
and write a lot more code than number of languages I learn, I'd much, much, prefer
to pay this overhead once, up front, rather than in my code each time.&lt;br&gt;
&lt;br&gt;
Of course, it's not necessarily this simple. It's possible to design a powerful language
that renders code even more difficult. Regular expressions are perhaps a nice example
of this; they're often called "write only" code. Every time I do a non-trivial regexp,
I'm always going back to the reference. Another example are C macros -- text-based,
they quickly let you get into trouble. The design of C# runs away from this and tries
to make it very difficult to write code that is "hard" to figure out -- if there's
any edge case where a feature might be confusing or not work, C# tends to not allow
it at all. 
&lt;br&gt;
&lt;br&gt;
F# design is different. F# doesn't try to shelter users - it gives you tools and lets
you decide how to use them. It makes the assumption that if you're writing a program,
*you have some clue of what you're doing*. F#'s tools are still safe (compared to
say, C), but sure, you can go create a mess if you'd like. 
&lt;br&gt;
&lt;br&gt;
Poor code quality is *not* something that should be fixed solely via technical measures.
I liken it to using web filters to make sure "employees aren't goofing off on the
Internet" -- this is a policy/management issue and should be solved via administrative
means. If one of my devs is spending all day on 4chan but gets work done and adds
value, what do I care? Similarly, if the code quality coming out is acceptable and
the solutions work correctly, I don't care if it used macros, custom operators, "difficult"
code, etc. The process to make sure code quality is high (code reviews) will take
care of anyone abusing language features in stupid ways.&lt;br&gt;
&lt;br&gt;
But in truth, F# isn't actually much more complex to use. To the beginner, what seems
to be "unnecessary terseness" and a lot of complicated syntax is actually a very basic
system in action. Many of the "built-in" F# features such as the |&amp;gt; pipeline operator
are defined right in the language itself. There's no magic going on -- you can create
your own functionality in exactly the same way. Once you understand the basic rules,
you'll see that most everything else follows them.&lt;br&gt;
&lt;br&gt;
But at any rate, why is "easy to learn" a benefit? Sure, it's handy to promote a language
if people can pick it up easily, but it's not indicitive of long-term power. True,
if you have a "web developer" who's going to add a few server-side scripts, it's nice
that he doesn't have to learn much. But if you're developing an application of any
substance, I fail to see how these help, given the negative effects of having a "simple"
language.&lt;br&gt;
&lt;br&gt;
P.S. On my site I'm not trying to infer that F# will take over the world or that C#
will go away. I've met too many "professional developers" to realise that anything
that requires thinking isn't going to achieve stellar adoption. I'm simply pointing
out that the reasons come down to apathy and intelligence (with respect to the learning
curve; there could be other reasons as well), regardless of how politically correct
one phrases it.&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=1a161d7d-0d6e-456d-804f-f621766f10af" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,1a161d7d-0d6e-456d-804f-f621766f10af.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=12fc8c25-a371-4992-9b49-6e21fdd10e3a</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,12fc8c25-a371-4992-9b49-6e21fdd10e3a.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,12fc8c25-a371-4992-9b49-6e21fdd10e3a.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=12fc8c25-a371-4992-9b49-6e21fdd10e3a</wfw:commentRss>
      <slash:comments>17</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">This is actually an open request for comments.
I'm honestly interested in hearing why F# is not always the better candidate versus
C#. What can C# do well that F# cannot? In nearly everything, F# seems to come out
on top, as far as I can see. 
<br /><br />
Let's get these out of the way:<br /><br />
   - <i>Personal preference.</i> Enough said.<br />
   - <i>In beta.</i> Enough said.<br />
   - <i>Legacy code.</i> Sure, if you have a project in C#, it may not make
too much sense to switch mid-way.<br />
   - <i>Management.</i> Enough said.<br />
   - <i>No benefit. </i>This is simply lack of education and needs to be
addressed separately.<br />
   - <i>F#'s too hard/it's hard to hire F# devs.</i> This is a non-issue
that is a separate topic. In summary, anyone worth hiring for C# work should be able
to handle F#. [Exception being a very small deadline with an existing team...]<br /><br />
The only code reason I've seen is heavy native interop/pointer work. F# seems to be
slightly more verbose than C# in this case. It's not much more, but I could see if
you're doing just pointer code then it could get annoying. (Interestingly enough,
F# COM interop is much nicer than C# because it supports named and optional parameters
(<a href="http://blogs.msdn.com/dsyme/archive/2008/05/02/full-release-notes-for-f-1-9-4.aspx">http://blogs.msdn.com/dsyme/archive/2008/05/02/full-release-notes-for-f-1-9-4.aspx</a>,
search for "chart")...)<br /><br />
What other reasons are there for C# over F#?<br /><br /><b>Edit: </b>Good point in the comments about C# being a standard with open source
implementations. That could be a big issue for some. Another good point is the current
lack of tool support (like ClickOnce, ASPX and WPF designers, etc.). I don't see any
intrinsic reason F# wouldn't have those, except for limited resources.<br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=12fc8c25-a371-4992-9b49-6e21fdd10e3a" /></body>
      <title>Why NOT F#?</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,12fc8c25-a371-4992-9b49-6e21fdd10e3a.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/09/16/Why+NOT+F.aspx</link>
      <pubDate>Tue, 16 Sep 2008 05:06:28 GMT</pubDate>
      <description>This is actually an open request for comments. I'm honestly interested in hearing why F# is not always the better candidate versus C#. What can C# do well that F# cannot? In nearly everything, F# seems to come out on top, as far as I can see. &lt;br&gt;
&lt;br&gt;
Let's get these out of the way:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;Personal preference.&lt;/i&gt; Enough said.&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;In beta.&lt;/i&gt; Enough said.&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;Legacy code.&lt;/i&gt; Sure, if you have a project in C#, it may not make
too much sense to switch mid-way.&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;Management.&lt;/i&gt; Enough said.&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;No benefit. &lt;/i&gt;This is simply lack of education and needs to be
addressed separately.&lt;br&gt;
&amp;nbsp;&amp;nbsp; - &lt;i&gt;F#'s too hard/it's hard to hire F# devs.&lt;/i&gt; This is a non-issue
that is a separate topic. In summary, anyone worth hiring for C# work should be able
to handle F#. [Exception being a very small deadline with an existing team...]&lt;br&gt;
&lt;br&gt;
The only code reason I've seen is heavy native interop/pointer work. F# seems to be
slightly more verbose than C# in this case. It's not much more, but I could see if
you're doing just pointer code then it could get annoying. (Interestingly enough,
F# COM interop is much nicer than C# because it supports named and optional parameters
(&lt;a href="http://blogs.msdn.com/dsyme/archive/2008/05/02/full-release-notes-for-f-1-9-4.aspx"&gt;http://blogs.msdn.com/dsyme/archive/2008/05/02/full-release-notes-for-f-1-9-4.aspx&lt;/a&gt;,
search for "chart")...)&lt;br&gt;
&lt;br&gt;
What other reasons are there for C# over F#?&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Edit: &lt;/b&gt;Good point in the comments about C# being a standard with open source
implementations. That could be a big issue for some. Another good point is the current
lack of tool support (like ClickOnce, ASPX and WPF designers, etc.). I don't see any
intrinsic reason F# wouldn't have those, except for limited resources.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=12fc8c25-a371-4992-9b49-6e21fdd10e3a" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,12fc8c25-a371-4992-9b49-6e21fdd10e3a.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=8ba440f9-e54b-4948-bb85-387e67765a41</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,8ba440f9-e54b-4948-bb85-387e67765a41.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,8ba440f9-e54b-4948-bb85-387e67765a41.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=8ba440f9-e54b-4948-bb85-387e67765a41</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There are other great books out there such as <a href="http://www.amazon.com/Expert-F-Experts-Voice-Net/dp/1590598504">Expert
F#</a>. The F# Dev center has links to many other "learn F#" articles. All of these
are great.<br /><br />
But, something I found helpful is going "purely functional", and Haskell is the perfect
vehicle. When you're forced to think only functional, and don't have the other "escapes"
F# has, you bend your mind into understanding how you can accomplish things without
using mutation or object-orientation.<br /><br />
The downside of Haskell is that many resources seem to be very challenging to get
into. There's no doubt that the learning curve for Haskell can be tough. On top of
that, many materials tend to dive right into monads and it tends to end up too scary.
I've even bought several other good books on functional programming, but none of them
were easily approachable. (They have good content, but you can't start from zero by
using them.) 
<br /><br />
Enter <a href="http://book.realworldhaskell.org/">Real World Haskell</a>. This is
a *very* easy to follow book and really drives functional programming home. It doesn’t
assume you know anything about functional programming at all, so the learning curve
is a gentle slope.
</p>
        <p>
Even better? It's completely available online, so you can start reading it <a href="http://book.realworldhaskell.org/read/index.html">right
now</a>! Plus, it has reader-submitted comments which are of tremendous use, as they
ask and answer many common questions that might arise as you read along, without interfering
with the flow. You can read the entire book here: <a href="http://book.realworldhaskell.org/read/">http://book.realworldhaskell.org/read/</a> [But
buy it to support the excellent work the authors have done!]<br /><br />
I've found that my F# skills have gone up tremendously by reading Real World Haskell.
For instance, I "sorta" understood F#'s computation expressions and builder. Say,
enough to use them -- that's easy, like most things in F#), but understanding the
concepts behind them? Starting to learn Haskell really brings the understanding around.
This isn't to say that you'll eschew mutation and OO in F# -- such concepts can be
very useful (and increase performance on the CLR). But at least you'll know when a
more elegant solution is available. 
<br /><br />
(Plus, it's fun! As someone in #haskell on freenode put it to me: "Learning Haskell
will f*ck with your brain, and you'll like it.")
</p>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=8ba440f9-e54b-4948-bb85-387e67765a41" />
      </body>
      <title>Become a better F# programmer via Real World Haskell</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,8ba440f9-e54b-4948-bb85-387e67765a41.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/09/02/Become+A+Better+F+Programmer+Via+Real+World+Haskell.aspx</link>
      <pubDate>Tue, 02 Sep 2008 04:00:06 GMT</pubDate>
      <description>&lt;p &gt;
There are other great books out there such as &lt;a href="http://www.amazon.com/Expert-F-Experts-Voice-Net/dp/1590598504"&gt;Expert
F#&lt;/a&gt;. The F# Dev center has links to many other "learn F#" articles. All of these
are great.&lt;br&gt;
&lt;br&gt;
But, something I found helpful is going "purely functional", and Haskell is the perfect
vehicle. When you're forced to think only functional, and don't have the other "escapes"
F# has, you bend your mind into understanding how you can accomplish things without
using mutation or object-orientation.&lt;br&gt;
&lt;br&gt;
The downside of Haskell is that many resources seem to be very challenging to get
into. There's no doubt that the learning curve for Haskell can be tough. On top of
that, many materials tend to dive right into monads and it tends to end up too scary.
I've even bought several other good books on functional programming, but none of them
were easily approachable. (They have good content, but you can't start from zero by
using them.) 
&lt;br&gt;
&lt;br&gt;
Enter &lt;a href="http://book.realworldhaskell.org/"&gt;Real World Haskell&lt;/a&gt;. This is
a *very* easy to follow book and really drives functional programming home. It doesn’t
assume you know anything about functional programming at all, so the learning curve
is a gentle slope.
&lt;/p&gt;
&lt;p &gt;
Even better? It's completely available online, so you can start reading it &lt;a href="http://book.realworldhaskell.org/read/index.html"&gt;right
now&lt;/a&gt;! Plus, it has reader-submitted comments which are of tremendous use, as they
ask and answer many common questions that might arise as you read along, without interfering
with the flow. You can read the entire book here: &lt;a href="http://book.realworldhaskell.org/read/"&gt;http://book.realworldhaskell.org/read/&lt;/a&gt; [But
buy it to support the excellent work the authors have done!]&lt;br&gt;
&lt;br&gt;
I've found that my F# skills have gone up tremendously by reading Real World Haskell.
For instance, I "sorta" understood F#'s computation expressions and builder. Say,
enough to use them -- that's easy, like most things in F#), but understanding the
concepts behind them? Starting to learn Haskell really brings the understanding around.
This isn't to say that you'll eschew mutation and OO in F# -- such concepts can be
very useful (and increase performance on the CLR). But at least you'll know when a
more elegant solution is available. 
&lt;br&gt;
&lt;br&gt;
(Plus, it's fun! As someone in #haskell on freenode put it to me: "Learning Haskell
will f*ck with your brain, and you'll like it.")
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=8ba440f9-e54b-4948-bb85-387e67765a41" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,8ba440f9-e54b-4948-bb85-387e67765a41.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=05245828-8eca-4ba0-8194-e9fa45df67c5</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,05245828-8eca-4ba0-8194-e9fa45df67c5.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,05245828-8eca-4ba0-8194-e9fa45df67c5.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=05245828-8eca-4ba0-8194-e9fa45df67c5</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">Many times when I talk to a pro-dynamic
typing person, they bring up duck typing. And when I say that duck typing could be
resolved statically, I usually get wierd looks, or worse. Well F# exposes static duck
typing to users. At least, using the definition that C# uses for duck typing of foreach
and collection initalizers. (Yes, of course its a compiler feature and not true CLR/runtime
checking.) <i>I'm not promoting this, just pointing it out for fun.</i><br /><br />
F# allows inline values to accept "statically resolved type variables". The <a href="http://research.microsoft.com/fsharp/manual/spec2.aspx">F#
specification</a> says (§5.1.2):<br /><blockquote>A type of the form ^ident is a statically resolved variable type. A fresh
type inference variable is created and added to the type inference environment (see
§14.6). This type variable is tagged with an attribute indicating it may not be generalized
except at inline definitions (see §14.7), and likewise any type variable with which
it is equated via a type inference equation may similarly not be generalized.<br /></blockquote>At the end of this post I have a simple example to help understand this
kind of type variable. But more interesting is a another constraint you can apply
to such type variables. §5.1.5.3 Member Constraints: "A constraint of the form (typar
or ... or typar) : (member-sig) is an explicit member constraint." But, inside the
the F# library, this form is used with function application! For example, the char
function is defined:<br /><br /><font face="Courier New">let inline char (x: ^a) = 
<br />
    (^a : (static member ToChar: ^a -&gt; char) (x)) // Function application!<br /><font face="Verdana">     ...&lt;snip /&gt; -- </font></font><font face="Verdana">I
removed all the special case and inline IL code</font> as its irrelevant for this
post<br /><br />
Well, if we have member constraints with function application... we have "statically
typed duck typing":<br /><br /><font face="Courier New">let inline speak (a: ^a) = 
<br />
    let x = (^a : (member speak: unit -&gt; string) (a))<br />
    printfn "It said: %s" x<br />
    let y = (^a : (member talk: unit -&gt; string) (a))<br />
    printfn "Then it said %s" y<br /><br />
type duck() =<br />
    member x.speak() = "quack"<br />
    member x.talk() = "quackity quack"<br />
type dog() =<br />
    member x.speak() = "woof"<br />
    member x.talk() = "arrrr"<br /><br />
let x = new duck()<br />
let y = new dog()<br />
speak x<br />
speak y<br /><br /><font face="Verdana">Outputs:</font><br /><br />
It said: quack<br />
Then it said quackity quack<br />
It said: woof<br />
Then it said arrrr<br /><br /></font>The restriction is that you have to use inline to get generalization*. If it's
not inline, then it'll add additional constraints based on usage. If you removed inline
in this case, you'd get the following:<br /><br /><font face="Courier New">warning FS0064: This construct causes code to be less generic
than indicated by the type annotations. The type variable 'a has been constrained
to be type 'duck'.<br />
error FS0001: The type 'dog' is not compatible with the type 'duck'.<br /></font><br />
Inline is as it sounds - the IL code is emitted inline, which is obviously a drawback
in many cases. But that's the only way it can work - it has to statically know what
types and compile the right method info into the binary. I suppose it'd be possible
for the CLR to support this intrinsically. That way, the JIT could emit much more
optimized code, versus creating a new method for reach type. I don't know dynamic
languages well enough to know if this would be at all a help for interop. 
<br /><br />
*Here's a simple example to demonstrate the difference between 'a and ^a type parameters
generalization.<br /><font face="Courier New"><br />
&gt; let id (a : 'a) = a;;<br />
val id : 'a -&gt; 'a<br /><br />
&gt; id 1, id "hi";;<br />
val it : int * string = (1, "hi") // Good, it's generic<br /><br />
&gt; let id (a : ^a) = a;;<br />
  let id (a : ^a) = a;;<br />
  -------------^^<br />
stdin(8,14): warning FS0064: This construct causes code to be less generic than indicated
by the type annotations. The type variable 'a has been constrained to be type 'obj'.<br /><br />
val id : obj -&gt; obj<br /><br />
&gt; id 1, id "hi";;<br />
val it : obj * obj = (1, "hi") // Constrained to obj - not generic<br /><br />
&gt; let inline id (a : ^a) = a;;<br />
val inline id :  ^a -&gt;  ^a<br /><br />
&gt; id 1, id "hi";;<br />
val it : int * string = (1, "hi") // Since it's inline, it's generic<br /></font><br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=05245828-8eca-4ba0-8194-e9fa45df67c5" /></body>
      <title>Statically typed duck typing in F#</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,05245828-8eca-4ba0-8194-e9fa45df67c5.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/31/Statically+Typed+Duck+Typing+In+F.aspx</link>
      <pubDate>Sun, 31 Aug 2008 01:14:34 GMT</pubDate>
      <description>Many times when I talk to a pro-dynamic typing person, they bring up duck typing. And when I say that duck typing could be resolved statically, I usually get wierd looks, or worse. Well F# exposes static duck typing to users. At least, using the definition that C# uses for duck typing of foreach and collection initalizers. (Yes, of course its a compiler feature and not true CLR/runtime checking.) &lt;i&gt;I'm
not promoting this, just pointing it out for fun.&lt;/i&gt; 
&lt;br&gt;
&lt;br&gt;
F# allows inline values to accept "statically resolved type variables". The &lt;a href="http://research.microsoft.com/fsharp/manual/spec2.aspx"&gt;F#
specification&lt;/a&gt; says (§5.1.2):&lt;br&gt;
&lt;blockquote&gt;A type of the form ^ident is a statically resolved variable type. A fresh
type inference variable is created and added to the type inference environment (see
§14.6). This type variable is tagged with an attribute indicating it may not be generalized
except at inline definitions (see §14.7), and likewise any type variable with which
it is equated via a type inference equation may similarly not be generalized.&lt;br&gt;
&lt;/blockquote&gt;At the end of this post I have a simple example to help understand this
kind of type variable. But more interesting is a another constraint you can apply
to such type variables. §5.1.5.3 Member Constraints: "A constraint of the form (typar
or ... or typar) : (member-sig) is an explicit member constraint." But, inside the
the F# library, this form is used with function application! For example, the char
function is defined:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;let inline char (x: ^a) = 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (^a : (static member ToChar: ^a -&amp;gt; char) (x)) // Function application!&lt;br&gt;
&lt;font face="Verdana"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&amp;lt;snip /&amp;gt; -- &lt;/font&gt;&lt;/font&gt;&lt;font face="Verdana"&gt;I
removed all the special case and inline IL code&lt;/font&gt; as its irrelevant for this
post&lt;br&gt;
&lt;br&gt;
Well, if we have member constraints with function application... we have "statically
typed duck typing":&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;let inline speak (a: ^a) = 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; let x = (^a : (member speak: unit -&amp;gt; string) (a))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printfn "It said: %s" x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; let y = (^a : (member talk: unit -&amp;gt; string) (a))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printfn "Then it said %s" y&lt;br&gt;
&lt;br&gt;
type duck() =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member x.speak() = "quack"&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member x.talk() = "quackity quack"&lt;br&gt;
type dog() =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member x.speak() = "woof"&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; member x.talk() = "arrrr"&lt;br&gt;
&lt;br&gt;
let x = new duck()&lt;br&gt;
let y = new dog()&lt;br&gt;
speak x&lt;br&gt;
speak y&lt;br&gt;
&lt;br&gt;
&lt;font face="Verdana"&gt;Outputs:&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
It said: quack&lt;br&gt;
Then it said quackity quack&lt;br&gt;
It said: woof&lt;br&gt;
Then it said arrrr&lt;br&gt;
&lt;br&gt;
&lt;/font&gt;The restriction is that you have to use inline to get generalization*. If it's
not inline, then it'll add additional constraints based on usage. If you removed inline
in this case, you'd get the following:&lt;br&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;warning FS0064: This construct causes code to be less generic
than indicated by the type annotations. The type variable 'a has been constrained
to be type 'duck'.&lt;br&gt;
error FS0001: The type 'dog' is not compatible with the type 'duck'.&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
Inline is as it sounds - the IL code is emitted inline, which is obviously a drawback
in many cases. But that's the only way it can work - it has to statically know what
types and compile the right method info into the binary. I suppose it'd be possible
for the CLR to support this intrinsically. That way, the JIT could emit much more
optimized code, versus creating a new method for reach type. I don't know dynamic
languages well enough to know if this would be at all a help for interop. 
&lt;br&gt;
&lt;br&gt;
*Here's a simple example to demonstrate the difference between 'a and ^a type parameters
generalization.&lt;br&gt;
&lt;font face="Courier New"&gt;
&lt;br&gt;
&amp;gt; let id (a : 'a) = a;;&lt;br&gt;
val id : 'a -&amp;gt; 'a&lt;br&gt;
&lt;br&gt;
&amp;gt; id 1, id "hi";;&lt;br&gt;
val it : int * string = (1, "hi") // Good, it's generic&lt;br&gt;
&lt;br&gt;
&amp;gt; let id (a : ^a) = a;;&lt;br&gt;
&amp;nbsp; let id (a : ^a) = a;;&lt;br&gt;
&amp;nbsp; -------------^^&lt;br&gt;
stdin(8,14): warning FS0064: This construct causes code to be less generic than indicated
by the type annotations. The type variable 'a has been constrained to be type 'obj'.&lt;br&gt;
&lt;br&gt;
val id : obj -&amp;gt; obj&lt;br&gt;
&lt;br&gt;
&amp;gt; id 1, id "hi";;&lt;br&gt;
val it : obj * obj = (1, "hi") // Constrained to obj - not generic&lt;br&gt;
&lt;br&gt;
&amp;gt; let inline id (a : ^a) = a;;&lt;br&gt;
val inline id :&amp;nbsp; ^a -&amp;gt;&amp;nbsp; ^a&lt;br&gt;
&lt;br&gt;
&amp;gt; id 1, id "hi";;&lt;br&gt;
val it : int * string = (1, "hi") // Since it's inline, it's generic&lt;br&gt;
&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=05245828-8eca-4ba0-8194-e9fa45df67c5" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,05245828-8eca-4ba0-8194-e9fa45df67c5.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=d1b84dce-1ffb-47d5-b761-5a52181418a6</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,d1b84dce-1ffb-47d5-b761-5a52181418a6.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,d1b84dce-1ffb-47d5-b761-5a52181418a6.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d1b84dce-1ffb-47d5-b761-5a52181418a6</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I find myself using "Comment Selection"
often as I hack up quick scripts. Before the F# CTP, doing "Comment Selection" (Ctrl-K,
Ctrl-C) would simply add (* *) around the selection. This makes it hard to uncomment
specific lines. Now it goes and adds // in front of each line. Small things like this,
in aggregate, decide a great IDE. For as much as I dislike some C# language decisions,
their code editor is the best of anything I've ever seen.<br /><br />
Oh yea, the IntelliSense is also now WAY better. What's still missing is the "always
active autocomplete", although I suspect this is harder in F#'s #light syntax than
in C#. 
<br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d1b84dce-1ffb-47d5-b761-5a52181418a6" /></body>
      <title>F# CTP Comment Selection</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,d1b84dce-1ffb-47d5-b761-5a52181418a6.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/30/F+CTP+Comment+Selection.aspx</link>
      <pubDate>Sat, 30 Aug 2008 18:10:59 GMT</pubDate>
      <description>I find myself using "Comment Selection" often as I hack up quick scripts. Before the F# CTP, doing "Comment Selection" (Ctrl-K, Ctrl-C) would simply add (* *) around the selection. This makes it hard to uncomment specific lines. Now it goes and adds // in front of each line. Small things like this, in aggregate, decide a great IDE. For as much as I dislike some C# language decisions, their code editor is the best of anything I've ever seen.&lt;br&gt;
&lt;br&gt;
Oh yea, the IntelliSense is also now WAY better. What's still missing is the "always
active autocomplete", although I suspect this is harder in F#'s #light syntax than
in C#. 
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d1b84dce-1ffb-47d5-b761-5a52181418a6" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,d1b84dce-1ffb-47d5-b761-5a52181418a6.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=4c444a50-11b9-4209-9aa6-dbc3fced5f35</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,4c444a50-11b9-4209-9aa6-dbc3fced5f35.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,4c444a50-11b9-4209-9aa6-dbc3fced5f35.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4c444a50-11b9-4209-9aa6-dbc3fced5f35</wfw:commentRss>
      <title>No, really, the Event-based Asynchronous Pattern IS bad</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,4c444a50-11b9-4209-9aa6-dbc3fced5f35.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/29/No+Really+The+Eventbased+Asynchronous+Pattern+IS+Bad.aspx</link>
      <pubDate>Fri, 29 Aug 2008 21:03:55 GMT</pubDate>
      <description>Came across this:&lt;br&gt;
&lt;a href="http://michaelcurbanski.com/log/2008/08/29/functional-programmers-hate-events/"&gt;http://michaelcurbanski.com/log/2008/08/29/functional-programmers-hate-events/&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
His main point is that async total sucks, but that "Events don’t necessarily make
async code tragically unreadable!" As a demonstration, he shows some simple async
HTTP code without full or exception continuations. His code just enforces my point. 
&lt;br&gt;
&lt;br&gt;
At any rate, yes, sure, if your continuation code isn't too related to your calling
code, events can work. I mentioned this in my &lt;a href="http://www.atrevido.net/blog/2008/08/28/Tsk+Tsk+Silverlight+Events+Are+Not+Asyncs+Friend.aspx"&gt;post&lt;/a&gt;:
"events are a bad choice for code that is not loosely coupled" followed with "sometimes
a simple delegate field would be a better choice". Indeed, looking at Mike's sample
code, it seems as if using a simple delegate field would allow some nice refactoring.&lt;br&gt;
&lt;br&gt;
But a point of the &lt;a href="http://msdn.microsoft.com/en-us/library/wewwczdw.aspx"&gt;Event-based
Asynchronous Pattern&lt;/a&gt; is, and I quote, to "Wait for resources to become available
without stopping ("hanging") your application". So basically, you want to take an
existing method, say, in response to a user action, but you can't let it block. This
is exactly perfect for a continuation based approach and not so much for a loosely-coupled
event-based approach. The act of "firing" an event should indicate that you're letting
_other listeners_ (notice the plural) know when you did something. 
&lt;br&gt;
&lt;br&gt;
More on when events are appropriate: Say you're listening for SNMP messages and when
you receive one, you let "everyone" know that you did. You don't care what they do
with the result, and they just go off on their own. Events can work. Or, take, for
example, the &lt;a href="http://msdn.microsoft.com/en-us/library/8xs8549b.aspx"&gt;BackgroundWorker&lt;/a&gt;.
The BackgroundWorker doesn't help thread your blocking code, it just pushes the block
to a background thread you don't care about. Your HTTP code will still be all sync,
and you'll still burn a thread. BackgroundWorker main help is that it coordinates
back to your "UI" thread, since many UI frameworks have strong thread affinity and
will crash otherwise.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;But that was the whole point&lt;/b&gt;
&lt;br&gt;
The event async pattern, specifically in things like Silverlight, is completely aimed
at *not letting the thread block*. That's the _only_ problem it is trying to solve.
Silverlight only forces this because the main thread is the UI thread, and they won't
want the browser hanging by dumb apps that don't put the block on a background thread.
If they didn't want to do continuations, they could have at least kept the sync APIs,
and thrown an exception if called from the main thread (thus letting people who know
what they're doing not have to deal with the ugly event syntax). 
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;BTW, F# kicks the crap out of your language&lt;/b&gt;
&lt;br&gt;
I didn't mention it in my previous event post, but F#'s computation expressions let
you deal with continuations in a totally sexy way. &lt;a href="http://research.microsoft.com/%7Edsyme/"&gt;H.H.
Don Syme&lt;/a&gt;, writes about it here: &lt;a href="http://blogs.msdn.com/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx"&gt;Introducing
F# Asynchronous Workflows&lt;/a&gt;. You learn F# if you don't know it. But just to demonstrate,
here's an example from that introduction:&lt;br&gt;
&lt;br&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; AsyncHttp(url:string)
=&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;async
{&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style="color: green;"&gt;//
Create the web request object&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;let&lt;/span&gt; req
= WebRequest.Create(url)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style="color: green;"&gt;//
Get the response, asynchronously 
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;let!&lt;/span&gt; rsp
= req.GetResponseAsync()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style="color: green;"&gt;//
Grab the response stream and a reader. Clean up when we're done&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;use&lt;/span&gt; stream
= rsp.GetResponseStream()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;use&lt;/span&gt; reader
= &lt;span style="color: blue;"&gt;new&lt;/span&gt; System.IO.StreamReader(stream)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;
&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt;"&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;//
synchronous read-to-end&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;span style="font-size: 10pt; font-family: 'Lucida Console';"&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt; reader.ReadToEnd()
}&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
The let! binding handles things asynchronously; the rest of the body becomes a continuation.
If that isn't superior to any C# approach and doesn't make async easy, I don't know
what would.&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=4c444a50-11b9-4209-9aa6-dbc3fced5f35" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,4c444a50-11b9-4209-9aa6-dbc3fced5f35.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=e224845a-738c-4f71-a9a5-8fc135495578</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,e224845a-738c-4f71-a9a5-8fc135495578.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,e224845a-738c-4f71-a9a5-8fc135495578.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e224845a-738c-4f71-a9a5-8fc135495578</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">A huge thank you and congratulations to
MSRC and the F# team! (And to MSFT in general for taking their commercial languages
up quite a few notches.)<br /><br /><a href="http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx">http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx</a><br /><br />
I was up late last night reading different things on MSR and came across Andrew Kennedy's
page. What a surprise to see this morning that F# includes Units of Measure Inference
and Checking. <a href="http://research.microsoft.com/%7Eakenn/units/intro.html">http://research.microsoft.com/~akenn/units/intro.html</a><b>Edit</b>:
Oh cool, Mr. Kennedy now has a blog and goes into detail: <a href="http://blogs.msdn.com/andrewkennedy/archive/2008/08/20/units-of-measure-in-f-part-one-introducing-units.aspx">Units
of Measure in F#: Part One, Introducing Units</a><br /><br />
It seems they did a ton of general clean up and added consistency with .NET and so
on. (For instance, you don't need to explicitly accept subtypes with #.)<br /><br />
There's also an "AutoOpen" feature for modules, which allows modules to automatically
get opened if their container namespace is -- I suppose this allows more namespace
oriented development like .NET prefers.  
<br /><br />
Too much stuff to understand the full impact just by browsing the release notes. I
think the license has changed; at least it's quite clear that our commercial plans
with F# are perfectly allowable (there was some question before). So, now it gets
interesting...<br /><br /><p></p><img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e224845a-738c-4f71-a9a5-8fc135495578" /></body>
      <title>The most anticipated release of the year is out: F#</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,e224845a-738c-4f71-a9a5-8fc135495578.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/29/The+Most+Anticipated+Release+Of+The+Year+Is+Out+F.aspx</link>
      <pubDate>Fri, 29 Aug 2008 19:19:13 GMT</pubDate>
      <description>A huge thank you and congratulations to MSRC and the F# team! (And to MSFT in general for taking their commercial languages up quite a few notches.)&lt;br&gt;
&lt;br&gt;
&lt;a href="http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx"&gt;http://blogs.msdn.com/dsyme/archive/2008/08/29/detailed-release-notes-for-the-f-september-2008-ctp-release.aspx&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
I was up late last night reading different things on MSR and came across Andrew Kennedy's
page. What a surprise to see this morning that F# includes Units of Measure Inference
and Checking. &lt;a href="http://research.microsoft.com/%7Eakenn/units/intro.html"&gt;http://research.microsoft.com/~akenn/units/intro.html&lt;/a&gt; &lt;b&gt;Edit&lt;/b&gt;:
Oh cool, Mr. Kennedy now has a blog and goes into detail: &lt;a href="http://blogs.msdn.com/andrewkennedy/archive/2008/08/20/units-of-measure-in-f-part-one-introducing-units.aspx"&gt;Units
of Measure in F#: Part One, Introducing Units&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
It seems they did a ton of general clean up and added consistency with .NET and so
on. (For instance, you don't need to explicitly accept subtypes with #.)&lt;br&gt;
&lt;br&gt;
There's also an "AutoOpen" feature for modules, which allows modules to automatically
get opened if their container namespace is -- I suppose this allows more namespace
oriented development like .NET prefers.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
Too much stuff to understand the full impact just by browsing the release notes. I
think the license has changed; at least it's quite clear that our commercial plans
with F# are perfectly allowable (there was some question before). So, now it gets
interesting...&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=e224845a-738c-4f71-a9a5-8fc135495578" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,e224845a-738c-4f71-a9a5-8fc135495578.aspx</comments>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=06e1766b-c068-415a-89f6-fdb0e73906eb</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,06e1766b-c068-415a-89f6-fdb0e73906eb.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,06e1766b-c068-415a-89f6-fdb0e73906eb.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=06e1766b-c068-415a-89f6-fdb0e73906eb</wfw:commentRss>
      <slash:comments>15</slash:comments>
      <title>Extension Methods Suck</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,06e1766b-c068-415a-89f6-fdb0e73906eb.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/27/Extension+Methods+Suck.aspx</link>
      <pubDate>Wed, 27 Aug 2008 00:17:53 GMT</pubDate>
      <description>
Been meaning to write this for a while, and I think I touched on it &lt;a href="http://www.atrevido.net/blog/2007/09/05/C+30+And+LINQ+Misunderstandings.aspx"&gt;here&lt;/a&gt;,
but I'd like to expand a bit. C# Extension Methods are just a hack to compensate for
C#'s poor handling of functions in general. As far as I can tell, they were added
solely so you can do this type of thing in LINQ:&lt;br&gt;
&lt;br&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&amp;nbsp; var squares = myInts.Select(i
=&amp;gt; i * i)&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
Instead of:&lt;br&gt;
&lt;br&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&amp;nbsp; var squares = Enumerable.Select(myInts,
i =&amp;gt; i * i)&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
In other words, they wanted to provide some simple infix syntax for functions. Well,
that's a poor approach to the problem. 
&lt;br&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; - First off, it only works on function specially declared to be
"extensions", which means your composition options are limited to whatever the library
has built in. I can't send arguments to arbitrary static methods, like, say, "someVar.Console.WriteLine". 
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; - Second, since extension methods are defined solely by their method
name, ambiguity is quite easy to come across. There's no way to qualify Foo.Function
versus Bar.Function.&lt;span style=""&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p style=""&gt;
&lt;b style=""&gt;Pipeline&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;
&lt;br&gt;
Other languages approach this with two simple things (which actually simplify the
entire language/type system overall). First off, we need to be able to define function
operators. I'll demonstrate with F#'s pipeline operator:&lt;br&gt;
&lt;br&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; let (|&amp;gt;)
x f = f x&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
[As a side note, any language that lets you toss around operators and functions will
allow this kind of syntax – it isn’t that F# had to have compiler support for this
particular operator.]
&lt;/p&gt;
&lt;p&gt;
&lt;span style=""&gt;In fake C#, it’d be something like (for fun, notice the lack of type
inference):&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p style="text-indent: 0.5in;"&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;B operator|&amp;gt; &amp;lt;A, B&amp;gt;(A
x, Func&amp;lt;A, B&amp;gt; f) { return f(x); }&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
This means that the |&amp;gt; operator will take x on the left and apply it to f on the
right side. If this existed in C#, you'd be able to write something like:&lt;br&gt;
&lt;br&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Hello" |&amp;gt;
Console.WriteLine&lt;br&gt;
&lt;/span&gt;or&lt;br&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (myInts
|&amp;gt; Enumerable.Any) { .... }&lt;br style=""&gt;
&lt;!--[if !supportLineBreakNewLine]--&gt;
&lt;br style=""&gt;
&lt;!--[endif]--&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Now we can pass in a parameter to static methods. But, hey, whaddya know? With this,
Extension Methods are solved for all single-argument static methods! That was easy.
But what about Select – it takes two parameters, so this won’t work. 
&lt;br&gt;
&lt;br&gt;
&lt;b style=""&gt;Enter the Lambda&lt;/b&gt;
&lt;br&gt;
What if ALL functions took one parameter and output one parameter? If that were the
case, then we’d be set. But how do allow more than one parameter? Well, what if, every
time you declared a method with more than one parameter, it actually returned a method
that took the next parameter? For example, we could write “Add” as:&lt;br&gt;
&lt;br&gt;
&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;Func&amp;lt;int,int&amp;gt;
Add(Func&amp;lt;int,int&amp;gt; a) { return b =&amp;gt; &lt;span style=""&gt;&amp;nbsp;&lt;/span&gt;a + b; }&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
This is known as the curried form of Add. We’d now call it as: Add(5)(6). We can do
cute stuff like “var inc = Add(1)”. But, as the Add declaration shows, in C# this
is too unwieldy (and this is a simple example!). The compiler should actually do all
this for us, so we can just write our functions normally but use them as if they were
written in curried form.
&lt;/p&gt;
&lt;p&gt;
Now, if we simply swap the order of arguments for Enumerable.Select, we have our extension
method ready to roll: Enumerable.Select(Func, IEnumerable) can be used so:
&lt;/p&gt;
&lt;p style="text-indent: 0.5in;"&gt;
&lt;font face="Courier New"&gt;var squares = myInts |&amp;gt; Enumerable.Select(i =&amp;gt; i *
i)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Now the call to Select takes the lambda (i * i) and returns a function that takes
an Enumerable. It is then given the myInts, and everyone is happy. This is just a
quick, crap, explanation. Google can lead you to many more interesting resources about
partial application, currying and so on.&lt;br&gt;
&lt;/p&gt;
At any rate, I think if C# had taken this approach, we'd all be much better off. To
top it off, more functions would take their arguments in a proper style. As it is,
uncurried versions of Extension Methods are incompatible with normal function pipelining.
Oh well.&lt;br&gt;
&lt;br&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=06e1766b-c068-415a-89f6-fdb0e73906eb" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,06e1766b-c068-415a-89f6-fdb0e73906eb.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=0a572828-5362-4d33-acf0-696d0ff30f4f</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,0a572828-5362-4d33-acf0-696d0ff30f4f.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,0a572828-5362-4d33-acf0-696d0ff30f4f.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0a572828-5362-4d33-acf0-696d0ff30f4f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <font face="Verdana">I
saw this stack implementation (</font>
          <a href="http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx">
            <font face="Verdana">http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx</font>
          </a>
          <font face="Verdana">)
and wanted to see how I'd approach it:</font>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">#light<br /><span style="COLOR: blue">open</span><span style="COLOR: black"> System<br /><br /></span><span style="COLOR: blue">type</span><span style="COLOR: black"> 'a ImmutableStack
= 
<br />
   | Empty<br />
   | Stack </span><span style="COLOR: blue">of</span><span style="COLOR: black"> 'a
* 'a ImmutableStack<br />
   </span><span style="COLOR: blue">member</span><span style="COLOR: black"> x.Peek()
= </span><span style="COLOR: blue">match</span><span style="COLOR: black"> x </span><span style="COLOR: blue">with</span><span style="COLOR: black"> |
Stack (v, _) </span><span style="COLOR: blue">-&gt;</span><span style="COLOR: black"> Some
v | _ </span><span style="COLOR: blue">-&gt;</span><span style="COLOR: black"> None<br />
   </span><span style="COLOR: blue">member</span><span style="COLOR: black"> x.Pop()
= </span><span style="COLOR: blue">match</span><span style="COLOR: black"> x </span><span style="COLOR: blue">with</span><span style="COLOR: black"> |
Stack (_, s) </span><span style="COLOR: blue">-&gt;</span><span style="COLOR: black"> s
| _ </span><span style="COLOR: blue">-&gt;</span><span style="COLOR: black"> failwith </span><span style="COLOR: #a31515">"empty
stack"<br /></span><span style="COLOR: black">   </span><span style="COLOR: blue">member</span><span style="COLOR: black"> x.Push
a = Stack(a, x)<br />
   </span><span style="COLOR: blue">member</span><span style="COLOR: black"> x.IsEmpty
= x = Empty 
<br /></span></div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <font face="Verdana">(Well,
I'd probably just use a list in most cases.) But interestingly, he has an All function,
which returns a sequence of the entire stack. </font>
          <a href="http://blogs.msdn.com/chrsmith/default.aspx">
            <font face="Verdana">Chris</font>
          </a>
          <font face="Verdana"> suggested
using sequence expressions and then recursively calling the function:</font>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
            <span style="COLOR: blue">member</span>
            <span style="COLOR: black"> x.All
=<br /></span>
            <span style="COLOR: blue">    match</span>
            <span style="COLOR: black"> data </span>
            <span style="COLOR: blue">with</span>
            <span style="COLOR: black">
              <br />
        | Empty </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> Seq.empty<br />
        | Value (v,n) </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black">
              <br />
            seq {<br />
                </span>
            <span style="COLOR: blue">yield</span>
            <span style="COLOR: black"> v<br />
                </span>
            <span style="COLOR: blue">yield!</span>
            <span style="COLOR: black"> n.All
}<br /></span>
          </div>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <font face="Verdana">This
is pretty nice as far as syntax goes. For some reason, I wanted to see how it'd look
without the recursion (which needs to generate a new enumerator, unless the compiler
is doing some wicked awesome stuff, which wouldn't surprise me). Here are some of
the things I came up with:</font>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <span style="COLOR: blue">member</span>
          <span style="COLOR: black"> x.All1
= <br />
   </span>
          <span style="COLOR: blue">let</span>
          <span style="COLOR: black"> until
f = Seq.generate (</span>
          <span style="COLOR: blue">fun</span>
          <span style="COLOR: black"> () </span>
          <span style="COLOR: blue">-&gt;</span>
          <span style="COLOR: black"> ref
x) f (</span>
          <span style="COLOR: blue">fun</span>
          <span style="COLOR: black"> _ </span>
          <span style="COLOR: blue">-&gt;</span>
          <span style="COLOR: black"> ())<br />
   until (</span>
          <span style="COLOR: blue">fun</span>
          <span style="COLOR: black"> cur </span>
          <span style="COLOR: blue">-&gt;</span>
          <span style="COLOR: black">
          </span>
          <span style="COLOR: blue">match</span>
          <span style="COLOR: black"> (!cur) </span>
          <span style="COLOR: blue">with</span>
          <span style="COLOR: black"> <br />
                        |
Stack(v, s) </span>
          <span style="COLOR: blue">-&gt;</span>
          <span style="COLOR: black"> cur
:= s<br />
                                        
Some v <br />
                        |
_ </span>
          <span style="COLOR: blue">-&gt;</span>
          <span style="COLOR: black"> None)</span>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <font face="Verdana">Seq.generate
continues calling the function until None is returned, so this provides the loop termination.
Another similar approach:</font>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
            <span style="COLOR: blue">member</span>
            <span style="COLOR: black"> x.All2
= 
<br />
      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> until
f = Seq.generate (</span>
            <span style="COLOR: blue">fun</span>
            <span style="COLOR: black"> () </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> ref
x) f (</span>
            <span style="COLOR: blue">fun</span>
            <span style="COLOR: black"> _ </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> ())<br />
      until (</span>
            <span style="COLOR: blue">fun</span>
            <span style="COLOR: black"> cur </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black">
            </span>
            <span style="COLOR: blue">try</span>
            <span style="COLOR: black"> (!cur).Peek()<br />
                        </span>
            <span style="COLOR: blue">finally</span>
            <span style="COLOR: black">
            </span>
            <span style="COLOR: blue">if</span>
            <span style="COLOR: black"> not
(!cur).IsEmpty </span>
            <span style="COLOR: blue">then</span>
            <span style="COLOR: black"> cur
:= (!cur).Pop())</span>
          </div>
          <span style="COLOR: black">
          </span>
        </div>
        <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
          <span style="COLOR: black">
            <br />
            <font face="Verdana">Since Peek already returns 'a option, we can use it directly.
The problem is that we need to update cur to cur.Pop </font>
            <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
              <font face="Verdana">and then
return a value. The try/finally works, but the whole deal still doesn't seem very
elegant. Sequence expressions allow us to yield:</font>
            </div>
            <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
            <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
              <span style="COLOR: blue">member</span>
              <span style="COLOR: black"> x.All3
=<br />
      </span>
              <span style="COLOR: blue">let</span>
              <span style="COLOR: black"> cur
= ref x<br />
      { </span>
              <span style="COLOR: blue">while</span>
              <span style="COLOR: black"> not
(!cur).IsEmpty </span>
              <span style="COLOR: blue">do<br /></span>
              <span style="COLOR: black">          </span>
              <span style="COLOR: blue">yield</span>
              <span style="COLOR: black"> (!cur).Peek()<br />
          </span>
              <span style="COLOR: blue">do</span>
              <span style="COLOR: black"> cur
:= (!cur).Pop() }<br />
      |&gt; Seq.choose(</span>
              <span style="COLOR: blue">fun</span>
              <span style="COLOR: black"> x </span>
              <span style="COLOR: blue">-&gt;</span>
              <span style="COLOR: black"> x)<br /></span>
            </div>
          </span>
        </div>
        <p>
          <span style="COLOR: black">I dislike this one. Because we're not doing the pattern
match ourselves, we end up yielding 'a option. But None is never valid because
we guard on IsEmpty. This makes us stick a Seq.choose on with an identity function
to strip off the Some. [Side note, is there no built-in identity function?] Bringing
the match into the seq fixes the issue, but the code is pretty long:</span>
        </p>
        <span style="COLOR: black">
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
            <span style="COLOR: blue">member</span>
            <span style="COLOR: black"> x.All4
=<br />
      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> cur
= ref x<br />
      { </span>
            <span style="COLOR: blue">while</span>
            <span style="COLOR: black"> not
(!cur).IsEmpty </span>
            <span style="COLOR: blue">do<br /></span>
            <span style="COLOR: black">          </span>
            <span style="COLOR: blue">match</span>
            <span style="COLOR: black"> (!cur).Peek() </span>
            <span style="COLOR: blue">with</span>
            <span style="COLOR: black">
              <br />
                      |
None </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black">
            </span>
            <span style="COLOR: blue">do</span>
            <span style="COLOR: black"> ()<br />
                      |
Some v </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black">
            </span>
            <span style="COLOR: blue">do</span>
            <span style="COLOR: black"> cur
:= (!cur).Pop() 
<br />
                                  </span>
            <span style="COLOR: blue">yield</span>
            <span style="COLOR: black"> v
}<br /></span>
          </div>
          <p>
 I think what bothers me the most here is that the while and match are redundant.
The None case will never be matches because we guard on IsEmpty. Moving that into
a separate function gives:
</p>
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
            <span style="COLOR: blue">member</span>
            <span style="COLOR: black"> x.All5
=<br /></span>
            <span style="COLOR: black">      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> v
= </span>
            <span style="COLOR: blue">function</span>
            <span style="COLOR: black"> Stack(vl,
_) </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> vl | _ </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> failwith </span>
            <span style="COLOR: #a31515">"dont
call on empty"<br /></span>
            <span style="COLOR: black">      <span style="COLOR: blue">let</span><span style="COLOR: black"> cur
= ref x<br /></span>      { </span>
            <span style="COLOR: blue">while</span>
            <span style="COLOR: black"> (!cur)
&lt;&gt; Empty </span>
            <span style="COLOR: blue">do<br /></span>
            <span style="COLOR: black">          </span>
            <span style="COLOR: blue">yield</span>
            <span style="COLOR: black"> v
(!cur)<br />
          </span>
            <span style="COLOR: blue">do</span>
            <span style="COLOR: black"> cur
:= (!cur).Pop() }<br /></span>
          </div>
          <p>
I prefer All5 to All4, but still think All1 or All2 are nicer. (Chris's original is
best as far as I can tell.) How else can this be done?<br /></p>
          <p>
            <em>Edit:</em> I lost sight of one of the principals of functional programming: Composition.
Here's a simple update to All5 that, IMHO, vastly improves it:
</p>
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white">
            <span style="COLOR: blue">member</span>
            <span style="COLOR: black"> x.All6
=<br />
      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> cur
= ref x<br />
      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> toVal
= </span>
            <span style="COLOR: blue">function</span>
            <span style="COLOR: black"> Stack(v,
_) </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> v | _ </span>
            <span style="COLOR: blue">-&gt;</span>
            <span style="COLOR: black"> failwith </span>
            <span style="COLOR: #a31515">"dont
call on empty"<br /></span>
            <span style="COLOR: black">      </span>
            <span style="COLOR: blue">let</span>
            <span style="COLOR: black"> next
() = </span>
            <span style="COLOR: blue">try</span>
            <span style="COLOR: black"> toVal
(!cur) </span>
            <span style="COLOR: blue">finally</span>
            <span style="COLOR: black"> cur
:= (!cur).Pop()<br />
      { </span>
            <span style="COLOR: blue">while</span>
            <span style="COLOR: black"> (!cur)
&lt;&gt; Empty </span>
            <span style="COLOR: blue">do</span>
            <span style="COLOR: black">
            </span>
            <span style="COLOR: blue">yield</span>
            <span style="COLOR: black"> next
() }</span>
          </div>
          <div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"> 
</div>
          <p>
          </p>
        </span>Every line builds up and you don't have to keep its details "active" in
your mind. This makes the sequence expression (which is the driver of the algorithm)
easy to verify. And again, to clarify, in a real system I'd probably use what Chris
suggested since it's the nicest syntax. The other versions are only seeing what it
looks like if we toss recursion and use a reference cell.
<img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=0a572828-5362-4d33-acf0-696d0ff30f4f" /></body>
      <title>Reference cells in F# sequences</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,0a572828-5362-4d33-acf0-696d0ff30f4f.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/08/16/Reference+Cells+In+F+Sequences.aspx</link>
      <pubDate>Sat, 16 Aug 2008 04:09:49 GMT</pubDate>
      <description>&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;font face=Verdana&gt;I
saw this stack implementation (&lt;/font&gt;&lt;a href="http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx"&gt;&lt;font face=Verdana&gt;http://blogs.msdn.com/jaredpar/archive/2008/08/15/immutablestack-in-f.aspx&lt;/font&gt;&lt;/a&gt;&lt;font face=Verdana&gt;)
and wanted to see how I'd approach it:&lt;/font&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;#light&lt;br&gt;
&gt;&lt;span style="COLOR: blue"&gt;open&lt;/span&gt;&lt;span style="COLOR: black"&gt; System&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: blue"&gt;type&lt;/span&gt;&lt;span style="COLOR: black"&gt; 'a ImmutableStack
= 
&lt;br&gt;
&amp;nbsp;&amp;nbsp; | Empty&lt;br&gt;
&amp;nbsp;&amp;nbsp; | Stack &lt;/span&gt;&lt;span style="COLOR: blue"&gt;of&lt;/span&gt;&lt;span style="COLOR: black"&gt; 'a
* 'a ImmutableStack&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.Peek()
= &lt;/span&gt;&lt;span style="COLOR: blue"&gt;match&lt;/span&gt;&lt;span style="COLOR: black"&gt; x &lt;/span&gt;&lt;span style="COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="COLOR: black"&gt; |
Stack (v, _) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; Some
v | _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; None&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.Pop()
= &lt;/span&gt;&lt;span style="COLOR: blue"&gt;match&lt;/span&gt;&lt;span style="COLOR: black"&gt; x &lt;/span&gt;&lt;span style="COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="COLOR: black"&gt; |
Stack (_, s) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; s
| _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; failwith &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"empty
stack"&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.Push
a = Stack(a, x)&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.IsEmpty
= x = Empty 
&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;font face=Verdana&gt;(Well,
I'd probably just use a list in most cases.) But interestingly, he has an All function,
which returns a sequence of the entire stack. &lt;/font&gt;&lt;a href="http://blogs.msdn.com/chrsmith/default.aspx"&gt;&lt;font face=Verdana&gt;Chris&lt;/font&gt;&lt;/a&gt;&lt;font face=Verdana&gt; suggested
using sequence expressions and then recursively calling the function:&lt;/font&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All
=&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; match&lt;/span&gt;&lt;span style="COLOR: black"&gt; data &lt;/span&gt;&lt;span style="COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="COLOR: black"&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Empty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; Seq.empty&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Value (v,n) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;seq {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield&lt;/span&gt;&lt;span style="COLOR: black"&gt; v&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield!&lt;/span&gt;&lt;span style="COLOR: black"&gt; n.All
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;font face=Verdana&gt;This
is pretty nice as far as syntax goes. For some reason, I wanted to see how it'd look
without the recursion (which needs to generate a new enumerator, unless the compiler
is doing some wicked awesome stuff, which wouldn't surprise me). Here are some of
the things I came up with:&lt;/font&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All1
=&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; until
f = Seq.generate (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; () &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; ref
x) f (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; ())&lt;br&gt;
&amp;nbsp;&amp;nbsp; until (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;match&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|
Stack(v, s) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= s&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Some v&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|
_ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; None)&lt;/span&gt; 
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;font face=Verdana&gt;Seq.generate
continues calling the function until None is returned, so this provides the loop termination.
Another similar approach:&lt;/font&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All2
= 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; until
f = Seq.generate (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; () &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; ref
x) f (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; ())&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;until (&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;try&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur).Peek()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;finally&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if&lt;/span&gt;&lt;span style="COLOR: black"&gt; not
(!cur).IsEmpty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;then&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= (!cur).Pop())&lt;/span&gt;
&lt;/div&gt;
&lt;span style="COLOR: black"&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: black"&gt;
&lt;br&gt;
&lt;font face=Verdana&gt;Since Peek already returns 'a option, we can use it directly. The
problem is that we need to update cur to cur.Pop &lt;/font&gt; 
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;font face=Verdana&gt;and&amp;nbsp;then
return a value. The try/finally works, but the whole deal still doesn't seem very
elegant. Sequence expressions allow us to yield:&lt;/font&gt;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All3
=&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
= ref x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="COLOR: black"&gt; not
(!cur).IsEmpty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur).Peek()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= (!cur).Pop() }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&amp;gt; Seq.choose(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt;&lt;span style="COLOR: black"&gt; x &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; x)&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="COLOR: black"&gt;I dislike this one. Because we're not doing the pattern
match ourselves, we end up yielding 'a option. But None is never valid&amp;nbsp;because
we guard on IsEmpty. This makes us stick a Seq.choose on with an identity function
to strip off the Some. [Side note, is there no built-in identity function?] Bringing
the match into the seq fixes the issue, but the code is pretty long:&lt;/span&gt;
&lt;/p&gt;
&lt;span style="COLOR: black"&gt; 
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All4
=&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
= ref x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="COLOR: black"&gt; not
(!cur).IsEmpty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;match&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur).Peek() &lt;/span&gt;&lt;span style="COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="COLOR: black"&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|
None &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;/span&gt;&lt;span style="COLOR: black"&gt; ()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|
Some v &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= (!cur).Pop() 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield&lt;/span&gt;&lt;span style="COLOR: black"&gt; v
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;nbsp;I think what bothers me the most here is that the while and match are redundant.
The None case will never be matches because we guard on IsEmpty. Moving that into
a separate function gives:
&lt;/p&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All5
=&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; v
= &lt;/span&gt;&lt;span style="COLOR: blue"&gt;function&lt;/span&gt;&lt;span style="COLOR: black"&gt; Stack(vl,
_) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; vl | _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; failwith &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"dont
call on empty"&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
= ref x&lt;br&gt;
&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur)
&amp;lt;&amp;gt; Empty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield&lt;/span&gt;&lt;span style="COLOR: black"&gt; v
(!cur)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= (!cur).Pop() }&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;
I prefer All5 to All4, but still think All1 or All2 are nicer. (Chris's original is
best as far as I can tell.)&amp;nbsp;How else can this be done?&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Edit:&lt;/em&gt; I lost sight of one of the principals of functional programming: Composition.
Here's a simple update to All5 that, IMHO, vastly improves it:
&lt;/p&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&lt;span style="COLOR: blue"&gt;member&lt;/span&gt;&lt;span style="COLOR: black"&gt; x.All6
=&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
= ref x&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; toVal
= &lt;/span&gt;&lt;span style="COLOR: blue"&gt;function&lt;/span&gt;&lt;span style="COLOR: black"&gt; Stack(v,
_) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; v | _ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="COLOR: black"&gt; failwith &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"dont
call on empty"&lt;br&gt;
&lt;/span&gt;&lt;span style="COLOR: black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt;&lt;span style="COLOR: black"&gt; next
() = &lt;/span&gt;&lt;span style="COLOR: blue"&gt;try&lt;/span&gt;&lt;span style="COLOR: black"&gt; toVal
(!cur) &lt;/span&gt;&lt;span style="COLOR: blue"&gt;finally&lt;/span&gt;&lt;span style="COLOR: black"&gt; cur
:= (!cur).Pop()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;/span&gt;&lt;span style="COLOR: blue"&gt;while&lt;/span&gt;&lt;span style="COLOR: black"&gt; (!cur)
&amp;lt;&amp;gt; Empty &lt;/span&gt;&lt;span style="COLOR: blue"&gt;do&lt;/span&gt;&lt;span style="COLOR: black"&gt; &lt;/span&gt;&lt;span style="COLOR: blue"&gt;yield&lt;/span&gt;&lt;span style="COLOR: black"&gt; next
() }&lt;/span&gt; 
&lt;/div&gt;
&lt;div style="FONT-SIZE: 10pt; FONT-FAMILY: Monospace; BACKGROUND-COLOR: white"&gt;&amp;nbsp;
&lt;/div&gt;
&lt;p&gt;
&lt;/span&gt;Every line builds up and you don't have to keep its details "active" in your
mind. This makes the sequence expression (which is the driver of the algorithm) easy
to verify. And again, to clarify, in a real system I'd probably use what Chris suggested
since it's the nicest syntax. The other versions are only seeing what it looks like
if we toss recursion and use a reference cell.&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=0a572828-5362-4d33-acf0-696d0ff30f4f" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,0a572828-5362-4d33-acf0-696d0ff30f4f.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=ae23e7f3-a43e-4bc5-90cc-12b2b74dff99</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,ae23e7f3-a43e-4bc5-90cc-12b2b74dff99.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,ae23e7f3-a43e-4bc5-90cc-12b2b74dff99.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=ae23e7f3-a43e-4bc5-90cc-12b2b74dff99</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://www.atrevido.net/blog/2008/06/26/LINQ+To+The+CRUD+CreateTable+Generator+In+F.aspx">last
post</a>, I said I'd write the sample in C# to compare to F#. Well, I grossly underestimated
the power of query comprehensions. The C# version is almost the same length (formatting
differences, mainly). I'm surprised and impressed. (Or maybe I'm writing F# like I'd
write C#.) Edit: I think maybe sequence expressions could cut it down a bit...
</p>
        <p>
...But... C# still can't do discriminated unions efficiently or effectively ;). 
</p>
        <p>
          <hr />
        </p>
        <p>
        </p>
        <div style="FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas">
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    1</span> <span style="COLOR: green">//
crudcreatecompare.cs: Generates LINQ CRUD table fields using the horribly named DatabaseBase
code</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    2</span> <span style="COLOR: green">//</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    3</span> <span style="COLOR: green">//
Tables look like: [Table(Name="dbo.Accounts")]</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    4</span> <span style="COLOR: green">//
Columns look like this:</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    5</span> <span style="COLOR: green">// 
[Column(Storage="_AccountName", DbType="VarChar(128) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    6</span> <span style="COLOR: green">// 
[DataMember(Order=1)] // Exists if serialization is turned on; used to order key parameters</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    7</span> <span style="COLOR: green">//
Emits:</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    8</span> <span style="COLOR: green">// 
public static readonly TableHelper&lt;Account, String&gt; Accounts =</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    9</span> <span style="COLOR: green">//   
  CreateTable(dc =&gt; dc.Accounts, a =&gt; a.AccountName);</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   10</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   11</span> <span style="COLOR: blue">using</span> System;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   12</span> <span style="COLOR: blue">using</span> System.Collections.Generic;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   13</span> <span style="COLOR: blue">using</span> System.Data.Linq.Mapping;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   14</span> <span style="COLOR: blue">using</span> System.IO;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   15</span> <span style="COLOR: blue">using</span> System.Linq;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   16</span> <span style="COLOR: blue">using</span> System.Reflection;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   17</span> <span style="COLOR: blue">using</span> System.Runtime.Serialization;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   18</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   19</span> <span style="COLOR: blue">class</span><span style="COLOR: #2b91af">Program</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   20</span> {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   21</span>     <span style="COLOR: blue">static</span><span style="COLOR: blue">void</span> Main()
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   22</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   23</span>         <span style="COLOR: #2b91af">Console</span>.WriteLine(
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   24</span>        
    <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">Program</span>().generate(
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   25</span>        
    <span style="COLOR: #a31515">"C:\\yourlinq.dll"</span>));
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   26</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   27</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   28</span>     <span style="COLOR: blue">string</span> generate(<span style="COLOR: blue">string</span> asmpath)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   29</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   30</span>         <span style="COLOR: blue">var</span> asm
= <span style="COLOR: #2b91af">Assembly</span>.LoadFrom(asmpath);
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   31</span>         <span style="COLOR: blue">var</span> lines
= <span style="COLOR: blue">from</span> t <span style="COLOR: blue">in</span> asm.GetExportedTypes()
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   32</span>        
            <span style="COLOR: blue">let</span> ta
= getAttr&lt;<span style="COLOR: #2b91af">TableAttribute</span>&gt;(t)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   33</span>        
            <span style="COLOR: blue">where</span> ta
!= <span style="COLOR: blue">null</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   34</span>        
            <span style="COLOR: blue">let</span> name
= pluralize(ta.Name.Replace(<span style="COLOR: #a31515">"dbo."</span>, <span style="COLOR: #a31515">""</span>))
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   35</span>        
            <span style="COLOR: blue">orderby</span> name
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   36</span>        
            <span style="COLOR: blue">select</span> genTable(t,
name);
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   37</span>         <span style="COLOR: blue">return</span> joinStrings(<span style="COLOR: #a31515">""</span>,
lines);
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   38</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   39</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   40</span>     <span style="COLOR: blue">string</span> genTable(<span style="COLOR: #2b91af">Type</span> t, <span style="COLOR: blue">string</span> tableName)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   41</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   42</span>         <span style="COLOR: blue">var</span> keyProps
=
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   43</span>        
    <span style="COLOR: blue">from</span> p <span style="COLOR: blue">in</span> t.GetProperties()
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   44</span>        
    <span style="COLOR: blue">let</span> c = getAttr&lt;<span style="COLOR: #2b91af">ColumnAttribute</span>&gt;(p)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   45</span>        
    <span style="COLOR: blue">where</span> c != <span style="COLOR: blue">null</span> &amp;&amp;
c.IsPrimaryKey
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   46</span>        
    <span style="COLOR: blue">let</span> dm = getAttr&lt;<span style="COLOR: #2b91af">DataMemberAttribute</span>&gt;(p)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   47</span>        
    <span style="COLOR: blue">orderby</span> dm == <span style="COLOR: blue">null</span> ?
0 : dm.Order
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   48</span>        
    <span style="COLOR: blue">select</span> p;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   49</span>         <span style="COLOR: blue">var</span> tw
= <span style="COLOR: blue">new</span><span style="COLOR: #2b91af">StringWriter</span>();
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   50</span>        
tw.WriteLine(<span style="COLOR: #a31515">"public static readonly TableHelper&lt;{0},
{1}&gt; {2} ="</span>,
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   51</span>        
    t.Name,
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   52</span>        
    joinStrings(<span style="COLOR: #a31515">", "</span>, keyProps.Select(p
=&gt; p.PropertyType.Name)),
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   53</span>        
    tableName);
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   54</span>        
tw.WriteLine(<span style="COLOR: #a31515">"\tCreateTable(dc =&gt; dc.{0}, {1});"</span>,
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   55</span>        
    tableName,
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   56</span>        
    joinStrings(<span style="COLOR: #a31515">", "</span>, keyProps.Select(p
=&gt; <span style="COLOR: #a31515">"a =&gt; a."</span> + p.Name)));
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   57</span>        
tw.WriteLine();
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   58</span>         <span style="COLOR: blue">return</span> tw.ToString();
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   59</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   60</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   61</span>     <span style="COLOR: blue">string</span> joinStrings(<span style="COLOR: blue">string</span> sep, <span style="COLOR: #2b91af">IEnumerable</span>&lt;<span style="COLOR: blue">string</span>&gt;
items)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   62</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   63</span>         <span style="COLOR: blue">return</span><span style="COLOR: blue">string</span>.Join(sep,
items.ToArray());
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   64</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   65</span>     <span style="COLOR: blue">string</span> pluralize(<span style="COLOR: blue">string</span> s)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   66</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   67</span>         <span style="COLOR: blue">return</span> s.EndsWith(<span style="COLOR: #a31515">"s"</span>)
? s : s + <span style="COLOR: #a31515">"s"</span>;
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   68</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   69</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   70</span>     T
getAttr&lt;T&gt;(<span style="COLOR: #2b91af">ICustomAttributeProvider</span> icap)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   71</span>     {
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   72</span>         <span style="COLOR: blue">var</span> a
= icap.GetCustomAttributes(<span style="COLOR: blue">typeof</span>(T), <span style="COLOR: blue">true</span>);
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   73</span>         <span style="COLOR: blue">return</span> a.Length
== 0 ? <span style="COLOR: blue">default</span>(T) : (T)a[0];
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   74</span>     }
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   75</span> }
</p>
        </div>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=ae23e7f3-a43e-4bc5-90cc-12b2b74dff99" />
      </body>
      <title>I underestimated the power of query comprehensions</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,ae23e7f3-a43e-4bc5-90cc-12b2b74dff99.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/06/26/I+Underestimated+The+Power+Of+Query+Comprehensions.aspx</link>
      <pubDate>Thu, 26 Jun 2008 06:15:10 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://www.atrevido.net/blog/2008/06/26/LINQ+To+The+CRUD+CreateTable+Generator+In+F.aspx"&gt;last
post&lt;/a&gt;, I said I'd write the sample in C# to compare to F#. Well, I grossly underestimated
the power of query comprehensions. The C# version is almost the same length (formatting
differences, mainly). I'm surprised and impressed. (Or maybe I'm writing F# like I'd
write C#.) Edit: I think maybe sequence expressions could cut it down a bit...
&lt;/p&gt;
&lt;p&gt;
...But... C# still can't do discriminated unions efficiently or effectively ;). 
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
crudcreatecompare.cs: Generates LINQ CRUD table fields using the horribly named DatabaseBase
code&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Tables look like: [Table(Name="dbo.Accounts")]&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Columns look like this:&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
[Column(Storage="_AccountName", DbType="VarChar(128) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
[DataMember(Order=1)] // Exists if serialization is turned on; used to order key parameters&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Emits:&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
public static readonly TableHelper&amp;lt;Account, String&amp;gt; Accounts =&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;9&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; CreateTable(dc =&amp;gt; dc.Accounts, a =&amp;gt; a.AccountName);&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;10&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;11&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;12&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;13&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Data.Linq.Mapping;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;14&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.IO;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;15&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Linq;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;16&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Reflection;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;17&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Runtime.Serialization;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;18&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;19&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Program&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;20&lt;/span&gt;&amp;nbsp;{
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;21&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;void&lt;/span&gt; Main()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;22&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;23&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;24&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;Program&lt;/span&gt;().generate(
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;25&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: #a31515"&gt;"C:\\yourlinq.dll"&lt;/span&gt;));
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;26&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;28&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; generate(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; asmpath)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;29&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;30&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; asm
= &lt;span style="COLOR: #2b91af"&gt;Assembly&lt;/span&gt;.LoadFrom(asmpath);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;31&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; lines
= &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; t &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; asm.GetExportedTypes()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;32&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; ta
= getAttr&amp;lt;&lt;span style="COLOR: #2b91af"&gt;TableAttribute&lt;/span&gt;&amp;gt;(t)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;33&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; ta
!= &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;34&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; name
= pluralize(ta.Name.Replace(&lt;span style="COLOR: #a31515"&gt;"dbo."&lt;/span&gt;, &lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;35&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;orderby&lt;/span&gt; name
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;36&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; genTable(t,
name);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;37&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; joinStrings(&lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;,
lines);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;38&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;39&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;40&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; genTable(&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt; t, &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; tableName)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;41&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;42&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; keyProps
=
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;43&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; p &lt;span style="COLOR: blue"&gt;in&lt;/span&gt; t.GetProperties()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;44&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; c = getAttr&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ColumnAttribute&lt;/span&gt;&amp;gt;(p)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;45&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; c != &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;
c.IsPrimaryKey
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;46&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; dm = getAttr&amp;lt;&lt;span style="COLOR: #2b91af"&gt;DataMemberAttribute&lt;/span&gt;&amp;gt;(p)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;47&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;orderby&lt;/span&gt; dm == &lt;span style="COLOR: blue"&gt;null&lt;/span&gt; ?
0 : dm.Order
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;48&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;select&lt;/span&gt; p;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;49&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; tw
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;StringWriter&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;50&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
tw.WriteLine(&lt;span style="COLOR: #a31515"&gt;"public static readonly TableHelper&amp;lt;{0},
{1}&amp;gt; {2} ="&lt;/span&gt;,
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;51&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Name,
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;52&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; joinStrings(&lt;span style="COLOR: #a31515"&gt;", "&lt;/span&gt;, keyProps.Select(p
=&amp;gt; p.PropertyType.Name)),
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;53&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; tableName);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;54&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
tw.WriteLine(&lt;span style="COLOR: #a31515"&gt;"\tCreateTable(dc =&amp;gt; dc.{0}, {1});"&lt;/span&gt;,
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;55&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; tableName,
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;56&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; joinStrings(&lt;span style="COLOR: #a31515"&gt;", "&lt;/span&gt;, keyProps.Select(p
=&amp;gt; &lt;span style="COLOR: #a31515"&gt;"a =&amp;gt; a."&lt;/span&gt; + p.Name)));
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;57&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
tw.WriteLine();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;58&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; tw.ToString();
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;59&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;60&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;61&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; joinStrings(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; sep, &lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&amp;gt;
items)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;62&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;63&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;.Join(sep,
items.ToArray());
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;64&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;65&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt; pluralize(&lt;span style="COLOR: blue"&gt;string&lt;/span&gt; s)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;66&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;67&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; s.EndsWith(&lt;span style="COLOR: #a31515"&gt;"s"&lt;/span&gt;)
? s : s + &lt;span style="COLOR: #a31515"&gt;"s"&lt;/span&gt;;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;68&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;69&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;70&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T
getAttr&amp;lt;T&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;ICustomAttributeProvider&lt;/span&gt; icap)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;71&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;72&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;var&lt;/span&gt; a
= icap.GetCustomAttributes(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(T), &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;73&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; a.Length
== 0 ? &lt;span style="COLOR: blue"&gt;default&lt;/span&gt;(T) : (T)a[0];
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;74&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;75&lt;/span&gt;&amp;nbsp;}
&lt;/p&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=ae23e7f3-a43e-4bc5-90cc-12b2b74dff99" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,ae23e7f3-a43e-4bc5-90cc-12b2b74dff99.aspx</comments>
      <category>Code</category>
      <category>FSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.atrevido.net/blog/Trackback.aspx?guid=d6d66155-c288-4984-b4ba-4e58285daf7f</trackback:ping>
      <pingback:server>http://www.atrevido.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.atrevido.net/blog/PermaLink,guid,d6d66155-c288-4984-b4ba-4e58285daf7f.aspx</pingback:target>
      <dc:creator>
      </dc:creator>
      <wfw:comment>http://www.atrevido.net/blog/CommentView,guid,d6d66155-c288-4984-b4ba-4e58285daf7f.aspx</wfw:comment>
      <wfw:commentRss>http://www.atrevido.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d6d66155-c288-4984-b4ba-4e58285daf7f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
With the <a href="http://www.atrevido.net/blog/2008/06/26/LINQ+To+The+CRUD+RTM.aspx">DatabaseBase</a> and
TableHelper classes, you still have to generate a CreateTable field per table. Why
do it by hand? I wrote an F# script to generate the statements. I must say, I'm
loving F# more than I had hoped. The more I learn, the better it gets. I've noticed
(even in C#) that using a functional style generally means less errors. This script
worked without bugs the first time (i.e., as soon as it compiled), which is pretty
cool (granted, it's not big, but I'm sure if I had tons of for loops, I woulda messed
up somewhere). Maybe this weekend I'll try writing it in C# just to compare (pretty
sure it'll be more than 59 lines!). And I'll preempt comments about readability:
Yes, it may be difficult to read if you don't know F#, but more on that later...
</p>
        <p>
I'd love feedback as to making it more "functional"; years of imperative programming
don't die quickly. Also, I'm not very happy with the definition of chooseAttr, but
I can't seem to get it to infer the type I want otherwise.
</p>
        <p>
Anyways, here's the code. You'll need to specify the references when compiling:
-r "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll"
-r "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll" 
</p>
        <p>
          <hr />
        </p>
        <p>
        </p>
        <div style="FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas">
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    1</span> <span style="COLOR: green">//
crudcreatetable.fsx: Generates LINQ CRUD table fields using the horribly named DatabaseBase
code</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    2</span> <span style="COLOR: green">//</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    3</span> <span style="COLOR: green">//
Tables look like: [Table(Name="dbo.Accounts")]</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    4</span> <span style="COLOR: green">//
Columns look like this:</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    5</span> <span style="COLOR: green">// 
[Column(Storage="_AccountName", DbType="VarChar(128) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    6</span> <span style="COLOR: green">// 
[DataMember(Order=1)] // Exists if serialization is turned on; used to order key parameters</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    7</span> <span style="COLOR: green">//
Emits:</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    8</span> <span style="COLOR: green">// 
public static readonly TableHelper&lt;Account, String&gt; Accounts =</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">    9</span> <span style="COLOR: green">//   
  CreateTable(dc =&gt; dc.Accounts, a =&gt; a.AccountName);</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   10</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   11</span> #light
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   12</span> <span style="COLOR: blue">open</span> System
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   13</span> <span style="COLOR: blue">open</span> System.Reflection
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   14</span> <span style="COLOR: blue">open</span> System.Data.Linq.Mapping
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   15</span> <span style="COLOR: blue">open</span> System.Runtime.Serialization
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   16</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   17</span> <span style="COLOR: blue">let</span> getAttr&lt;'target,
'a <span style="COLOR: blue">when</span> 'a :&gt; ICustomAttributeProvider&gt; (ty
: 'a) = 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   18</span>     <span style="COLOR: blue">match</span> List.of_array
(ty.GetCustomAttributes(typeof&lt;'target&gt;, <span style="COLOR: blue">true</span>)) <span style="COLOR: blue">with</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   19</span>        
| a::_ <span style="COLOR: blue">-&gt;</span> Some (a :?&gt; 'target)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   20</span>        
| [] <span style="COLOR: blue">-&gt;</span> None
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   21</span> <span style="COLOR: blue">let</span> chooseAttr&lt;'target,
'a <span style="COLOR: blue">when</span> 'a :&gt; ICustomAttributeProvider&gt; (ty
: 'a) = 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   22</span>     <span style="COLOR: blue">match</span> getAttr&lt;'target,_&gt;
ty <span style="COLOR: blue">with</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   23</span>        
| Some(a) <span style="COLOR: blue">-&gt;</span> Some(ty,a)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   24</span>        
| None <span style="COLOR: blue">-&gt;</span> None
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   25</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   26</span> <span style="COLOR: blue">let</span> joinStrings
(sep:string) items = items |&gt; Seq.fold1 (<span style="COLOR: blue">fun</span> acc
x <span style="COLOR: blue">-&gt;</span> acc + sep + x) 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   27</span> <span style="COLOR: blue">let</span> pluralize
(name:string) = <span style="COLOR: blue">if</span> name.EndsWith(<span style="COLOR: #a31515">"s"</span>) <span style="COLOR: blue">then</span> name <span style="COLOR: blue">else</span> name
+ <span style="COLOR: #a31515">"s"</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   28</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   29</span> <span style="COLOR: blue">let</span> generate(asmpath:string)
=
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   30</span>     <span style="COLOR: blue">let</span> genTable
(t:Type, tableName) =
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   31</span>         <span style="COLOR: blue">let</span> keyProps
= 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   32</span>        
    t.GetProperties() 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   33</span>        
    |&gt; Seq.choose (chooseAttr&lt;ColumnAttribute,_&gt;)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   34</span>        
    |&gt; Seq.filter(<span style="COLOR: blue">fun</span> (p,c) <span style="COLOR: blue">-&gt;</span> c.IsPrimaryKey)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   35</span>        
    |&gt; Seq.map(<span style="COLOR: blue">fun</span> (p,_) <span style="COLOR: blue">-&gt;</span> p,
getAttr&lt;DataMemberAttribute, _&gt; p)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   36</span>        
    |&gt; Seq.orderBy(<span style="COLOR: blue">function</span> | _,Some(dm) <span style="COLOR: blue">-&gt;</span> dm.Order
| _ <span style="COLOR: blue">-&gt;</span> 0)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   37</span>        
    |&gt; Seq.map (<span style="COLOR: blue">fun</span> (p,_) <span style="COLOR: blue">-&gt;</span> p)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   38</span>         <span style="COLOR: blue">let</span> tw
= <span style="COLOR: blue">new</span> IO.StringWriter()
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   39</span>         <span style="COLOR: blue">let</span> pn
fmt = Printf.twprintfn tw fmt
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   40</span>        
pn <span style="COLOR: #a31515">"public static readonly TableHelper&lt;%s, %s&gt;
%s ="</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   41</span>        
    t.Name 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   42</span>        
    (joinStrings <span style="COLOR: #a31515">", "</span> (keyProps
|&gt; Seq.map (<span style="COLOR: blue">fun</span> p <span style="COLOR: blue">-&gt;</span> p.PropertyType.Name)))
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   43</span>        
    tableName
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   44</span>        
pn <span style="COLOR: #a31515">"\tCreateTable(dc =&gt; dc.%s, %s);"</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   45</span>        
    tableName
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   46</span>        
    (joinStrings <span style="COLOR: #a31515">", "</span> (keyProps
|&gt; Seq.map (<span style="COLOR: blue">fun</span> p <span style="COLOR: blue">-&gt;</span><span style="COLOR: #a31515">"a
=&gt; a."</span> + p.Name)))
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   47</span>        
pn <span style="COLOR: #a31515">""</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   48</span>        
tw.ToString()
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   49</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   50</span>     <span style="COLOR: blue">let</span> asm
= Assembly.LoadFrom asmpath <span style="COLOR: green">// Don't use ReflectionOnly
'cause it won't resolve dependencies</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   51</span>     asm.GetExportedTypes
() 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   52</span>        
|&gt; Seq.choose (chooseAttr&lt;TableAttribute,_&gt;)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   53</span>        
|&gt; Seq.map (<span style="COLOR: blue">fun</span> (t,ta) <span style="COLOR: blue">-&gt;</span> t,
ta.Name.Replace(<span style="COLOR: #a31515">"dbo."</span>, <span style="COLOR: #a31515">""</span>)
|&gt; pluralize)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   54</span>        
|&gt; Seq.orderBy (<span style="COLOR: blue">fun</span> (t,_) <span style="COLOR: blue">-&gt;</span> t.Name)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   55</span>        
|&gt; Seq.map genTable
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   56</span>        
|&gt; Seq.fold1 (+)
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   57</span> 
</p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   58</span> generate <span style="COLOR: #a31515">"C:\\yourlinq.dll"</span></p>
          <p style="MARGIN: 0px">
            <span style="COLOR: #2b91af">   59</span>     |&gt;
Console.WriteLine
</p>
        </div>
        <img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d6d66155-c288-4984-b4ba-4e58285daf7f" />
      </body>
      <title>LINQ to the CRUD CreateTable generator in F#</title>
      <guid isPermaLink="false">http://www.atrevido.net/blog/PermaLink,guid,d6d66155-c288-4984-b4ba-4e58285daf7f.aspx</guid>
      <link>http://www.atrevido.net/blog/2008/06/26/LINQ+To+The+CRUD+CreateTable+Generator+In+F.aspx</link>
      <pubDate>Thu, 26 Jun 2008 05:13:44 GMT</pubDate>
      <description>&lt;p&gt;
With the &lt;a href="http://www.atrevido.net/blog/2008/06/26/LINQ+To+The+CRUD+RTM.aspx"&gt;DatabaseBase&lt;/a&gt; and
TableHelper classes, you still have to generate a CreateTable field per table. Why
do it by hand? I wrote an F#&amp;nbsp;script to generate the statements. I must say, I'm
loving F# more than I had hoped. The more I learn, the better it gets. I've noticed
(even in C#) that using a functional style generally means less errors. This script
worked without bugs the first time (i.e., as soon as it compiled), which is pretty
cool (granted, it's not big, but I'm sure if I had tons of for loops, I woulda messed
up somewhere). Maybe this weekend I'll try writing it in C# just to compare (pretty
sure it'll be more than 59 lines!). And I'll preempt comments&amp;nbsp;about readability:
Yes, it may be difficult to read if you don't know F#, but more on that later...
&lt;/p&gt;
&lt;p&gt;
I'd love feedback as to making it more "functional"; years of imperative programming
don't die quickly. Also, I'm not very happy with the definition of chooseAttr, but
I can't seem to get it to infer the type I want otherwise.
&lt;/p&gt;
&lt;p&gt;
Anyways, here's the code.&amp;nbsp;You'll need to specify the references when compiling:
-r "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.Linq.dll"
-r "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll" 
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;div style="FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas"&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
crudcreatetable.fsx: Generates LINQ CRUD table fields using the horribly named DatabaseBase
code&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Tables look like: [Table(Name="dbo.Accounts")]&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Columns look like this:&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
[Column(Storage="_AccountName", DbType="VarChar(128) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;6&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
[DataMember(Order=1)] // Exists if serialization is turned on; used to order key parameters&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//
Emits:&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;8&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;
public static readonly TableHelper&amp;lt;Account, String&amp;gt; Accounts =&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;9&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: green"&gt;//&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp; CreateTable(dc =&amp;gt; dc.Accounts, a =&amp;gt; a.AccountName);&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;10&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;11&lt;/span&gt;&amp;nbsp;#light
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;12&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;13&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Reflection
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;14&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Data.Linq.Mapping
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;15&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;open&lt;/span&gt; System.Runtime.Serialization
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;16&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;17&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; getAttr&amp;lt;'target,
'a &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; 'a :&amp;gt; ICustomAttributeProvider&amp;gt; (ty
: 'a) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;18&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;match&lt;/span&gt; List.of_array
(ty.GetCustomAttributes(typeof&amp;lt;'target&amp;gt;, &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;)) &lt;span style="COLOR: blue"&gt;with&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;19&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
| a::_ &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; Some (a :?&amp;gt; 'target)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;20&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
| [] &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; None
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;21&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; chooseAttr&amp;lt;'target,
'a &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; 'a :&amp;gt; ICustomAttributeProvider&amp;gt; (ty
: 'a) = 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;22&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;match&lt;/span&gt; getAttr&amp;lt;'target,_&amp;gt;
ty &lt;span style="COLOR: blue"&gt;with&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;23&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
| Some(a) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; Some(ty,a)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;24&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
| None &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; None
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;25&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;26&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; joinStrings
(sep:string) items = items |&amp;gt; Seq.fold1 (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; acc
x &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; acc + sep + x) 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; pluralize
(name:string) = &lt;span style="COLOR: blue"&gt;if&lt;/span&gt; name.EndsWith(&lt;span style="COLOR: #a31515"&gt;"s"&lt;/span&gt;) &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; name &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; name
+ &lt;span style="COLOR: #a31515"&gt;"s"&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;28&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;29&lt;/span&gt;&amp;nbsp;&lt;span style="COLOR: blue"&gt;let&lt;/span&gt; generate(asmpath:string)
=
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;30&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; genTable
(t:Type, tableName) =
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;31&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; keyProps
= 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;32&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; t.GetProperties() 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;33&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.choose (chooseAttr&amp;lt;ColumnAttribute,_&amp;gt;)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;34&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.filter(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; (p,c) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; c.IsPrimaryKey)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;35&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.map(&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; (p,_) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; p,
getAttr&amp;lt;DataMemberAttribute, _&amp;gt; p)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;36&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.orderBy(&lt;span style="COLOR: blue"&gt;function&lt;/span&gt; | _,Some(dm) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; dm.Order
| _ &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; 0)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;37&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt; Seq.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; (p,_) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; p)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;38&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; tw
= &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; IO.StringWriter()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;39&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; pn
fmt = Printf.twprintfn tw fmt
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;40&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
pn &lt;span style="COLOR: #a31515"&gt;"public static readonly TableHelper&amp;lt;%s, %s&amp;gt;
%s ="&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;41&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Name 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;42&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (joinStrings &lt;span style="COLOR: #a31515"&gt;", "&lt;/span&gt; (keyProps
|&amp;gt; Seq.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; p &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; p.PropertyType.Name)))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;43&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; tableName
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;44&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
pn &lt;span style="COLOR: #a31515"&gt;"\tCreateTable(dc =&amp;gt; dc.%s, %s);"&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;45&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; tableName
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;46&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (joinStrings &lt;span style="COLOR: #a31515"&gt;", "&lt;/span&gt; (keyProps
|&amp;gt; Seq.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; p &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="COLOR: #a31515"&gt;"a
=&amp;gt; a."&lt;/span&gt; + p.Name)))
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;47&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
pn &lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;48&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
tw.ToString()
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;49&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;50&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR: blue"&gt;let&lt;/span&gt; asm
= Assembly.LoadFrom asmpath &lt;span style="COLOR: green"&gt;// Don't use ReflectionOnly
'cause it won't resolve dependencies&lt;/span&gt;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;51&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm.GetExportedTypes
() 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;52&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
|&amp;gt; Seq.choose (chooseAttr&amp;lt;TableAttribute,_&amp;gt;)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;53&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
|&amp;gt; Seq.map (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; (t,ta) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; t,
ta.Name.Replace(&lt;span style="COLOR: #a31515"&gt;"dbo."&lt;/span&gt;, &lt;span style="COLOR: #a31515"&gt;""&lt;/span&gt;)
|&amp;gt; pluralize)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;54&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
|&amp;gt; Seq.orderBy (&lt;span style="COLOR: blue"&gt;fun&lt;/span&gt; (t,_) &lt;span style="COLOR: blue"&gt;-&amp;gt;&lt;/span&gt; t.Name)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;55&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
|&amp;gt; Seq.map genTable
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;56&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
|&amp;gt; Seq.fold1 (+)
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;57&lt;/span&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;58&lt;/span&gt;&amp;nbsp;generate &lt;span style="COLOR: #a31515"&gt;"C:\\yourlinq.dll"&lt;/span&gt; 
&lt;/p&gt;
&lt;p style="MARGIN: 0px"&gt;
&lt;span style="COLOR: #2b91af"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;59&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;gt;
Console.WriteLine
&lt;/p&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.atrevido.net/blog/aggbug.ashx?id=d6d66155-c288-4984-b4ba-4e58285daf7f" /&gt;</description>
      <comments>http://www.atrevido.net/blog/CommentView,guid,d6d66155-c288-4984-b4ba-4e58285daf7f.aspx</comments>
      <category>FSharp</category>
    </item>
  </channel>
</rss>