<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Andrew Kennedy's Blog : Generics</title><link>http://blogs.msdn.com/andrewkennedy/archive/tags/Generics/default.aspx</link><description>Tags: Generics</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Units of Measure in F#: Part Three, Generic Units</title><link>http://blogs.msdn.com/andrewkennedy/archive/2008/09/04/units-of-measure-in-f-part-three-generic-units.aspx</link><pubDate>Thu, 04 Sep 2008 13:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8885070</guid><dc:creator>andrewkennedy</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/andrewkennedy/comments/8885070.aspx</comments><wfw:commentRss>http://blogs.msdn.com/andrewkennedy/commentrss.aspx?PostID=8885070</wfw:commentRss><description>&lt;P&gt;In the first two articles in this series, we saw how to declare base and derived units, introduce constants with units, define our own conversion functions, and have F# check the units for us.&lt;/P&gt;
&lt;P&gt;But what if we're writing code that doesn't care what units it is working in? That's the subject of today's article.&lt;/P&gt;
&lt;P&gt;Let's start simple. What is the type of &lt;FONT face="courier new"&gt;fun x -&amp;gt; x*x&lt;/FONT&gt;? Well, multiplication is overloaded, so F# defaults to integers:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_2.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb.png" width=173 height=82 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;But if we annotate the argument, we can define squaring for all kinds of floats (hovering over the last of these to show its type):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_6.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_2.png" width=320 height=121 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;This looks painful! What if we don't tell F# what the units are, using the underscore notation to say "find me its units"?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_8.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_3.png" width=263 height=83 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;F# has inferred a &lt;EM&gt;generic&lt;/EM&gt; unit type for squaring. The notation &lt;FONT face="courier new"&gt;'u&lt;/FONT&gt; looks like a type parameter, but is in fact a &lt;EM&gt;unit-of-measure &lt;/EM&gt;parameter that can be instantiated at any units. Let's use it on masses, lengths and speeds:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_10.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_4.png" width=216 height=115 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_4.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;F# can infer generic unit-of-measure types with type annotations required only to resolve overloading. Here are some simple examples, tested out in F# Interactive:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_12.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_12.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_5.png" width=378 height=176 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_5.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Here's one that requires a bit of head-scratching to understand:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_14.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_14.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_6.png" width=396 height=80 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_6.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Returning to reality, suppose we want to sum the elements of a list. In true functional style, we use one of the &lt;A href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29" mce_href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29"&gt;fold&lt;/A&gt; operators.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_16.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_16.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_7.png" width=336 height=80 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_7.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Oops - we don't seem to have a nice generic type! The reason is simple: unless units are specified, constants, including zero, are assumed to have no units, i.e. to be dimensionless. So instead, let's give &lt;FONT face="courier new"&gt;0.0&lt;/FONT&gt; some units, but not tell F# what they are, by writing &lt;FONT face="courier new"&gt;0.0&amp;lt;_&amp;gt;&lt;/FONT&gt;:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_18.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_18.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_8.png" width=343 height=78 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_8.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's better!&lt;/P&gt;
&lt;P&gt;Now we can go wild and write all sorts of statistical stuff.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_20.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_20.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_9.png" width=811 height=409 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_9.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;And here are the beautiful types that are inferred for these functions:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_22.png" mce_href="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_22.png"&gt;&lt;IMG style="MARGIN: 0px 0px 0px 25px" border=0 alt=image src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_10.png" width=622 height=185 mce_src="http://blogs.msdn.com/blogfiles/andrewkennedy/WindowsLiveWriter/UnitsofMeasureinFPartTwoGenericUnits_D89F/image_thumb_10.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Summing up: &lt;/STRONG&gt;we've seen how to write &lt;EM&gt;code&lt;/EM&gt; that is &lt;EM&gt;generic &lt;/EM&gt;(a.k.a. as &lt;EM&gt;polymorphic&lt;/EM&gt;) in units-of-measure.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Next time&lt;EM&gt;, &lt;/EM&gt;&lt;/STRONG&gt;we'll write &lt;EM&gt;types&lt;/EM&gt; that are generic in units-of-measure. This makes the feature &lt;EM&gt;extensible&lt;/EM&gt;: if &lt;FONT face="courier new"&gt;float&lt;/FONT&gt;s with units aren't what you want, just define your own types!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8885070" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/F_2300_/default.aspx">F#</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units+of+Measure/default.aspx">Units of Measure</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Types/default.aspx">Types</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Units/default.aspx">Units</category><category domain="http://blogs.msdn.com/andrewkennedy/archive/tags/Generics/default.aspx">Generics</category></item></channel></rss>