<?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>rel=me &#187; C#</title>
	<atom:link href="http://rel.me/c/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Mon, 02 Aug 2010 20:09:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>On duck typing and interfaces</title>
		<link>http://rel.me/2008/01/02/on-duck-typing-and-interfaces/</link>
		<comments>http://rel.me/2008/01/02/on-duck-typing-and-interfaces/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 00:59:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[duck typing]]></category>
		<category><![CDATA[http]]></category>

		<guid isPermaLink="false">/2008/04/01/on-duck-typing-and-interfaces</guid>
		<description><![CDATA[I was looking at different type systems (apparently there are 4 dimensions) in different languages and ran across this use of duck typing in C# : For example, the C#&#8217;s foreach operator already uses duck typing. This might be surprising to some, but to support foreach in C# you don&#8217;t need to implement IEnumerable! All [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking at different type systems (apparently there are <a href="http://programming.reddit.com/info/63tnv/comments/c02qx55">4 dimensions</a>) in different languages and ran across this use of <a href="http://blogs.msdn.com/kcwalina/archive/2007/07/18/DuckNotation.aspx">duck typing in C#</a> :</p>
<blockquote><p>
For example, the C#&#8217;s foreach operator already uses duck typing. This might be surprising to some, but to support foreach in C# you don&#8217;t need to implement IEnumerable! All you have to do is:<br/><br />
<br/><br />
Provide a public method GetEnumerator that takes no parameters and returns a type that has two members: a) a method MoveMext that takes no parameters and return a Boolean, and b) a property Current with a getter that returns an Object.
</p></blockquote>
<p>Do you have multiple fine-grained interfaces (like IEnumerable and IEnumerator and ISupportsAdd) or a single interface with all the methods and maybe they throw UnsupportedOperationExceptions if they don&#8217;t actually support a particular call (like in a mutable versus immutable collection)?</p>
<p>The closer you get to more fine grained interfaces like <tt>ISupportsAdd&lt;T&gt;</tt> or <tt>Closeable</tt>, defining a contract for a single method <tt>Add(T obj)</tt> or <tt>Close</tt> is that you might as well just ask the class if it has that method (if it &#8220;quacks&#8221;) instead of whether it implemented a single method interface. Better yet, just assume it and let it throw an error at runtime. Whatever the compiler doesn&#8217;t give you, some decent test coverage will.</p>
<p>But what strikes me as particularly great is that <tt>foreach</tt> is obviously such a powerful thing to restrict to a single interface, and so they chose &#8220;Duck Notation&#8221;, because in this case it is worth it.</p>
<p>This reminded me of why I like ecma 4 (and its derivatives like actionscript). You have freedom to use static and dynamic typing at will.</p>
<p>If you check out my take on an AS3 HTTP client library, <a href="http://as3httpclientlib.googlecode.com/svn/trunk/src/org/httpclient/HttpRequest.as">HttpRequest</a> class and allowing the request body to be of any type:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span class="kw3">public</span> <span class="kw2">class</span> HttpRequest <span class="br0">&#123;</span>
&nbsp;
  <span class="co1">// Request method. For example, &quot;GET&quot;</span>
  protected <span class="kw2">var</span> _method:<span class="kw3">String</span>;
&nbsp;
  <span class="co1">// Request header</span>
  protected <span class="kw2">var</span> _header:HttpHeader;
&nbsp;
  <span class="co1">// Request body</span>
  protected <span class="kw2">var</span> _body:<span class="sy0">*</span>;
&nbsp;
  <span class="coMULTI">/**
   * Create request.
   *  
   * The request body can be anything but should respond to:
   *  - readBytes(bytes:ByteArray, offset:uint, length:uint)
   *  - length
   *  - bytesAvailable
   *  - close
   *  
   * @param method HTTP Method, for example 'GET'
   * @param header HTTP request header (or null)
   * @param body HTTP request body (or null)
   *  
   */</span>
  <span class="kw3">public</span> <span class="kw2">function</span> HttpRequest<span class="br0">&#40;</span>method:<span class="kw3">String</span>, header:HttpHeader = <span class="kw2">null</span>, body:<span class="sy0">*</span> = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    _method = method;
    _header = header;      
    _body = body;
&nbsp;
 ...</pre></div></div>

<p>I think this is a good choice because:</p>
<ol>
<li>The <a href="http://livedocs.adobe.com/flex/201/langref/flash/utils/IDataInput.html">IDataInput</a> interface (that ByteArray already implements) doesn&#8217;t have a length property and we need to know the request body size (Content-Length) before we send.</li>
<li>The IDataInput interface wants you to implement a ton of read methods, which would make it annoying to force people to implement.</li>
<li>ByteArray has these methods and also implements the length property already. It quacks.</li>
<li>If we created our own interface with these methods, we can&#8217;t add this interface retroactively to ByteArray.</li>
<li>I wrote it. Heh.</li>
</ol>
<p>The other option is to subclass ByteArray and have that implement the new interface, but it just seems awkward (in this case) having any empty subclass solely for the point of enforcing an interface.</p>
<p>Another place where this principle applied for me is in <a href="http://as3httpclientlib.googlecode.com/svn/trunk/src/org/httpclient/HttpSocket.as">HttpSocket</a>. We need to support both Socket (for http) and TLSSocket (for https). TLSSocket has all the same (mostly) methods as Socket (but does not extend Socket; yay for composition over inheritance). Creating a proxy to delegate to different socket implementations based on the http scheme is an option but to me wasn&#8217;t worth it. And using a design pattern to overcome the lack of an appropriate and existing interface just seems to make things worse.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/01/02/on-duck-typing-and-interfaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Open sourced (Flickr OpenGL C# Library)</title>
		<link>http://rel.me/2007/03/20/open-sourced-flickr-opengl-c-library/</link>
		<comments>http://rel.me/2007/03/20/open-sourced-flickr-opengl-c-library/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 02:06:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[tao]]></category>

		<guid isPermaLink="false">/2007/09/03/open-sourced-flickr-opengl-c-library</guid>
		<description><![CDATA[Crossposted from cellardoorsw.com: I GPL licensed all the source to Slickr (that I wrote), and released it into the wild. You can view the trac page, or go straight to the source. Its all C# and OpenGL goodness with some gnarly calls to some windows api&#8217;s (for extra&#8217;s nothing critical). There are a couple NAnt [...]]]></description>
			<content:encoded><![CDATA[<p>Crossposted from <a href="http://cellardoorsw.com/">cellardoorsw.com</a>:</p>
<p>I GPL licensed all the source to Slickr (that I wrote), and released it into the wild. You can view the <a href="http://trac.ducktyper.com/slickr/">trac page</a>, or go straight to the <a href="http://svn.ducktyper.com/slickr">source</a>. Its all C# and OpenGL goodness with some gnarly calls to some windows api&#8217;s (for extra&#8217;s nothing critical). There are a couple NAnt build scripts for when I was trying to get it working under linux, but you&#8217;ll want Visual Studio (2003) to get started quick (and look at the <a href="http://svn.ducktyper.com/slickr/trunk/README">README</a>).<br />
Who knew it would be such a pain to draw <a href="http://svn.ducktyper.com/slickr/trunk/Source/GlFont2.cs">text</a> and did I really need to query the <a href="http://svn.ducktyper.com/slickr/trunk/Source/Util/Windows/WindowsUtils.cs">monitor power state</a>?</p>
<p>I&#8217;ll try to write an overall design doc soon, but until then start at any of the <a href="http://svn.ducktyper.com/slickr/trunk/ScreenSaver/Entrypoint.cs">Entrypoint</a>&#8216;s. And in the end all that is really going on is this:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">private void DrawGLTexture(SlickrImage image)<tt>
</tt>{<tt>
</tt>  PointFloat p = image.GetMovement(window.Width, window.Height, true);<tt>
</tt>  float scale = image.GetScale(window.Width, window.Height);<tt>
</tt>  float fade = image.GetPercentageFade();<tt>
</tt>  int texWidth = image.TextureWidth;<tt>
</tt>  int texHeight = image.TextureHeight;<tt>
</tt><tt>
</tt>  Gl.glMatrixMode(Gl.GL_PROJECTION);                                  // Select The Projection Matrix<tt>
</tt>  Gl.glLoadIdentity();                                                // Reset The Projection Matrix<tt>
</tt>  float r = (window.Width/scale);<tt>
</tt>  float b = (window.Height/scale);<tt>
</tt><tt>
</tt>  Gl.glOrtho(0, r, b, 0, -1.0f, 1.0f);<tt>
</tt>  Gl.glMatrixMode(Gl.GL_MODELVIEW);                                   // Select The Modelview Matrix<tt>
</tt>  Gl.glLoadIdentity();<tt>
</tt><tt>
</tt>  Gl.glEnable (Gl.GL_BLEND); // for text fading<tt>
</tt>  Gl.glBlendFunc (Gl.GL_ONE, Gl.GL_ONE_MINUS_SRC_ALPHA);<tt>
</tt>  Gl.glColor4f (fade, fade, fade, fade);<tt>
</tt><tt>
</tt>  float posX = p.x; //stepX +<tt>
</tt>  float posY = p.y; //stepY +<tt>
</tt><tt>
</tt>  image.DrawAtPoint(posX, posY);<tt>
</tt><tt>
</tt>  Gl.glDisable(Gl.GL_BLEND);<tt>
</tt>}</pre>
</td>
</tr>
</table>
<p>If you are interesting in working on bug fixes, enhacements, or whatever give me a <a href="mailto:gabrielh@gmail.com">shout.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/03/20/open-sourced-flickr-opengl-c-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
