<?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>Andrey Shchekin &#187; History</title>
	<atom:link href="http://blog.ashmind.com/category/history/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ashmind.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 18:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Simple typeswitch in C# 3.0, Part 1: The Problem</title>
		<link>http://blog.ashmind.com/2007/09/12/simple-typeswitch-in-c-30-part-1-the-problem/</link>
		<comments>http://blog.ashmind.com/2007/09/12/simple-typeswitch-in-c-30-part-1-the-problem/#comments</comments>
		<pubDate>Tue, 11 Sep 2007 22:17:36 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[History]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/09/12/simple-typeswitch-in-c-30-part-1-the-problem/</guid>
		<description><![CDATA[.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p style="font-size: 80%; color: gray">This is the first post in a two-post series on a typeswitch implementation in C#.<br />This one contains a problem statement and possible solutions in other languages.<br />The second one will contain a Switch.Type description and benchmarks. </p>
<p>Somewhat&nbsp;often I find myself writing code to do something based on runtime type of a value.<br />A classic case is to filter a tree with different types of nodes (expression tree, for example).</p>
<p>The&nbsp;code often looks like
<pre class="csharpcode"><span class="kwrd">if</span> (x <span class="kwrd">is</span> A) {
    DoWithA(x <span class="kwrd">as</span> A);
}
<span class="kwrd">else</span> <span class="kwrd">if</span> (x <span class="kwrd">is</span> B) {
    DoWithB(x <span class="kwrd">as</span> B);
}
<span class="kwrd">else</span>
    <span class="rem">// ...</span>
</pre>
<p>Or, if you are a heavy performance freak like me it is like
<pre class="csharpcode">A a = x <span class="kwrd">as</span> A;
<span class="kwrd">if</span> (a != <span class="kwrd">null</span>) {
    DoWithA(a);
    <span class="kwrd">return</span>;
}

B b = x <span class="kwrd">as</span> B;
<span class="kwrd">if</span> (b != <span class="kwrd">null</span>) {
    DoWithB(b);
    <span class="kwrd">return</span>;
}

<span class="rem">// ...</span></pre>
<p>After second type it really starts to smell.</p>
<p>I could have used a <a href="http://en.wikipedia.org/wiki/Visitor_pattern">Visitor</a>.<br />But I really dislike it due to the coupling between the Visitor interface and the underlying class hierarchy.<br />Also requires me to extend hierarchy with a zero value Accept method.</p>
<p>I can also use some kind of hashtable-based smart resolver, but it would be complex and slow.</p>
<p>Actually, that is not an obscure problem and a lot of other languages have their solutions.<br />There are two common ones:</p>
<ol>
<li>
<p>OO concept known as <em>multiple dispatch</em>.<br />Multiple dispatch is just a bunch of &#8220;method overloads&#8221; resolved by runtime environment basing on the <em>runtime</em> argument types.<br />This is quite different from ordinary method overloading — for example, in C# <em>compiler</em> picks an overloaded method during compilation.</p>
<p>Actually, .Net has a way to do multiple dispatch through Reflection (Type.InvokeMethod), but it quite slow and not compiler-type-safe.</p>
<p>There is a brilliant paper &#8220;<a href="http://homepages.cwi.nl/~ralf/JavaGI/">Generalized Interfaces for Java</a>&#8221; that gives some insight on useful multiple dispatch in Java/C#-like languages.<br />Hopefully we&#8217;ll get that functionality in C# and CLR sooner or later.</p>
<li>Functional language concept known as <a href="http://en.wikipedia.org/wiki/Pattern_matching">pattern matching</a>.<br />This is a kind of powerful switch/case statement (with a simplified syntax).<br />I do not actually know much about functional languages, so that is my understanding.</li>
</ol>
<p>The simplest possible construct (I do not want to dive into multiple dispatch) might look like this:
<pre class="csharpcode"><span class="kwrd">typeswitch</span> (x) {
   <span class="kwrd">case</span> (A a): DoWithA(a); <span class="kwrd">break</span>;
   <span class="kwrd">case</span> (B b): DoWithB(b); <span class="kwrd">break</span>;
   <span class="kwrd">default</span>: <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException();
}</pre>
</p>
<p>And lcs <a href="http://research.microsoft.com/research/pubs/view.aspx?tr_id=658">Research C# Compiler</a> has a similar syntax sample:
<pre class="csharpcode"><span class="kwrd">typeswitch</span> (o) {
    <span class="kwrd">case</span> Int32 (x): Console.WriteLine(x); <span class="kwrd">break</span>;
    <span class="kwrd">case</span> Symbol (s): Symbols.Add(s); <span class="kwrd">break</span>;
    <span class="kwrd">case</span> Segment: popSegment(); <span class="kwrd">break</span>;
    <span class="kwrd">default</span>: <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException();
}</pre>
<p><a href="http://research.microsoft.com/Comega/">Cω</a> language also had an actual typeswitch construct.<br />But I was not able to find out it&#8217;s syntax (my old VS.Net is somewhy ruined and web is silent on it).<br />Anyway, Andrey Titov (who I hope will also blog someday) reminded me that it compiled to zero IL (seems it was too experimental).</p>
<p>Stay tuned, next time we&#8217;ll see how it is possible to emulate typeswitch in C# 3.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/09/12/simple-typeswitch-in-c-30-part-1-the-problem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XHTML and microformats revisited</title>
		<link>http://blog.ashmind.com/2007/06/03/xhtml-and-microformats-revisited/</link>
		<comments>http://blog.ashmind.com/2007/06/03/xhtml-and-microformats-revisited/#comments</comments>
		<pubDate>Sat, 02 Jun 2007 21:26:25 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[History]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Markup]]></category>
		<category><![CDATA[microformats]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/06/03/xhtml-and-microformats-revisited/</guid>
		<description><![CDATA[Since my previous post on microformats, I have decided that my opinion in this matter needs more evidence. While I could collect all following information before writing the post, I didn&#8217;t have enough motivation to do the research. But now, after writing it, I have my self-esteem as a motivation. Ok, so I proposed using [...]]]></description>
			<content:encoded><![CDATA[<p>Since my <a href="http://blog.ashmind.com/index.php/2007/05/22/microformats-are-web-20-virus/">previous post</a> on microformats, I have decided that my opinion in this matter needs more evidence.<br />
While I could collect all following information <em>before</em> writing the post, I didn&#8217;t have enough motivation to do the research.<br />
But now, <em>after</em> writing it, I have my self-esteem as a motivation.</p>
<p>Ok, so I proposed using (namespaced) custom tags instead of overloading existing ones.<br />
Now let&#8217;s go scientific and see what questions this solution may rise.</p>
<ol>
<li>Do modern browsers support CSS styling for unknown tags in HTML documents?</li>
<li>Can these tags be added to document without breaking standard compliance (validity)?</li>
<li>What possible problems can arise from using non-standard tags in modern browsers?</li>
<li>&#8230;</li>
</ol>
<p>For practical purposes, these can be converted into two main questions</p>
<ol>
<li><em>Should</em> custom tags work?</li>
<li><em>Do</em> custom tags work in modern browsers?</li>
</ol>
<p>And the answers are:</p>
<ol>
<li>By default, no.</li>
<li>Not perfectly, but yes.</li>
</ol>
<p>Now let&#8217;s discuss it in detail.</p>
<p>To understand the first answer is to understand what exactly is HTML, what is XML and what is XHTML.<br />
The most important (maybe obvious) point is: HTML is not a subset of XML and HTML is not compatible with XML.<br />
HTML and XML are both a subsets of SGML, and SGML does not provide a way to mix different subsets within a single document.<br />
So custom XML tags are not allowed in a HTML document.</p>
<p>While there are some solutions that allow arbitrary XML to be placed in a HTML document.<br />
For example, Microsoft has <a href="http://msdn2.microsoft.com/en-us/library/ms766512.aspx">XML Data Islands</a>.<br />
But they can be considered grammar hacks due to XML-HTML incompatibility.</p>
<p>Practically, however, HTML documents have to be viewed as &#8220;tag soup&#8221; by the browsers, so custom tags do not cause document rendering to fail.</p>
<p>So, if I am formally out of luck with HTML, what about XHTML?<br />
For simplicity, one can view XHTML is a rewrite of HTML to follow XML rules.<br />
So any custom tags should be allowed in XHTML if they are properly namespaced.</p>
<p>But there are a lot of problems with authoring XHTML.<br />
While some of them are more like challenges (script/style syntax), one is extremely important.<br />
The only way to tell modern browsers that that the document is XHTML is to serve it as application/xhtml+xml<br />
(See <a href="http://www.hixie.ch/advocacy/xhtml">this document</a> for an excellent explanation).<br />
And Internet Explorer <a href="http://blogs.msdn.com/ie/archive/2005/09/15/467901.aspx">doesn&#8217;t support XHTML</a> at all — so it refuses to render application/xhtml+xml.<br />
(It doesn&#8217;t mean IE can&#8217;t open XHTML. When XHTML document is sent as text/html, IE renders it with HTML engine).<br />
So I was out of luck once again.</p>
<p>At that point I understood the reasoning of microformats.<br />
Standard compliance is an important part of better Web, and there is no completely valid way to use custom tags.</p>
<p>But what is with the second question? It seems that actual situation is way better than one could suppose.<br />
Firefox, IE7 and Opera 9 all could render the custom tags style correctly in the document served as text/html.<br />
(To be really pedantic, I set DTD and xmlns to XHTML.<br />
After all, even if text/html documents are never parsed as XHTML, MIME Type is a server setting, not document one.)<br />
But IE7 has a one important characteristic — it does not render custom tag styles unless there is an xmlns for their namespace on html tag.<br />
No other tag is sufficient.</p>
<p>What does it mean? It means that while one can make a document that is styled correctly in these IE7,<br />
document part containing custom tags can not be reused without providing a namespace on the aggregating document.<br />
But it not an extremely important point, since for aggreagation one does not actually control styles as well.</p>
<p>So, practically speaking, one can create a document that uses custom XML tags for the cost of formal document validity.<br />
(The document can still be made formally valid by using custom DTD, but this will put IE and FF into <a href="http://www.quirksmode.org/">quirks mode</a>).</p>
<p>By the way, the challenge of adding custom tags to HTML was faced by MathML (mathematical markup language) community for years.<br />
If you are interested, you can read these discussions:</p>
<ul>
<li><a href="http://groups.google.ru/group/netscape.public.mozilla.mathml/browse_frm/thread/3c8bf8b155267234">Cannot render MathML — netscape.public.mozilla.mathml</a> (2001)</li>
<li><a href="http://groups.google.com/group/mozilla.dev.tech.mathml/browse_thread/thread/796fad03fdd4fc59/2b6952b15cd6a7d6">MathML-in-HTML5 — mozilla.dev.tech.mathml</a> (2006)</li>
</ul>
<p>Personally, I still see microformats as a step in wrong direction.<br />
While hCard provides HTML with a way to express the vCard semantics, I would prefer it to be just a HTML-compatible way, not the recommended one.<br />
I see HTML as standard that needs support, but not popularized extensions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/06/03/xhtml-and-microformats-revisited/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

