<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>voyce &#187; event</title>
	<atom:link href="http://www.voyce.com/index.php/tag/event/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voyce.com</link>
	<description>Programming and debugging tidbits</description>
	<lastBuildDate>Sat, 03 Jul 2010 12:40:51 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Implementing INotifyPropertyChanged with F#</title>
		<link>http://www.voyce.com/index.php/2009/03/02/implementing-inotifypropertychanged-with-fsharp/</link>
		<comments>http://www.voyce.com/index.php/2009/03/02/implementing-inotifypropertychanged-with-fsharp/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 12:15:29 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=90</guid>
		<description><![CDATA[I like F# for a lot of things, but, man, is it a pain to support events. In C# it&#8217;s trivial to implement an interface like INotifyPropertyChanged consisting only of an event, but in F# you have to jump through some hoops to map native functions to delegates/events. F# is generally much terser than C# [...]]]></description>
			<content:encoded><![CDATA[<p>I like F# for a lot of things, but, man, is it a pain to support events. In C# it&#8217;s trivial to implement an interface like INotifyPropertyChanged consisting only of an event, but in F# you have to jump through some hoops to map native functions to delegates/events. F# is generally much terser than C# and other .NET languages, but not in this case. After spending some time the other day trying to figure out the right combination of syntax and helper functions (and unsucessfully googling for it), I thought I&#8217;d upload a bare-bones implementation here as an aide-memoire.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #000080;">.</span><span style="color: #505090;">ComponentModel</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> MyObject<span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">mutable</span> propval <span style="color: #000080;">=</span> <span style="color: #c6c;">0.0</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> event <span style="color: #000080;">=</span> Event<span style="color: #000080;">&lt;</span>_, _<span style="color: #000080;">&gt;</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">interface</span> INotifyPropertyChanged <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #06c; font-weight: bold;">member</span> this<span style="color: #000080;">.</span><span style="color: #505090;">add_PropertyChanged</span><span style="color: #000080;">&#40;</span>e<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
            event<span style="color: #000080;">.</span><span style="color: #505090;">Publish</span><span style="color: #000080;">.</span><span style="color: #505090;">AddHandler</span><span style="color: #000080;">&#40;</span>e<span style="color: #000080;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">member</span> this<span style="color: #000080;">.</span><span style="color: #505090;">remove_PropertyChanged</span><span style="color: #000080;">&#40;</span>e<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
            event<span style="color: #000080;">.</span><span style="color: #505090;">Publish</span><span style="color: #000080;">.</span><span style="color: #505090;">RemoveHandler</span><span style="color: #000080;">&#40;</span>e<span style="color: #000080;">&#41;</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">member</span> this<span style="color: #000080;">.</span><span style="color: #505090;">MyProperty</span>
        <span style="color: #06c; font-weight: bold;">with</span> get<span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span> propval
        <span style="color: #06c; font-weight: bold;">and</span>  set<span style="color: #000080;">&#40;</span>v<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
            propval <span style="color: #000080;">&lt;-</span> v
            event<span style="color: #000080;">.</span><span style="color: #505090;">Trigger</span><span style="color: #000080;">&#40;</span>this, <span style="color: #06c; font-weight: bold;">new</span> PropertyChangedEventArgs<span style="color: #000080;">&#40;</span><span style="color: #008080;">&quot;MyProperty&quot;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span></pre></div></div>

<p>It turns out that in F# version 1.9.6.16 there&#8217;s a slightly more concise syntax for this, as pointed out by Rei in the comments (thanks!). It uses the CLIEvent attribute to hook up the .NET event:</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #000080;">.</span><span style="color: #505090;">ComponentModel</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> MyObject<span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">mutable</span> propval <span style="color: #000080;">=</span> <span style="color: #c6c;">0.0</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">let</span> propertyChanged <span style="color: #000080;">=</span> Event<span style="color: #000080;">&lt;</span>_, _<span style="color: #000080;">&gt;</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">interface</span> INotifyPropertyChanged <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #000080;">&#91;</span><span style="color: #000080;">&lt;</span>clievent<span style="color: #000080;">&gt;</span><span style="color: #000080;">&#93;</span>
        <span style="color: #06c; font-weight: bold;">member</span> x<span style="color: #000080;">.</span><span style="color: #505090;">PropertyChanged</span> <span style="color: #000080;">=</span> propertyChanged<span style="color: #000080;">.</span><span style="color: #505090;">Publish</span>
&nbsp;
    <span style="color: #06c; font-weight: bold;">member</span> this<span style="color: #000080;">.</span><span style="color: #505090;">MyProperty</span>
        <span style="color: #06c; font-weight: bold;">with</span> get<span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span> propval
        <span style="color: #06c; font-weight: bold;">and</span>  set<span style="color: #000080;">&#40;</span>v<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
            propval <span style="color: #000080;">&lt;-</span> v
            propertyChanged<span style="color: #000080;">.</span><span style="color: #505090;">Trigger</span><span style="color: #000080;">&#40;</span>this, <span style="color: #06c; font-weight: bold;">new</span> PropertyChangedEventArgs<span style="color: #000080;">&#40;</span><span style="color: #008080;">&quot;MyProperty&quot;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2009/03/02/implementing-inotifypropertychanged-with-fsharp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
