<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-12502111</id><updated>2012-01-05T14:50:29.829+01:00</updated><category term='yield'/><category term='jumps'/><category term='return'/><category term='operator'/><category term='declaration'/><category term='initializer'/><category term='creation'/><category term='debugging'/><category term='literal'/><category term='constant'/><category term='field'/><category term='property'/><category term='genericity'/><category term='ranges'/><category term='chaining'/><category term='implementation'/><category term='event'/><category term='method'/><category term='syntax'/><category term='typecast'/><category term='array'/><category term='constructor'/><category term='style'/><category term='interface'/><category term='C#'/><category term='indexer'/><category term='iterator'/><category term='Lyra'/><category term='member'/><category term='functional programming'/><category term='optimization'/><category term='locals'/><category term='boolean'/><title type='text'>The Freya Programming Language</title><subtitle type='html'>Freya is a new object oriented programming language designed for the .NET Framework.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>35</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-12502111.post-101879826391895667</id><published>2008-09-07T15:41:00.033+02:00</published><updated>2008-09-14T01:56:00.534+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='syntax'/><category scheme='http://www.blogger.com/atom/ns#' term='Lyra'/><title type='text'>Enter Lyra</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1px 0 1px 8px;border-style:none;padding:0px;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/SMPaI1vVuuI/AAAAAAAAAJA/1yf7Ez58hJ0/s320/lyra01.jpg" border="0" alt="Stringed instruments, get it?" /&gt;&lt;span class="fline"&gt;Let me state this loud&lt;/span&gt; and clear: I'm proud of Freya and the Freya compiler. I think my goals, or almost all of them, has been achieved: there's a working implementation of a powerful general programming language, running on a powerful interesting framework. What's more: I have great plans for Freya, including the addition of some interesting features not found in similar languages.&lt;/div&gt;&lt;div&gt;There's a catch, however: Freya is a complex language, requiring a complex implementation. And I'm virtually alone, by my own decision, developing the language, the compiler and a simple IDE for testing... and that shows. For instance: almost all relevant features from C&lt;sup&gt;#&lt;/sup&gt; 2.0 has already been included and implemented in Freya, and we already have some features from C&lt;sup&gt;#&lt;/sup&gt; 3.0, as extension methods, and object and collection initializers. Nevertheless, there are some equally important features still missing, such as anonymous methods and complex type inference for lambdas (type inference for generic method calls, however, is already working). And the worst part is that we'll need to rewrite the type attribution from the compiler in order to accommodate the powerful inference engine required for these enhancements.&lt;/div&gt;&lt;hr class="p"&gt;&lt;div&gt;So enter &lt;b&gt;Lyra&lt;/b&gt; (pronounced as &lt;i&gt;lie-rah&lt;/i&gt;). Lyra is a fully developed and independent programming language derived from Freya. Its syntax is almost identical to Freya's, but we have introduced some simplifications, in order to make it easier to understand and implement. These are some of the differences:&lt;/div&gt;&lt;ol&gt;&lt;li&gt;Implementation in Lyra takes place along with declaration. No more separated &lt;b&gt;implementation for&lt;/b&gt; sections, for including executable content.&lt;/li&gt;&lt;li&gt;Property and event implementations have changed accordingly. The new implementation syntax is very compact and easy to understand, and uses four new special "keywords": &lt;b&gt;get:&lt;/b&gt;, &lt;b&gt;set:&lt;/b&gt;, &lt;b&gt;add:&lt;/b&gt; and &lt;b&gt;remove:&lt;/b&gt;. Semicolons are an integral part of the corresponding tokens, and they avoid both those dirty contextual keywords and disturbing valid identifiers in existing code.&lt;/li&gt;&lt;li&gt;Interface delegation is supported, but it has moved to the implementing member declaration.&lt;/li&gt;&lt;li&gt;Constructor calls now requires parentheses, except when followed by a list initializer.&lt;/li&gt;&lt;li&gt;In compensation, constructor calls can now start an object path.&lt;/li&gt;&lt;li&gt;Local variable declarations have been moved to the statement list, as declaration statements. This trick avoids some syntactic anomalies while defining properties and events.&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Let's see an example showing how to write properties in Lyra:&lt;/div&gt;&lt;pre&gt;&lt;b&gt;property&lt;/b&gt; Items[Index: &lt;font color="darkcyan"&gt;Integer&lt;/font&gt;]: &lt;font color="darkcyan"&gt;Boolean&lt;/font&gt;;&lt;br /&gt;&lt;b&gt;get:&lt;/b&gt;&lt;br /&gt;    &lt;i&gt;// Compound assignments are not expressions.&lt;/i&gt;&lt;br /&gt;    Index -= Min;&lt;br /&gt;    &lt;i&gt;// &lt;b&gt;then&lt;/b&gt; is optional before a keyword.&lt;/i&gt;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; Index &amp;lt; 0 &lt;b&gt;or&lt;/b&gt; Index &amp;gt; High&lt;br /&gt;        &lt;b&gt;raise new&lt;/b&gt; &lt;font color="darkcyan"&gt;ArgumentException&lt;/font&gt;("Index");&lt;br /&gt;    &lt;i&gt;// Symbolic and literal operators are both allowed.&lt;/i&gt;&lt;br /&gt;    Result := (Bits[Index &lt;b&gt;div&lt;/b&gt; 32] &amp;&lt;br /&gt;        (1 &amp;lt;&amp;lt; (Index &lt;b&gt;mod&lt;/b&gt; 32))) &amp;lt;&amp;gt; 0&lt;br /&gt;&lt;b&gt;set:&lt;/b&gt;&lt;br /&gt;    Index -= Min;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; Index &amp;lt; 0 &lt;b&gt;or&lt;/b&gt; Index &amp;gt; High&lt;br /&gt;        &lt;b&gt;raise new&lt;/b&gt; &lt;font color="darkcyan"&gt;ArgumentException&lt;/font&gt;("Index");&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; Value &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;        Bits[Index &lt;b&gt;div&lt;/b&gt; 32] |= 1 &amp;lt;&amp;lt; (Index &lt;b&gt;mod&lt;/b&gt; 32)&lt;br /&gt;    &lt;b&gt;else&lt;/b&gt;&lt;br /&gt;        Bits[Index &lt;b&gt;div&lt;/b&gt; 32] &amp;= ~(1 &amp;lt;&amp;lt; (Index &lt;b&gt;mod&lt;/b&gt; 32))&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;&lt;/pre&gt;&lt;div&gt;Of course, we can still use most of the Freya tricks, such as expression-based methods and properties (and operators, and iterators), the abbreviated syntax for constructor chains, implicitly implemented properties (I think Freya/Lyra's trick is better than C&lt;sup&gt;#&lt;/sup&gt;'s). Assertions are fully supported, even when declared in an interface type. Numeric types are still implicitly extended with static members from the &lt;i&gt;Math&lt;/i&gt; class. Lyra, in other words, is as powerful as Freya... but probably more elegant, too.&lt;/div&gt;&lt;hr class="p"&gt;&lt;div&gt;In the next months, all our research and implementation effort will concentrate on Lyra. These are some of our planned tasks:&lt;/div&gt;&lt;ul&gt;&lt;li&gt;Anonymous methods and lambdas are still missing. Probably, I'll implement only one of the syntactic varieties, but I still have some troubles with the syntax: C#'s lambdas, when implemented verbatim, don't make a valid LALR(1) grammar.&lt;/li&gt;&lt;li&gt;I still believe in the usefulness of &lt;i&gt;access sentinels&lt;/i&gt;: wrapping code that would be inserted by the compiler around methods that access some class members. This is a safe form of Aspect Oriented Programming (and no, properties don't do the same trick!)&lt;/li&gt;&lt;li&gt;Lyra will feature mixins declared with interfaces. You'll be able to add common implementation methods to interfaces, as in this example:&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;font color="darkcyan"&gt;IStack&lt;/font&gt; = &lt;b&gt;interface&lt;/b&gt;[X]&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Count: &lt;font color="darkcyan"&gt;Integer&lt;/font&gt;; &lt;b&gt;readonly&lt;/b&gt;;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Push(Value: X);&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Pop: X;&lt;br /&gt;&lt;br /&gt;    &lt;i&gt;// These are the new "mixins":&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; IsEmpty: &lt;font color="darkcyan"&gt;Boolean&lt;/font&gt; =&amp;gt;&lt;br /&gt;        Count = 0;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Push(Values: &lt;font color="darkcyan"&gt;IEnumerable&lt;/font&gt;[X]);&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;for&lt;/b&gt; v &lt;b&gt;in&lt;/b&gt; Values &lt;b&gt;do&lt;/b&gt; Push(v)&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Clear;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;while&lt;/b&gt; Count &gt; 0 &lt;b&gt;do&lt;/b&gt; Pop&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;It's some kind of funny that extension methods (that's what mixins are, at the end of the day), access sentinels and assertions are related by their implementations. For instance, pre and postconditions are implemented in Freya as extension methods. Class invariants' implementation, on the other hand, uses a similar approach as data sentinels to avoid firing in internal call boundaries.&lt;/li&gt;&lt;li&gt;I must acknowledge that most assertion implementations I know, beyond Eiffel, are shallow, incomplete or plainly wrong. Most of them don't handle inheritance and don't support assertion on interface types. Assertions in Freya addressed these problems, and Lyra will continue enhancing assertions support.&lt;/li&gt;&lt;li&gt;I have to complete the "Intellisense" compiler. This is a second (lighter) compiler that must take care of class navigation and code completion. I can't use the main compiler: it performs some code generation tasks very early in the compiling process.&lt;/li&gt;&lt;li&gt;Of course, Visual Studio integration is still an important item in the shopping list, just as CodeDOM support is.&lt;/li&gt;&lt;li&gt;Compiling must be moved to an isolated application domain, in order to avoid problems when unloading compiled assemblies.&lt;/li&gt;&lt;li&gt;Dynamic methods and "dynamic programming" support, you said? Huh, I hate the very concept! But you were kidding, weren't you?&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Let me insist, anyway: Freya development hasn't been abandoned. As soon as Lyra moves to the next level, we'll update Freya in order to keep the pace.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-101879826391895667?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/101879826391895667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=101879826391895667' title='12 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/101879826391895667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/101879826391895667'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2008/09/enter-lyra.html' title='Enter Lyra'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/SMPaI1vVuuI/AAAAAAAAAJA/1yf7Ez58hJ0/s72-c/lyra01.jpg' height='72' width='72'/><thr:total>12</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-7139817094074252690</id><published>2008-09-07T14:11:00.012+02:00</published><updated>2008-09-07T15:00:17.653+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax'/><title type='text'>Mean and lean</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1px 0 1px 8px;border-style:none;padding:0px;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/SMPQSta5GMI/AAAAAAAAAI4/MvF1naZrGyQ/s320/csharp.jpg" border="0" alt="" /&gt;&lt;span class="p"&gt;Wouldn't be a good thing if&lt;/span&gt; you were allowed to define functions like these in C#?&lt;/div&gt;&lt;pre&gt;&lt;b&gt;public long&lt;/b&gt; fact(&lt;b&gt;int&lt;/b&gt; n) =&amp;gt;&lt;br /&gt;    n &amp;lt;= 1 ? 1 : n * fact(n - 1);&lt;/pre&gt;&lt;pre&gt;&lt;b&gt;operator&lt;/b&gt;+(Complex c1, Complex c2) =&amp;gt;&lt;br /&gt;    &lt;b&gt;new&lt;/b&gt; Complex(c1.Re + c2.Re, c1.Im + c2.Im);&lt;/pre&gt;&lt;div&gt;Actually, you are allowed even riskier tricks when dealing with inline lambdas. The uniformity principle states that, at least, some of these tricks should be accepted while defining regular functions. And that's what Freya does.&lt;/div&gt;&lt;div&gt;By the way, there are no omissions in the operator definition... by Freya standards, of course. Operators are always public and static, so why bother the user with stating this each and every time. On the other hand, the return type has been omitted: it can be statically deduced from the defining expression. Freya has precise rules for these static inferences: you can omit the return type when the expression is a constructor call, a typecast or, recursively, when it's a common expression block wrapping an expression with static inferable return type or a conditional with identical inferred types in both of its branches.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-7139817094074252690?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/7139817094074252690/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=7139817094074252690' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7139817094074252690'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7139817094074252690'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2008/09/mean-and-lean.html' title='Mean and lean'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/SMPQSta5GMI/AAAAAAAAAI4/MvF1naZrGyQ/s72-c/csharp.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-1604154318410288977</id><published>2007-11-21T21:45:00.000+01:00</published><updated>2008-12-11T19:24:56.564+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='jumps'/><title type='text'>Jump!</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1px 0 1px 8px;border-style:none;padding:0px;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/R0SY7LeLwcI/AAAAAAAAAF4/5xuj8Xn9yxo/s320/twisted.jpg" border="0" alt="" /&gt;&lt;span class="p"&gt;A fair share of the algorithms&lt;/span&gt; implemented by the peephole optimizer has to do with jump optimization. The compiler has already been carefully crafted to produce the best possible code before reaching this pass, but some optimizations may reintroduce small inefficiencies, that must be solved one way or another.&lt;/div&gt;&lt;div&gt;This is a summary of the tasks performed by our optimizer:&lt;/div&gt;&lt;ol start="1" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;strong&gt;Chained jumps detection&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;A chained jump is a branch instruction that jumps into an unconditional branch. In some pathological cases, the chain may extend through several jumps. When the optimizer detects such a chain, it substitutes all intermediate targets by the target at the end of the chain. We must be careful in order to avoid circular jumps.&lt;/div&gt;&lt;ol start="2" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Jumps to return&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;An unconditional branch may land on a return operation code. Execution time is saved by substituting the original branch with a return.&lt;/div&gt;&lt;ol start="3" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;strong&gt;Common branch suffixes&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;As already explained &lt;a href="http://freyalang.blogspot.com/2007/11/common-branch-suffixes.html"&gt;here&lt;/a&gt;, this is a space-saving optimization that detects repeated code at the end of converging executing paths. Since time is more important than space, this pass is executed after jump to return detection.&lt;/div&gt;&lt;ol start="4" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Unreachable code detection&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;Code is symbolically executed, marking all instructions reached by some execution path. Then, all non-marked instructions are deleted.&lt;/div&gt;&lt;ol start="5" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Trivial jumps removal&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;As a side effect of the previous pass, code is compacted and some trivial jump patterns may be introduced. For instance, we could have a jump to the immediately next instruction in the code stream, or a conditional branch skipping an unconditional branch, which could be substituted by a single inverted conditional branch.&lt;/div&gt;&lt;ol start="6" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Peephole optimization, properly speaking&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;Now we scan the instruction stream searching for some short code sequences that can be simplified. For instance, we look for consecutive assignments on the same field, leaving just the last assignment. Consecutive assignments may be generated by automatic transformations, such as the algorithm for generating iterators. If a local variable is written and then read, and if this is the only use of that local variable, we can drop both the read and the write operations. This seemingly silly pattern is frequently generated by &lt;b&gt;for&lt;/b&gt;/&lt;b&gt;in&lt;/b&gt; loops.&lt;/div&gt;&lt;ol start="7" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Tail recursion&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;Then, we look for recursive calls before a return operation code. When some security conditions are met, we scan back the code that pushes the parameters for that call, in order to modify the arguments in the current stack frame, and then we substitute the method call by a jump to the beginning of the method. We never use the &lt;b&gt;tail&lt;/b&gt; prefix from the IL instruction set, since it's not efficiently implemented.&lt;/div&gt;&lt;ol start="8" style="margin-bottom: 4px;"&gt;&lt;li&gt;&lt;b&gt;Branch operand size tuning&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="margin-left: 24px;"&gt;Our last pass assigns offsets for each surviving operations, and computes distances for each remaining jumps. Then, branch codes are changed into short jumps whenever possible. Full size jumps requires five bytes, while a short jump is codified in just two bytes.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-1604154318410288977?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/1604154318410288977/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=1604154318410288977' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/1604154318410288977'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/1604154318410288977'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/11/jump.html' title='Jump!'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/R0SY7LeLwcI/AAAAAAAAAF4/5xuj8Xn9yxo/s72-c/twisted.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-1303118840050123507</id><published>2007-11-20T18:04:00.001+01:00</published><updated>2008-12-11T19:24:56.822+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='property'/><category scheme='http://www.blogger.com/atom/ns#' term='interface'/><category scheme='http://www.blogger.com/atom/ns#' term='style'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax'/><title type='text'>Stating the obvious</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1px 0 1px 8px;border-style:none;padding:0px;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/R0MT0LeLwbI/AAAAAAAAAFw/yQz88q66dbY/s320/clmembers.jpg" border="0" alt="" /&gt;&lt;span class="p"&gt;Should we always&lt;/span&gt; state the obvious? Interface types can only declare methods, properties and events (and assertions, since we're talking about Freya!). Fields, in any case, are not allowed inside an interface declaration.&lt;/div&gt;&lt;div&gt;Properties, however, are designed most of the times to resemble fields. Why should we stick to the &lt;b&gt;property&lt;/b&gt; keyword when declaring properties for an interface type?&lt;/div&gt;&lt;pre&gt;IScene = &lt;b&gt;interface&lt;/b&gt;&lt;br /&gt;   Sampler: ISampler; &lt;b&gt;readonly&lt;/b&gt;;&lt;br /&gt;   Camera: ICamera; &lt;b&gt;readonly&lt;/b&gt;;&lt;br /&gt;   Root: IShape; &lt;b&gt;readonly&lt;/b&gt;;&lt;br /&gt;   Lights: Array[ILight]; &lt;b&gt;readonly&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;b&gt;method&lt;/b&gt; Render: PixelMap;&lt;br /&gt;   &lt;b&gt;method&lt;/b&gt; Render(Row, Col: Integer): Pixel;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;&lt;/pre&gt;&lt;div&gt;Of course, if you feel uneasy about dropping the keyword, you can still use it. Notice that there's no possible confusion, even with the declaration of &lt;i&gt;Lights&lt;/i&gt;: that's a property returning an array, not an array property.&lt;/div&gt;&lt;div class="p"&gt;By the way, the above code fragment shows another omission: that of the final semicolon after the type declaration. Freya syntax has been relaxed to use optional semicolons in several places: after the &lt;b&gt;end&lt;/b&gt; keyword in a complex type declaration and after the statement block in a method implementation. We're not going as far as Eiffel, where semicolons are optional almost everywhere, but our new syntax makes easier manual code translation from C# into Freya.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-1303118840050123507?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/1303118840050123507/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=1303118840050123507' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/1303118840050123507'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/1303118840050123507'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/11/stating-obvious.html' title='Stating the obvious'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/R0MT0LeLwbI/AAAAAAAAAFw/yQz88q66dbY/s72-c/clmembers.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-6015921976650385064</id><published>2007-11-20T14:21:00.000+01:00</published><updated>2008-12-11T19:24:56.997+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='jumps'/><title type='text'>Common branch suffixes</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1px 0 1px 8px; border-style:none; padding: 0px;" src="http://3.bp.blogspot.com/_QWaXq7W8dVI/R0Lgr7eLwaI/AAAAAAAAAFo/BokhsVoBUOQ/s320/branch.jpg" border="0" alt="" /&gt;&lt;span class="p"&gt;Another little enhancement&lt;/span&gt; has made its way into the Freya optimizer: common branch suffix detection. This technique detects repeated operation codes in code paths converging in a single point, and moves the branching instruction to take advantage of the common code. It's a trick for reducing code size without affecting its speed, at least, in a direct way. Of course, less code means faster JIT translation and better CPU cache performance.&lt;/div&gt;&lt;div&gt;Let's say we must generate code for this statement:&lt;/div&gt;&lt;pre&gt;&lt;b&gt;if&lt;/b&gt; condition &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;   a.m(p&lt;sub&gt;1&lt;/sub&gt;, p&lt;sub&gt;2&lt;/sub&gt;, q)&lt;br /&gt;&lt;b&gt;else&lt;/b&gt;&lt;br /&gt;   a.m(p&lt;sub&gt;3&lt;/sub&gt;, p&lt;sub&gt;4&lt;/sub&gt;, q)&lt;/pre&gt;&lt;div class="p"&gt;The translation scheme would be as follows:&lt;/div&gt;&lt;pre class="p"&gt;    &lt;em&gt;EVAL(&lt;/em&gt;condition&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;br.false&lt;/b&gt; @@else&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;a&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;1&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;2&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;q&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;callvirt&lt;/b&gt; m&lt;br /&gt;    &lt;b&gt;br&lt;/b&gt; @@continue&lt;br /&gt;@@else:&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;a&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;3&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;4&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;q&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;callvirt&lt;/b&gt; m&lt;br /&gt;@@continue:&lt;/pre&gt;&lt;div&gt;It's hard for the compiler to detect the common code in the Abstract Syntax Tree, so this is a task for our &lt;i&gt;peephole optimizer&lt;/i&gt;. The optimizer kicks in after each unconditional jump, and scans backwards in parallel, from the branching point and the branch target. The above translation scheme is simplified this way:&lt;/div&gt;&lt;pre&gt;    &lt;em&gt;EVAL(&lt;/em&gt;condition&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;br.false&lt;/b&gt; @@else&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;a&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;1&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;2&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;br&lt;/b&gt; @@continue&lt;br /&gt;@@else:&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;a&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;3&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;p&lt;sub&gt;4&lt;/sub&gt;&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;@@continue:&lt;br /&gt;    &lt;em&gt;EVAL(&lt;/em&gt;q&lt;em&gt;)&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;callvirt&lt;/b&gt; m&lt;/pre&gt;&lt;div&gt;In almost all situations, this change won't affect speed. There is, however, a case where we must be careful: suppose our conditional was the last statement in a method. In that case, the unconditional branch would jump into a return code, and the peephole optimizer would have substituted the jump with a direct return, saving the time consumed by the jump. To prevent this, common branch suffixes are always detected after the jump optimizer has finished its own duty.&lt;/div&gt;&lt;div&gt;About the impact of this optimization in real-world applications, please note that our example can't benefit from Freya's conditional expressions. What's more, the automatically generated code for iterators typically contains many assignments to the state field that can be factored out with our technique.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-6015921976650385064?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/6015921976650385064/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=6015921976650385064' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/6015921976650385064'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/6015921976650385064'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/11/common-branch-suffixes.html' title='Common branch suffixes'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_QWaXq7W8dVI/R0Lgr7eLwaI/AAAAAAAAAFo/BokhsVoBUOQ/s72-c/branch.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-5681663549922040270</id><published>2007-11-17T16:09:00.000+01:00</published><updated>2008-12-11T19:24:57.377+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='iterator'/><category scheme='http://www.blogger.com/atom/ns#' term='ranges'/><title type='text'>Ranges</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;Range expressions&lt;/span&gt; may appear, in Freya, in three different contexts:&lt;/div&gt;&lt;ol&gt;&lt;li&gt;&lt;img style="FLOAT: right; MARGIN: 1px 0px 1px 6px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpIMMY1UL6I/AAAAAAAAADI/ZT5-qdU4JcM/s320/arrow.jpg" border="0" /&gt;As case labels.&lt;/li&gt;&lt;li&gt;In &lt;b&gt;for&lt;/b&gt; statements.&lt;/li&gt;&lt;li&gt;As the right operand of the &lt;b&gt;in&lt;/b&gt; operator.&lt;/li&gt;&lt;/ol&gt;&lt;div class="p"&gt;Of course, the original source was the &lt;b&gt;case&lt;/b&gt; statement:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;case&lt;/b&gt; &lt;em&gt;expression&lt;/em&gt; &lt;b&gt;of&lt;/b&gt;&lt;br /&gt;    0, 3..20, 23: Console.WriteLine('Howdy');&lt;br /&gt;    50..6000: Console.WriteLine('Silly');&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Please note that Freya, imitating C#, also supports case statements with string expressions, but in that case, ranges are not allowed, due to the underlying implementation, which depends on hash dictionaries. That's not a satisfactory situation. It could be acceptable for C#, where there're no ranges in its &lt;b&gt;switch&lt;/b&gt;, but not for Freya. The solution, perhaps, could be the adoption of a new data structure for implementing ranges. But that's another story.&lt;/div&gt;&lt;h3&gt;Membership&lt;/h3&gt;&lt;div class="p"&gt;From the very first drafts, Freya featured an &lt;b&gt;in&lt;/b&gt; operator, designed to work with collections. An expression like this:&lt;/div&gt;&lt;pre class="p"&gt;x &lt;b&gt;in&lt;/b&gt; A&lt;/pre&gt;&lt;div class="p"&gt;is translated this way:&lt;/div&gt;&lt;pre class="p"&gt;ICollection[T](A).Contains(x)&lt;/pre&gt;&lt;div class="p"&gt;where &lt;i&gt;T&lt;/i&gt; is the compiling time type of &lt;i&gt;x&lt;/i&gt;. There's even a sort of &lt;i&gt;"contains pattern"&lt;/i&gt;, in the line of the "enumerable pattern", which allows any type with a suitable &lt;i&gt;Contains&lt;/i&gt; method to be used as the right operand of the &lt;b&gt;in&lt;/b&gt; operator.&lt;/div&gt;&lt;div class="p"&gt;We evaluated then several techniques for extending the &lt;b&gt;in&lt;/b&gt; operator so it could act on ranges. The obvious technique was allowing ranges inside the brackets of array literals, imitating Pascal sets:&lt;/div&gt;&lt;pre class="p"&gt;x &lt;b&gt;in&lt;/b&gt; [1, 2, 5..10, 16]&lt;/pre&gt;&lt;div class="p"&gt;After some tests, we discarded the idea. Implementing Pascalian bit sets was not an option: it would have required a core Freya assembly, and that's a scary idea. Another problem had to do with the places you could use an array literal with ranges inside. My intuition was crying aloud that the way was far from adding new data structures, like bitsets or extended enumerable interfaces.&lt;/div&gt;&lt;div class="p"&gt;Right now, I think ranges must exists as themselves in the language. The first hint came from the &lt;b&gt;for&lt;/b&gt; statement. One day, I found myself writing loops like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;for&lt;/b&gt; i &lt;b&gt;in&lt;/b&gt; low .. high &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;    Console.WriteLine(i);&lt;/pre&gt;&lt;div class="p"&gt;Of course it is a cheap syntactic variant of the ascending numerical &lt;b&gt;for&lt;/b&gt;, but there was another possible interpretation: the range expression was an iterator expression, i.e., a &lt;i&gt;IEnumerable[T]&lt;/i&gt; expression. Why shouldn't we allow expressions like this?&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; i &lt;b&gt;in&lt;/b&gt; low..high &lt;b&gt;and&lt;/b&gt;&lt;br /&gt;   j &lt;b&gt;not in&lt;/b&gt; low..high &lt;b&gt;then&lt;/b&gt; ...&lt;/pre&gt;&lt;div class="p"&gt;Each range check is translated as two comparisons. For instance:&lt;/div&gt;&lt;pre class="p"&gt;&lt;em&gt;// This expression...&lt;/em&gt;&lt;br /&gt;x &lt;b&gt;in&lt;/b&gt; &lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; .. &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// is translated as&lt;/em&gt;&lt;br /&gt;x &amp;gt;= &lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &lt;b&gt;and&lt;/b&gt; x &amp;lt;= &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// and this one...&lt;/em&gt;&lt;br /&gt;x + 1/x &lt;b&gt;in&lt;/b&gt; &lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; .. &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// is translated as:&lt;/em&gt;&lt;br /&gt;&lt;b&gt;using&lt;/b&gt; &lt;i&gt;temp&lt;/i&gt; := x + 1/x &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;    &lt;i&gt;temp&lt;/i&gt; &amp;gt;= &lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &lt;b&gt;and&lt;/b&gt; &lt;i&gt;temp&lt;/i&gt; &amp;lt;= &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;/pre&gt;&lt;div class="p"&gt;As a side effect, we now must allow &lt;b&gt;in&lt;/b&gt; with an &lt;i&gt;IEnumerable[T]&lt;/i&gt; right operand... expanding it as an inline search loop. But this variant is not implemented yet.&lt;/div&gt;&lt;h3&gt;The existence test&lt;/h3&gt;&lt;div class="p"&gt;There's an interesting and natural variant of the membership test:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; * &lt;b&gt;in&lt;/b&gt; collection &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;    &lt;em&gt;// The collection is not empty.&lt;/em&gt;&lt;/pre&gt;&lt;div class="p"&gt;You can also use the negated variant:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; * &lt;b&gt;not in&lt;/b&gt; collection &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;    &lt;em&gt;// The collection is not empty.&lt;/em&gt;&lt;/pre&gt;&lt;div class="p"&gt;In two of the possible cases, the implementation is trivial. For instance, if the existence test is applied to a range, you have a simple comparison involving the boundaries:&lt;/div&gt;&lt;pre class="p"&gt;&lt;em&gt;// This expression...&lt;/em&gt;&lt;br /&gt;* &lt;b&gt;in&lt;/b&gt; &lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; .. &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// is translated as&lt;/em&gt;&lt;br /&gt;&lt;i&gt;e&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &amp;lt;= &lt;i&gt;e&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;/pre&gt;&lt;div class="p"&gt;When the right side implements the &lt;i&gt;IList[X]&lt;/i&gt; generic interface, the test is based on the &lt;i&gt;Count&lt;/i&gt; property:&lt;/div&gt;&lt;pre class="p"&gt;&lt;em&gt;// This expression...&lt;/em&gt;&lt;br /&gt;* &lt;b&gt;in&lt;/b&gt; &lt;i&gt;list&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// is translated as&lt;/em&gt;&lt;br /&gt;&lt;i&gt;list&lt;/i&gt;.Count &amp;gt; 0&lt;/pre&gt;&lt;div class="p"&gt;However, there's a case where the existence test is really useful. The right operand may only implement the so called &lt;i&gt;enumeration pattern&lt;/i&gt;, as in the &lt;b&gt;foreach&lt;/b&gt; statement, or one of the &lt;i&gt;IEnumerable&lt;/i&gt; interfaces. Let's say the right operand implements &lt;i&gt;IEnumerable[X]&lt;/i&gt;:&lt;/div&gt;&lt;pre class="p"&gt;&lt;em&gt;// This expression...&lt;/em&gt;&lt;br /&gt;* &lt;b&gt;in&lt;/b&gt; &lt;i&gt;enumerable&lt;/i&gt;&lt;br /&gt;&lt;em&gt;// is translated as this expression:&lt;/em&gt;&lt;br /&gt;&lt;b&gt;using&lt;/b&gt; &lt;i&gt;e&lt;/i&gt; := &lt;i&gt;enumerable&lt;/i&gt;.GetEnumerator &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;    &lt;i&gt;e&lt;/i&gt;.MoveNext&lt;/pre&gt;&lt;div class="p"&gt;Please note, first, that we have here a &lt;b&gt;using&lt;/b&gt; expression, not a statement. Second: we need a &lt;b&gt;using&lt;/b&gt; expression because the value returned by &lt;i&gt;GetEnumerator&lt;/i&gt; may implement &lt;i&gt;IDisposable&lt;/i&gt;. In that case, we need a resource protection block to dispose the enumerator after the test. Actually, our new existence test is saving you from writing a &lt;b&gt;for&lt;/b&gt;/&lt;b&gt;in&lt;/b&gt; statement:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;var&lt;/b&gt; result := &lt;b&gt;false&lt;/b&gt;;&lt;br /&gt;&lt;b&gt;for&lt;/b&gt; v &lt;b&gt;in&lt;/b&gt; &lt;i&gt;enumerable&lt;/i&gt;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    result := &lt;b&gt;true&lt;/b&gt;;&lt;br /&gt;    &lt;b&gt;break&lt;/b&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-5681663549922040270?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/5681663549922040270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=5681663549922040270' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/5681663549922040270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/5681663549922040270'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/11/ranges.html' title='Ranges'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpIMMY1UL6I/AAAAAAAAADI/ZT5-qdU4JcM/s72-c/arrow.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-3687091480472095524</id><published>2007-08-22T21:46:00.000+02:00</published><updated>2008-12-11T19:24:57.589+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='style'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax'/><title type='text'>Elisions</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:0 0 0 8px; padding: 0; border-style: none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RsyS7qr_U3I/AAAAAAAAAEQ/enxd8vKtPPc/s320/ae.jpg" border="0" alt="" /&gt;&lt;span class="p"&gt;Less typing means&lt;/span&gt; faster programming, and simple syntactic changes can save a lot of keystrokes. The newest Freya syntax allows you to drop the &lt;b&gt;do&lt;/b&gt; and &lt;b&gt;then&lt;/b&gt; keywords when they precede a statement starting with another keyword.&lt;/div&gt;&lt;div&gt;In this fragment, both the &lt;b&gt;for&lt;/b&gt; and the &lt;b&gt;while&lt;/b&gt; statements have dropped their &lt;b&gt;do&lt;/b&gt;'s:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; GetPrimes(Max: Integer): BitsArray;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    Result := &lt;b&gt;new&lt;/b&gt; BitsArray(2, Max);&lt;br /&gt;    &lt;b&gt;for&lt;/b&gt; i &lt;b&gt;in&lt;/b&gt; 2..Max &lt;b&gt;div&lt;/b&gt; 2 : &lt;b&gt;not&lt;/b&gt; Result[i]&lt;br /&gt;    &lt;b&gt;begin&lt;br /&gt;        var&lt;/b&gt; j := i + i;&lt;br /&gt;        &lt;b&gt;while&lt;/b&gt; j &lt;= Max&lt;br /&gt;        &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;            Result[j] := true;&lt;br /&gt;            j += i;&lt;br /&gt;        &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;    Result.Invert;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div&gt;Note that we have also dropped the &lt;b&gt;for var&lt;/b&gt; combination that used to mark local variable type inference in previous versions.&lt;/div&gt;&lt;div class="p"&gt;There is another interesting detail in the above code:&lt;/div&gt;&lt;pre class="p"&gt;    &lt;b&gt;for&lt;/b&gt; i &lt;b&gt;in&lt;/b&gt; 2..Max &lt;b&gt;div&lt;/b&gt; 2 : &lt;b&gt;not&lt;/b&gt; Result[i]&lt;/pre&gt;&lt;div&gt;First, there's a seemingly innocent variation of the classical &lt;b&gt;for&lt;/b&gt;/&lt;b&gt;to&lt;/b&gt; numerical loop: we have disguised it as if we were iterating over a numerical range. We still cannot eliminate the classical statement, since this "iterator" does not substitute the &lt;b&gt;downto&lt;/b&gt; loop.&lt;/div&gt;&lt;div&gt;However, the real purpose of this change has to do with the Boolean expression after the semicolon. It acts as a filter, and it can be used both with virtual numerical ranges and with real iterators.&lt;/div&gt;&lt;div&gt;At the end, an iterating filter translate as a nested &lt;b&gt;if&lt;/b&gt; statement... but you have saved some typing, and your code is a little more expressive, since you don't have to look for a matching &lt;b&gt;else&lt;/b&gt; before understanding the purpose of that nested &lt;b&gt;if&lt;/b&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-3687091480472095524?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/3687091480472095524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=3687091480472095524' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/3687091480472095524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/3687091480472095524'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/08/elisions.html' title='Elisions'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RsyS7qr_U3I/AAAAAAAAAEQ/enxd8vKtPPc/s72-c/ae.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-4860191914657598575</id><published>2007-07-08T02:37:00.000+02:00</published><updated>2008-12-11T19:24:57.755+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='constructor'/><category scheme='http://www.blogger.com/atom/ns#' term='style'/><category scheme='http://www.blogger.com/atom/ns#' term='chaining'/><category scheme='http://www.blogger.com/atom/ns#' term='creation'/><title type='text'>Be constructive</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 6px;border-style:none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpDqEY1ULzI/AAAAAAAAACQ/ZQ_RUCEPk6w/s320/constr.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;We have already seen&lt;/span&gt; how useful can be &lt;a href="http://freyalang.blogspot.com/2007/06/whiter-shade-of-functional-flavor.html" target="_blank" title="A whiter shade of a functional flavor"&gt;expression based&lt;/a&gt; method implementations. Is there anything similar for constructors? Constructors have no return value, so they cannot directly benefit from expression based implementations... but it turns out there's a surprisingly similar feature at our reach.&lt;/div&gt;&lt;div class="p"&gt;In a sense, a constructor can be viewed as a static function (a method returning some value). The returned value is, of course, a new instance from the class the constructor belongs. In any case, there are a couple of constraints for constructors that regular methods have not: the return type must always be the same, inside a given class, and the returned instance must always be a &lt;i&gt;new&lt;/i&gt; instance.&lt;/div&gt;&lt;div class="p" style="background-color: #d0d0ff; border-top: 1px dotted #a0a0bf; border-bottom: 1px dotted #a0a0bf; line-height: normal; padding: 4px 8px 4px 8px;"&gt;&lt;b&gt;Note&lt;/b&gt;: It could make sense to remove the latter restriction. It's an important part of the contract for languages with explicit memory deallocation. Now think how you must implement an instance cache for a given class: since you cannot overload the &lt;b&gt;new&lt;/b&gt; operator as in classic C++, you must search all creation expressions along the whole application to substitute them with method calls.&lt;/div&gt;&lt;div class="p"&gt;Given the previous constraints, and the fact that constructor calls are functional calls, we can find the equivalent to expression-based implementations: chained calls to sibling or parent constructors... a feature that was there all the time:&lt;/div&gt;&lt;pre class="p"&gt;Stack = &lt;b&gt;class&lt;/b&gt;[X]&lt;br /&gt;&lt;b&gt;private&lt;/b&gt;&lt;br /&gt;    Items: Array[X];&lt;br /&gt;    Count: Integer;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt;(Capacity: Integer);&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Items := &lt;b&gt;new&lt;/b&gt; Array[X](Capacity);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt; : &lt;font color="darkcyan"&gt;Self&lt;/font&gt;(128);&lt;br /&gt;&lt;br /&gt;    &lt;i&gt;// ...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;It's true it looks so different from regular expression-based implementations, but it's a historical consequence: chaining has been available in C++ from long time ago. The only novelty is that you can omit the whole method block from that point on.&lt;/div&gt;&lt;div class="p"&gt;It's recommendable to write always this kind of "bodiless" chaining in the class declaration, instead of using an &lt;b&gt;implementation&lt;/b&gt; section. Neither C# nor Freya supports default parameter values, so it's very frequent to find a handful of constructors in a given type, each of them removing some parameters from the signature's tail. We can use chaining to implement all of them, except the first:&lt;/div&gt;&lt;pre class="p"&gt;Metal = &lt;b&gt;sealed class&lt;/b&gt;(IMaterial)&lt;br /&gt;&lt;b&gt;public&lt;br /&gt;    constructor&lt;/b&gt;(Color: Pixel;&lt;br /&gt;        MinRefl, MaxRefl, Diffuse: Double;&lt;br /&gt;        PhongAmount, PhongSize: Double);&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt;(Color: Pixel;&lt;br /&gt;        MinRefl, MaxRefl, Diffuse: Double) :&lt;br /&gt;        &lt;font color="darkcyan"&gt;Self&lt;/font&gt;(Color, MinRefl, MaxRefl, Diffuse, 0, 1);&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt;(Color: Pixel;&lt;br /&gt;        MinRefl, MaxRefl: Double) :&lt;br /&gt;        &lt;font color="darkcyan"&gt;Self&lt;/font&gt;(Color, MinRefl, MaxRefl, 1, 0, 1);&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt;(Color: Pixel; MinRefl: Double) :&lt;br /&gt;        &lt;font color="darkcyan"&gt;Self&lt;/font&gt;(Color, MinRefl, 1, 1, 0, 1);&lt;br /&gt;    &lt;b&gt;constructor&lt;/b&gt;(Color: Pixel) :&lt;br /&gt;        &lt;font color="darkcyan"&gt;Self&lt;/font&gt;(Color, 0.1, 1, 1, 0, 1);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;There's a second related question: is there any similar feature for &lt;i&gt;iterators&lt;/i&gt;? I have an affirmative answer, but the syntactic details still need some boiling...&lt;/div&gt;&lt;hr class="p"&gt;&lt;div class="p"&gt;Though it's not an official Freya feature, it could also make sense to bring back another old C++ feature: field initialization in constructors. We could have written the previously shown &lt;i&gt;Stack&lt;/i&gt; constructors like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;i&gt;// This is not Freya... yet...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;constructor&lt;/b&gt;(Capacity: Integer)&lt;br /&gt;    : Items(&lt;b&gt;new&lt;/b&gt; Array[X](Capacity));&lt;br /&gt;&lt;b&gt;constructor&lt;/b&gt;&lt;br /&gt;    : Self(128);&lt;/pre&gt;&lt;div class="p"&gt;Of course, this enhances the expressivity of chaining, but that's not the main point. The main reason to bring this feature back has to do with programming by contract: this kind of field initialization may be necessary for implementing &lt;i&gt;not nullable reference types&lt;/i&gt;, as in &lt;a href="http://research.microsoft.com/specsharp/" target="_blank"&gt;Spec#&lt;/a&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-4860191914657598575?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/4860191914657598575/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=4860191914657598575' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/4860191914657598575'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/4860191914657598575'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/07/be-constructive.html' title='Be constructive'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpDqEY1ULzI/AAAAAAAAACQ/ZQ_RUCEPk6w/s72-c/constr.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-4055159475447322718</id><published>2007-07-07T12:13:00.000+02:00</published><updated>2008-12-11T19:24:58.067+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='style'/><title type='text'>Notepad Oriented Programming</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 8px;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/Ro_ESo1ULpI/AAAAAAAAABA/RPK5wHMXa-g/s320/nopl.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;One of the shameless&lt;/span&gt; goals of Freya is to become a &lt;i&gt;Notepad Oriented Programming Language&lt;/i&gt;: you must be able to write Freya applications with the Notepad and little more (Reflector, perhaps?). But that's a pretty hard goal when you're designing a language inspired by the Algol/Pascal lineage. It's not only that you must use those &lt;b&gt;begin&lt;/b&gt;/&lt;b&gt;end&lt;/b&gt; blocks instead of curly braces. If you must keep method declarations apart from their implementations, as in Delphi, then you'll have to type a lot... or lean on an editor that replicates the missing implementations by user request.&lt;/div&gt;&lt;div class="p"&gt;I think we have achieved the above stated goal. Right now, you can write Freya code that looks as compact and easy as C# code, and in some circumstances, the Freya variant may be even shorter than the C# equivalent. To illustrate this, let's take a look at C# and Freya operators. Let's say we're writing a &lt;i&gt;Complex&lt;/i&gt; class in C#. Here you have two user defined operators on that class:&lt;/div&gt;&lt;pre class="p"&gt;&lt;i&gt;// C#&lt;/i&gt;&lt;br /&gt;&lt;b&gt;public static&lt;/b&gt; Complex &lt;b&gt;operator&lt;/b&gt;+(Complex c1, Complex c2)&lt;br /&gt;{&lt;br /&gt;    &lt;b&gt;return new&lt;/b&gt; Complex(c1.Re + c2.Re, c1.Im + c2.Im);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;b&gt;public static&lt;/b&gt; Complex &lt;b&gt;operator&lt;/b&gt;-(Complex c1, Complex c2)&lt;br /&gt;{&lt;br /&gt;    &lt;b&gt;return new&lt;/b&gt; Complex(c1.Re - c2.Re, c1.Im - c2.Im);&lt;br /&gt;}&lt;/pre&gt;&lt;div class="p"&gt;In a first attempt, those two operators would be translated to Freya this way:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;static operator&lt;/b&gt;+(c1, c2: Complex): Complex;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result := &lt;b&gt;new&lt;/b&gt; Complex(&lt;br /&gt;            c1.Re + c2.Re, c1.Im + c2.Im);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;static operator&lt;/b&gt;-(c1, c2: Complex): Complex;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result := &lt;b&gt;new&lt;/b&gt; Complex(&lt;br /&gt;            c1.Re - c2.Re, c1.Im - c2.Im);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;There's nothing to be especially proud of in the above example: our fragment is longer in Freya than in C#. It's true that we have spared ourselves from some Delphi.NET eccentricities. For instance, we have written &lt;i&gt;inline implementations&lt;/i&gt;, avoiding those nasty duplications imposed by the &lt;b&gt;interface&lt;/b&gt;/&lt;b&gt;implementation&lt;/b&gt; artificial split. We have also saved something in constructor calls: we use &lt;b&gt;new&lt;/b&gt; as in C#, instead of calling  some named constructor, as Delphi requires. It has nothing to do with saving two or three characters in each call (we're loosing that tiny advantage by using &lt;b&gt;static&lt;/b&gt; instead of &lt;b&gt;class&lt;/b&gt;, as in Delphi), but our syntax makes easier to translate existing code from C# to Freya. Last, but no least, we can use symbolic names for the operators, instead of &lt;i&gt;Add&lt;/i&gt; and &lt;i&gt;Subtract&lt;/i&gt;, as in Delphi or &lt;a href="http://www.chromesville.com" target="_blank"&gt;Chrome&lt;/a&gt;.&lt;/div&gt;&lt;div class="p"&gt;&lt;i&gt;Expression based implementations&lt;/i&gt; will let us simplify the above listing:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;operator&lt;/b&gt;+(c1, c2: Complex): Complex =&amp;gt;&lt;br /&gt;        &lt;b&gt;new&lt;/b&gt; Complex(c1.Re + c2.Re, c1.Im + c2.Im);&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;operator&lt;/b&gt;-(c1, c2: Complex): Complex =&amp;gt;&lt;br /&gt;        &lt;b&gt;new&lt;/b&gt; Complex(c1.Re - c2.Re, c1.Im - c2.Im);&lt;/pre&gt;&lt;div class="p"&gt;We have deleted both &lt;b&gt;begin&lt;/b&gt;/&lt;b&gt;end&lt;/b&gt; blocks, and both assignations to &lt;i&gt;Result&lt;/i&gt;. Since operators are always public and static in .NET, we have also dropped the &lt;b&gt;static&lt;/b&gt; modifier. We now have code comparable to C# in length, and maybe even shorter. But we can keep shortening our example:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;operator&lt;/b&gt;+(c1, c2: Complex) =&amp;gt; &lt;b&gt;new&lt;/b&gt; Complex(&lt;br /&gt;        c1.Re + c2.Re, c1.Im + c2.Im);&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;operator&lt;/b&gt;-(c1, c2: Complex) =&amp;gt; &lt;b&gt;new&lt;/b&gt; Complex(&lt;br /&gt;        c1.Re - c2.Re, c1.Im - c2.Im);&lt;/pre&gt;&lt;div class="p"&gt;We are showing the last addition to Freya: return type inference for expression-based implementations. This is not a full featured type inference, as in functional languages or a modern language as &lt;a href="http://nemerle.org/Language" target="_blank"&gt;Nemerle&lt;/a&gt;. The Freya compiler only allows the omission of the return type when it finds an expression based implementation, and when the implementing expression is an instantiation expression. We think that complex inferences are not a good thing, at least with a language as Freya, so we have added some inference... up to a sensible point.&lt;/div&gt;&lt;div class="p"&gt;We can use return type inference in yet another case, as this example shows:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;operator&lt;/b&gt;/(c1, c2: Complex) =&gt;&lt;br /&gt;    &lt;b&gt;using&lt;/b&gt; r2 := c2.Re.Sqr + c2.Im.Sqr &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;new&lt;/b&gt; Complex(&lt;br /&gt;            (+c1.Re * c2.Re + c1.Im * c2.Im) / r2,&lt;br /&gt;            (-c1.Re * c2.Im - c1.Im * c2.Re) / r2);&lt;/pre&gt;&lt;div class="p"&gt;In this case, we have a &lt;i&gt;common expression block&lt;/i&gt; containing a &lt;b&gt;new&lt;/b&gt; expression, so we can safely deduce the return type before resolving the whole expression.&lt;/div&gt;&lt;div class="p"&gt;Of course, you won't be forced to write code in this style: if you want your code to look like good old Pascal, you still can write it that way... and I'm not being sarcastic. There's an important problem with compact code: how you get there. When you write programs by assembling small pieces into bigger ones, the result will contain extra code and glue that you probably won't need. The most frequent reason has to do with the fact that modular code, as you store it in your mental pattern library, must deal with a yet unknown context, so it probably has extra checks for handling extreme cases and such. When you adapt those code pieces for a given task, some special cases render improbable, and the corresponding guarding code can be deleted.&lt;/div&gt;&lt;div class="p"&gt;A second source of redundant coding is &lt;i&gt;gluing&lt;/i&gt;. How do you compute the square root of the Zipperstein-Marmaduke Formula? First, you must evaluate the ZMF and then you'll have to find that pesky square root. In your first attempt, it's highly probable that the ZMF return value was stored in a local variable. There are two possibilities about the final code: either you can keep a separate line for dealing with ZMF, just to keeping what your code does, or you can merge both computations in a single expression. It's up to you to decide.&lt;/div&gt;&lt;div class="p"&gt;The consequence: compact code may be easier to read than to write... as the failure of functional programming languages to gain enough users has shown. Freya doesn't require you to write the shortest possible code, but that's still an option you have once you master the language.&lt;/div&gt;&lt;hr class="p"&gt;&lt;div class="p"&gt;... by the way, now we can write the &lt;a href="http://freyalang.blogspot.com/2007/06/whiter-shade-of-functional-flavor.html#060107_ray" target="_blank" title="A whiter shade of a functional flavor"&gt;&lt;i&gt;Ray&lt;/i&gt; class&lt;/a&gt; as follows:&lt;/div&gt;&lt;pre class="p"&gt;Ray = &lt;b&gt;sealed class&lt;br /&gt;public&lt;/b&gt;&lt;br /&gt;    Origin, Direction: Vector;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Items[Time: Double] =&amp;gt; &lt;b&gt;new&lt;/b&gt; Vector(&lt;br /&gt;        Origin.X + Time * Direction.X,&lt;br /&gt;        Origin.Y + Time * Direction.Y,&lt;br /&gt;        Origin.Z + Time * Direction.Z);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-4055159475447322718?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/4055159475447322718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=4055159475447322718' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/4055159475447322718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/4055159475447322718'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/07/notepad-oriented-programming.html' title='Notepad Oriented Programming'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/Ro_ESo1ULpI/AAAAAAAAABA/RPK5wHMXa-g/s72-c/nopl.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-7869911123446816557</id><published>2007-06-07T15:51:00.000+02:00</published><updated>2008-12-11T19:24:58.365+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='iterator'/><category scheme='http://www.blogger.com/atom/ns#' term='yield'/><title type='text'>It is more blessed to yield than to return</title><content type='html'>&lt;div&gt;&lt;img style="float:right; margin:1 0 0px 6px;border-style: none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpEG3o1UL1I/AAAAAAAAACg/6tGBAvFDbnw/s320/recycle.jpg" border="0" /&gt;&lt;span class="p"&gt;Yield statements in Freya must&lt;/span&gt; be associated with expressions with the same type as the return value from the iterator:&lt;/div&gt;&lt;pre&gt;BinaryTree[X].TreeNode = &lt;b&gt;class&lt;/b&gt;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Value: X;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Left, Right: TreeNode;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;iterator&lt;/b&gt; InOrder: X;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;if&lt;/b&gt; Left &amp;lt;&amp;gt; &lt;b&gt;nil then&lt;br /&gt;            for var&lt;/b&gt; v &lt;b&gt;in&lt;/b&gt; Left.InOrder &lt;b&gt;do&lt;br /&gt;                yield&lt;/b&gt; v;&lt;br /&gt;        &lt;b&gt;yield&lt;/b&gt; Value;&lt;br /&gt;        &lt;b&gt;if&lt;/b&gt; Right &amp;lt;&amp;gt; &lt;b&gt;nil then&lt;br /&gt;            for var&lt;/b&gt; v &lt;b&gt;in&lt;/b&gt; Right.InOrder &lt;b&gt;do&lt;br /&gt;                yield&lt;/b&gt; v;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;i&gt;// ...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div&gt;Since &lt;i&gt;InOrder&lt;/i&gt; returns an &lt;i&gt;X&lt;/i&gt; in each step, each &lt;b&gt;yield&lt;/b&gt; expression must also belong to this type. Let's see what happens if we allow attaching an &lt;i&gt;IEnumerable[X]&lt;/i&gt; to &lt;b&gt;yield&lt;/b&gt;:&lt;/div&gt;&lt;pre&gt;BinaryTree[X].TreeNode = &lt;b&gt;class&lt;/b&gt;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Value: X;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Left, Right: TreeNode;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;iterator&lt;/b&gt; InOrder: X;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;if&lt;/b&gt; Left &amp;lt;&amp;gt; &lt;b&gt;nil then&lt;br /&gt;            yield&lt;/b&gt; Left.InOrder;&lt;br /&gt;        &lt;b&gt;yield&lt;/b&gt; Value;&lt;br /&gt;        &lt;b&gt;if&lt;/b&gt; Right &amp;lt;&amp;gt; &lt;b&gt;nil then&lt;br /&gt;            yield&lt;/b&gt; Right.InOrder;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;i&gt;// ...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div&gt;I think it's easy to tell both uses of yield. When the &lt;b&gt;yield&lt;/b&gt; expression has type &lt;i&gt;IEnumerable[X]&lt;/i&gt;, instead of &lt;i&gt;X&lt;/i&gt;, the compiler translates the statement into a &lt;b&gt;for&lt;/b&gt;/&lt;b&gt;in&lt;/b&gt; loop, saving the programmer a lot of keystrokes.&lt;/div&gt;&lt;div&gt;Finally, let's take a second look on the code that dropped from the iterator:&lt;/div&gt;&lt;pre&gt;&lt;b&gt;for var&lt;/b&gt; v &lt;b&gt;in&lt;/b&gt; Right.InOrder &lt;b&gt;do&lt;br /&gt;    yield&lt;/b&gt; v;&lt;/pre&gt;&lt;div&gt;It's easy to see that the compiler can generate better code for this special kind of loops: there's no need to allocate a explicit variable to hold the value returned by the (nested) iterator in each step. In other words, the above fragment can be translated like this (ignoring the final call to &lt;i&gt;Dispose&lt;/i&gt;):&lt;/div&gt;&lt;pre&gt;&lt;b&gt;var&lt;/b&gt; en := Right.InOrder.GetEnumerator;&lt;br /&gt;&lt;b&gt;while&lt;/b&gt; en.MoveNext &lt;b&gt;do&lt;br /&gt;    yield&lt;/b&gt; en.Current;&lt;/pre&gt;&lt;div&gt;Our compiler now detects this case and generates efficient code for it. You should realize that, though the loop variable &lt;i&gt;v&lt;/i&gt; looks like a local variable, it is implemented via fields, since the code is part of an iterator.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-7869911123446816557?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/7869911123446816557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=7869911123446816557' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7869911123446816557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7869911123446816557'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/06/it-is-more-blessed-to-yield-than-to.html' title='It is more blessed to yield than to return'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpEG3o1UL1I/AAAAAAAAACg/6tGBAvFDbnw/s72-c/recycle.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-7609055774735658835</id><published>2007-06-01T09:43:00.000+02:00</published><updated>2008-12-11T19:24:58.517+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='boolean'/><category scheme='http://www.blogger.com/atom/ns#' term='implementation'/><title type='text'>A whiter shade of a functional flavor</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Take a look at&lt;/span&gt; this example:&lt;/div&gt;&lt;pre class="p"&gt;&lt;i&gt;// Yes, this is still Freya!&lt;/i&gt;&lt;br /&gt;&lt;b&gt;method&lt;/b&gt; Factorial(n: Integer): LongInt &lt;b&gt;=&amp;gt;&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; n &lt;= 1 &lt;b&gt;then&lt;/b&gt; 1 &lt;b&gt;else&lt;/b&gt; n * Factorial(n - 1);&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 8px; border-style: none;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/RpAAmo1ULqI/AAAAAAAAABI/4PKZfhK4Tx0/s320/eff.jpg" border="0" /&gt;There are two new features in those three lines. The first one is inline implementation of executable members via expressions. The second one is the conditional &lt;i&gt;expression&lt;/i&gt;. The former leads to the latter, so that's the order I will follow for explaining it.&lt;/div&gt;&lt;div class="p"&gt;First of all, why do we need inline implementation? Freya started as a Delphi/Pascal mutation, and adopted the splitting style: you must group  your declarations in one place, and all executable code is written in a different section of the same file. This approach has its pros and cons. It's easy to extract a "contract form" from the source, without sophisticated edition tools. Bad news has to do with projects featuring a lot of small classes: the syntactic overhead may be greater than the real code. The following &lt;i&gt;Ray&lt;/i&gt; class is a good example:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    Ray = &lt;b&gt;sealed class&lt;br /&gt;    public&lt;/b&gt;&lt;br /&gt;        Origin, Direction: Vector;&lt;br /&gt;        &lt;b&gt;property&lt;/b&gt; Items[Time: Double]: Vector;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Ray &lt;b&gt;is&lt;br /&gt;&lt;br /&gt;    property&lt;/b&gt; Items(Time: Double): Vector;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result.X := Origin.X + Time * Direction.X;&lt;br /&gt;        Result.Y := Origin.Y + Time * Direction.Y;&lt;br /&gt;        Result.Z := Origin.Z + Time * Direction.Z;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;A first step could be moving the implementation part inside the own &lt;i&gt;Ray&lt;/i&gt; class. Gains are small, however:&lt;/div&gt;&lt;pre class="p"&gt;Ray = &lt;b&gt;sealed class&lt;br /&gt;public&lt;/b&gt;&lt;br /&gt;    Origin, Direction: Vector;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Items[Time: Double]: Vector;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation&lt;br /&gt;&lt;br /&gt;    property&lt;/b&gt; Items(Time: Double): Vector;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result.X := Origin.X + Time * Direction.X;&lt;br /&gt;        Result.Y := Origin.Y + Time * Direction.Y;&lt;br /&gt;        Result.Z := Origin.Z + Time * Direction.Z;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;This still looks more Delphic than Eiffel-like. And I must confess I love Eiffel. So, we can reunite the &lt;i&gt;Items&lt;/i&gt; implementation with its declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;a name="#060107_ray"&gt;&lt;/a&gt;Ray = &lt;b&gt;sealed class&lt;br /&gt;public&lt;/b&gt;&lt;br /&gt;    Origin, Direction: Vector;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Items[Time: Double]: Vector;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result.X := Origin.X + Time * Direction.X;&lt;br /&gt;        Result.Y := Origin.Y + Time * Direction.Y;&lt;br /&gt;        Result.Z := Origin.Z + Time * Direction.Z;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;This explains the &lt;i&gt;inline implementation&lt;/i&gt; bit. Now take a look at this record:&lt;/div&gt;&lt;pre class="p"&gt;Vector = &lt;b&gt;record&lt;br /&gt;public&lt;br /&gt;    property&lt;/b&gt; X, Y, Z: Double;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Length: Double;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result := (X.Sqr + Y.Sqr + Z.Sqr).Sqrt;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;&lt;i&gt;X&lt;/i&gt;, &lt;i&gt;Y&lt;/i&gt; and &lt;i&gt;Z&lt;/i&gt; are field-based properties, since they have no explicit implementations. Our &lt;i&gt;vector&lt;/i&gt; type features another read-only property, &lt;i&gt;Length&lt;/i&gt;, which we have already implemented inline. The getter body is very simple: a single statement, actually a return statement. &lt;i&gt;Déjà vu&lt;/i&gt;, anyone?&lt;/div&gt;&lt;div class="p"&gt;... yes, this is a situation we will find again when dealing with anonymous methods. C# 2 introduced anonymous methods, but the syntax was too heavy for simple methods like our &lt;i&gt;get_Length&lt;/i&gt;. For this reason, C# 3 introduced &lt;i&gt;lambda expressions&lt;/i&gt;: a simplified syntax for writing anonymous methods with only a return statement in their bodies. Of course, C# 3's lambdas has more interesting features than merely this syntactic trick (more powerful type inference, for instance).&lt;/div&gt;&lt;div class="p"&gt;C# has a Spartan syntax, but Pascal heirs are a lot more verbose, so this simplification is even more important for us. Now ask yourself, why limit the benefits of the lambda style to writing anonymous methods:&lt;/div&gt;&lt;pre class="p"&gt;Vector = &lt;b&gt;record&lt;br /&gt;public&lt;br /&gt;    property&lt;/b&gt; X, Y, Z: Double;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;property&lt;/b&gt; Length: Double &lt;b&gt;=&amp;gt;&lt;/b&gt;&lt;br /&gt;        (X.Sqr + Y.Sqr + Z.Sqr).Sqrt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;We have "shaven" all the &lt;b&gt;begin&lt;/b&gt;/&lt;b&gt;end&lt;/b&gt; unnecesary junk, and the assumed assignment to &lt;i&gt;Result&lt;/i&gt;. The final code is a lot more readable and less error prone. Now you know what does &lt;i&gt;"expression-based inline implementation"&lt;/i&gt; meant.&lt;/div&gt;&lt;div class="p"&gt;There remains a question: why did we add such a filthy nasty feature such as a stinky conditional expression inspired by C? In my defense, please note that our conditional expression smells more like Haskell or Miranda than good old C. There's also a good hidden signal: the LALR generator did not complain for the duplicated use of &lt;b&gt;if&lt;/b&gt;/&lt;b&gt;then&lt;/b&gt;/&lt;b&gt;else&lt;/b&gt;. Chances to confuse the expression and the statement are, thus, minimal. But the main reason is power: once we have single expression lambdas, we want to make this feature as powerful as possible. Thanks to this, we have been able to reduce &lt;i&gt;Factorial&lt;/i&gt;'s implementation to a single inlined expression, as already shown.&lt;/div&gt;&lt;div class="p"&gt;Of course, there are more things that we could do for enhancing expressions. This method returns a "normalized" copy of a vector:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; Normalized: Vector;&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;    var&lt;/b&gt; L := Self.Length;&lt;br /&gt;    Result := &lt;b&gt;if&lt;/b&gt; L = 0&lt;br /&gt;        &lt;b&gt;then&lt;/b&gt; Self&lt;br /&gt;        &lt;b&gt;else new&lt;/b&gt; Vector(X / L, Y / L, Z / L);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;We are already using a conditional expression, but that's not enough to reduce this simple implementation to a single expression. What we need are "real" lambdas: I mean, inline anonymous functions... wait... that's what C# 3 now provides, isn't it? Not exactly: now we can embed lambdas inside the "imperative" side of C#, but not inside its brand-new, functional-like personality. I want something like this (provisional syntax):&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; Normalized: Vector &lt;b&gt;=&amp;gt;&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; L = 0 &lt;b&gt;then&lt;/b&gt; Self&lt;br /&gt;    &lt;b&gt;else new&lt;/b&gt; Vector(X / L, Y / L, Z / L)&lt;br /&gt;    &lt;b&gt;where&lt;/b&gt; L := Self.Length;&lt;/pre&gt;&lt;div class="p"&gt;Why do I have used now an assignment instead of the &lt;b&gt;=&amp;gt;&lt;/b&gt; keyword? To make short a long story: an assignment means a single evaluation, no matter how many times the defined symbol is used. On the contrary, &lt;b&gt;=&amp;gt;&lt;/b&gt; means substituting the symbol with its definition each time. This is not a concern for a functional programming language, since they have, or they shouldn't have any side effects. See this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;static&lt;/b&gt; I: Double;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;property&lt;/b&gt; A: Double = I + 1;&lt;br /&gt;&lt;b&gt;property&lt;/b&gt; B: Double &lt;b&gt;=&amp;gt;&lt;/b&gt; I + 1;&lt;/pre&gt;&lt;div class="p"&gt;&lt;i&gt;A&lt;/i&gt; is a field based property (a read/write one, by the way), and the underlying field is initialized as &lt;i&gt;I + 1&lt;/i&gt; when an instance of the declaring class is created. On the other hand, &lt;i&gt;B&lt;/i&gt; is a read only, inline implemented property. Each time you read from &lt;i&gt;B&lt;/i&gt;, the expression &lt;i&gt;I + 1&lt;/i&gt; is reevaluated. Something similar happens with interface delegations:&lt;/div&gt;&lt;pre class="p"&gt;MyClass = &lt;b&gt;class&lt;/b&gt;(IEnumerable)&lt;br /&gt;    &lt;i&gt;// ...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;implementation&lt;br /&gt;    interface&lt;/b&gt; IEnumerable = GetArray();&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;For the above class, the compiler creates a hidden &lt;i&gt;IEnumerable&lt;/i&gt; field, and initializes it with the value return by the call to &lt;i&gt;GetArray&lt;/i&gt;. This is an alternative:&lt;/div&gt;&lt;pre class="p"&gt;MyClass = &lt;b&gt;class&lt;/b&gt;(IEnumerable)&lt;br /&gt;    &lt;i&gt;// ...&lt;/i&gt;&lt;br /&gt;&lt;b&gt;implementation&lt;br /&gt;    interface&lt;/b&gt; IEnumerable &lt;b&gt;is&lt;/b&gt; GetArray();&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;Each time we apply a method from &lt;i&gt;IEnumerable&lt;/i&gt; on a &lt;i&gt;MyClass&lt;/i&gt; instance, &lt;i&gt;GetArray&lt;/i&gt; is reevaluated, and the method call is redirected to the value returned by &lt;i&gt;GetArray&lt;/i&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-7609055774735658835?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/7609055774735658835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=7609055774735658835' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7609055774735658835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/7609055774735658835'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/06/whiter-shade-of-functional-flavor.html' title='A whiter shade of a functional flavor'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/RpAAmo1ULqI/AAAAAAAAABI/4PKZfhK4Tx0/s72-c/eff.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-8851464654706642515</id><published>2007-02-18T20:10:00.000+01:00</published><updated>2008-12-11T19:24:58.670+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='method'/><title type='text'>Extension methods in Freya</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;I'm working right&lt;/span&gt; now on &lt;i&gt;extension methods&lt;/i&gt; for Freya. These methods must be declared as part of static classes, with a special syntax:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    MathTools = &lt;b&gt;static class&lt;br /&gt;    public&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;method&lt;/b&gt; Abs: Double &lt;b&gt;for&lt;/b&gt; Self: Double;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;&lt;/pre&gt;&lt;div class="p"&gt;This is a (trivial) implementation for &lt;i&gt;Abs&lt;/i&gt;:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; MathTools &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Abs: Double &lt;b&gt;for&lt;/b&gt; Self: Double;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result := Math.Abs(Self);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;The above declaration is translated like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    MathTools = &lt;b&gt;static class&lt;br /&gt;    publi&lt;/b&gt;&lt;br /&gt;        &lt;b&gt;method&lt;/b&gt; Abs(Self: Double): Double;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; MathTools &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Abs(Self: Double): Double;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Result := Math.Abs(Self);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;There's no mistery: the bizarre &lt;i&gt;"with Self"&lt;/i&gt; parameter has been moved to the head of the parameter list. In our example, our parameter list was initially empty, but that's an accident, not a requirement.&lt;/div&gt;&lt;div class="p"&gt;Let's see now how we would use the extension method:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;var&lt;/b&gt;&lt;br /&gt;    d: Double;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    d := -1.0;&lt;br /&gt;    Console.WriteLine(d.Abs);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;It looks like the &lt;i&gt;Double&lt;/i&gt; class had defined an &lt;i&gt;Abs&lt;/i&gt; instance method from the first time. However, we both know that &lt;i&gt;Abs&lt;/i&gt; was defined later, in an extension class. Of course, you can also use &lt;i&gt;MathTools.Abs&lt;/i&gt; as a static method:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;var&lt;/b&gt;&lt;br /&gt;    d: Double;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    d := -1.0;&lt;br /&gt;    Console.WriteLine(MathTools.Abs(d));&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:left; margin:1px 8px 0px 0; border-style: none;" src="http://3.bp.blogspot.com/_QWaXq7W8dVI/RpAJgI1ULvI/AAAAAAAAABw/NzUXapmOxxc/s320/exts1.jpg" border="0" /&gt;This feature has a relatively long history. As far as I know, it appeared in Delphi 8, mainly as a way to add "predefined" methods to the &lt;i&gt;Object&lt;/i&gt; base class. I didn't like the new feature. Then, it has reappeared in the C# 3.0 draft, probably as a syntatic sugar for some LINQ features. I still dislike extension methods.&lt;/div&gt;&lt;div class="p"&gt;There're still some loosing ends. The C# 3.0 draft suggests that extension classes are automatically registered via &lt;b&gt;using&lt;/b&gt; clauses. I suppose there will be some kind of attribute mark at class level for telling appart extension classes from regular classes, in order to avoid full member reading in the compiler at initialization time.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-8851464654706642515?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/8851464654706642515/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=8851464654706642515' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/8851464654706642515'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/8851464654706642515'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/02/extension-methods-in-freya.html' title='Extension methods in Freya'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_QWaXq7W8dVI/RpAJgI1ULvI/AAAAAAAAABw/NzUXapmOxxc/s72-c/exts1.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-8197591124978298155</id><published>2007-02-13T21:28:00.000+01:00</published><updated>2008-12-11T19:24:58.886+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='property'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><category scheme='http://www.blogger.com/atom/ns#' term='field'/><title type='text'>Field-based properties</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Freya already&lt;/span&gt; supports &lt;a href="http://freyalang.blogspot.com/2005/05/on-properties-and-events.html" target="_blank"&gt;field-based properties&lt;/a&gt;. If you declare a property, but you don't provide an implementation, the compiler adds automatically a hidden field and implements the property using the new field. Even if the property is declared as read only, you can assign values to the hidden field using the property name, as long as your code is located inside the declaring class or inside another class nested in the declaring class.&lt;/div&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 8px; border-style: none;" src="http://3.bp.blogspot.com/_QWaXq7W8dVI/RpACfI1ULrI/AAAAAAAAABQ/ecOCEkEWfV4/s320/fields.jpg" border="0" /&gt;I was declaring the hidden field as private, but I have realized that it's better to declare it as an internal field, so any class compiled inside the same assembly could access the field instead of the property. Of course, if the property is read only, any other class still can not write on the field. Another constraint has to do with virtual properties: if the property is declared as virtual, it's not safe to reference the field instead of the property.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-8197591124978298155?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/8197591124978298155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=8197591124978298155' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/8197591124978298155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/8197591124978298155'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/02/field-based-properties.html' title='Field-based properties'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_QWaXq7W8dVI/RpACfI1ULrI/AAAAAAAAABQ/ecOCEkEWfV4/s72-c/fields.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-116956989456599829</id><published>2007-01-23T17:19:00.000+01:00</published><updated>2008-12-11T19:24:59.112+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><title type='text'>Predefined extensions</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Freya will have to&lt;/span&gt; implement something like C# 3.0's &lt;i&gt;extension methods&lt;/i&gt;. I'm not an extension method's fan, and some details still wait for a decision. Meanwhile, we have added some predefined extension methods to the predefined &lt;b&gt;double&lt;/b&gt; type, so you can write this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;property&lt;/b&gt; Length: Double;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    Result := (x.Sqr + y.Sqr + z.Sqr).Sqrt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 8px; border-style: none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpAD2Y1ULsI/AAAAAAAAABY/J4cCCAHOIm4/s320/exts.jpg" border="0" /&gt;Every method defined in the static &lt;i&gt;System.Math&lt;/i&gt; class can be used as an instance method on any &lt;i&gt;System.Double&lt;/i&gt; instance. On the other hand, there's no &lt;i&gt;Math.Sqr&lt;/i&gt; method at all: it's just an intrinsic method, requiring some compiler magic. Another "intrinsic" method extension: the &lt;i&gt;Ord&lt;/i&gt; method applied to a &lt;i&gt;Char&lt;/i&gt; expression.&lt;/div&gt;&lt;div class="p"&gt;Another addition to the compiler: a "peephole" optimizer. This is an optimizer that works on a IL representation. Right now, the optimizer choose between short and long branch codes, eliminates jumps in cascade, remove some useless data writes (most of them induced by the &lt;i&gt;Result&lt;/i&gt; variable) and detects unreachable code.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-116956989456599829?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/116956989456599829/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=116956989456599829' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/116956989456599829'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/116956989456599829'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/01/predefined-extensions.html' title='Predefined extensions'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpAD2Y1ULsI/AAAAAAAAABY/J4cCCAHOIm4/s72-c/exts.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-116803289896258378</id><published>2007-01-05T22:29:00.000+01:00</published><updated>2008-12-11T19:24:59.248+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='initializer'/><category scheme='http://www.blogger.com/atom/ns#' term='creation'/><title type='text'>Collection initializers</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Freya have another&lt;/span&gt; new feature borrowed from C# 3.0: &lt;i&gt;collection initializers&lt;/i&gt;.&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;var&lt;/b&gt; L := &lt;b&gt;new&lt;/b&gt; List[Integer]![1, 2, 3, 4];&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 0px; border-style: none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpAGd41ULtI/AAAAAAAAABg/PeszkDhg_B4/s320/pool.jpg" border="0" /&gt;As you can see, an array literal can be appended to the creation of any class implementing the generic &lt;i&gt;ICollection[T]&lt;/i&gt; interface type. But, this time, the array literal doesn't generates an array at runtime. Instead, for each item in the literal, a call to the method &lt;i&gt;Add&lt;/i&gt;, from the collection, is generated.&lt;/div&gt;&lt;div class="p"&gt;In order to realize why we need collection initializers, you must weight the alternative method:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;var&lt;/b&gt; L := &lt;b&gt;new&lt;/b&gt; List[Integer]([1, 2, 3, 4]);&lt;/pre&gt;&lt;div class="p"&gt;In this case, the array literal is passed as a parameter to the constructor call. However, the parameter is declared as &lt;i&gt;IEnumerable[T]&lt;/i&gt;. Can you see what it means? A temporal array must be generated and initialized, item by item. Then, the array must be casted as a &lt;i&gt;IEnumerable[T]&lt;/i&gt; reference, which will be passed to the constructor. Inside the constructor, &lt;i&gt;GetEnumerator&lt;/i&gt; is called, and a second temporal object is created. Finally, a loop is performed, in order to add an item for each enumerated item.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-116803289896258378?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/116803289896258378/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=116803289896258378' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/116803289896258378'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/116803289896258378'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2007/01/collection-initializers.html' title='Collection initializers'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpAGd41ULtI/AAAAAAAAABg/PeszkDhg_B4/s72-c/pool.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-114357181264821551</id><published>2006-03-28T20:35:00.000+02:00</published><updated>2008-12-11T19:25:00.616+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='property'/><category scheme='http://www.blogger.com/atom/ns#' term='indexer'/><title type='text'>Indexers</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 2px;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpAIvY1ULuI/AAAAAAAAABo/ffoeT-WeoAU/s320/idx.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;I had postponed any&lt;/span&gt; decision on Freya indexers until I could find a better model for representing name overloading in the symbol table. The original data structure was borrowed from &lt;a href="http://blogs.msdn.com/jmstall/archive/2005/02/06/368192.aspx" target="_blank"&gt;Blue&lt;/a&gt;, and it was based in a clever trick: each context table contained a &lt;em&gt;"group symbol"&lt;/em&gt; entry, which associated a name like &lt;em&gt;WriteLine&lt;/em&gt; to the list of overloaded methods. At the same time, each of these methods was included independently. Since the context table is a kind of hash dictionary, method names were "decorated" or mangled, adding information from the parameter types.&lt;/div&gt;&lt;div class="p"&gt;I started with a version of Blue without indexers, and that's the reason why the symbol table only considered overloading for methods, but not for properties. There was another problem with explicit and implicit user defined conversions. The raw &lt;em&gt;op_Implicit&lt;/em&gt; and &lt;em&gt;op_Explicit&lt;/em&gt; methods violate one important rule in Freya and C#: they cannot be distinguished by the parameters signature! Suppose you write this in C#:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public static operator explicit double&lt;/b&gt;(Complex);&lt;br /&gt;&lt;b&gt;public static operator explicit float&lt;/b&gt;(Complex);&lt;/pre&gt;&lt;div class="p"&gt;They translate to something like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public static double&lt;/b&gt; op_Explicit(Complex);&lt;br /&gt;&lt;b&gt;public static float&lt;/b&gt; op_Explicit(Complex);&lt;/pre&gt;&lt;div class="p"&gt;Both methods have identical parameter lists, and they only differ in their return types. Of course, this is forbidden in C# and Freya... but is supported by the CLR. But the decorated parameter trick was inappropiate for representing this situation.&lt;/div&gt;&lt;div class="p"&gt;I have solved the problem by only including the group name for user defined conversions and indexers. Probably, there's no reason for including decorated method names in the hash table, but I won't mess right now with such a far reaching change in the compiler.&lt;/div&gt;&lt;div class="p"&gt;Finally, Freya has full functional indexers, following Visual Basic.NET. The reason:&lt;/div&gt;&lt;ol&gt;&lt;li style="font-size:90%;"&gt;The VB model is the most general.&lt;/li&gt;&lt;li style="font-size:90%;"&gt;Surprisingly, you can access secondary indexers from a C# application.&lt;/li&gt;&lt;li style="font-size:90%;"&gt;So far, I haven't discovered any important drawback in the VB model.&lt;/li&gt;&lt;/ol&gt;&lt;div class="p"&gt;I have also added &lt;i&gt;lifted operators&lt;/i&gt; to Freya: these are automatic extensions for predefined and user operators for dealing with nullable types, and they imitate the behavior of SQL operators when they act on null values.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-114357181264821551?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/114357181264821551/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=114357181264821551' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/114357181264821551'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/114357181264821551'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/03/indexers.html' title='Indexers'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpAIvY1ULuI/AAAAAAAAABo/ffoeT-WeoAU/s72-c/idx.jpg' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-114175329052908536</id><published>2006-03-07T18:17:00.000+01:00</published><updated>2008-12-11T19:25:00.860+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='operator'/><category scheme='http://www.blogger.com/atom/ns#' term='typecast'/><title type='text'>Operating with operators</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Let's do a quick review of&lt;/span&gt; operators in Freya. Operators must be declared, as usual, in &lt;strong&gt;public static&lt;/strong&gt; sections:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;static public&lt;/strong&gt;&lt;br /&gt;    &lt;strong&gt;method&lt;/strong&gt;+(c1, c2: Complex): Complex;&lt;br /&gt;    &lt;strong&gt;operator&lt;/strong&gt;-(c1, c2: Complex): Complex;&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 0px 8px; border-style: none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpCwJY1ULwI/AAAAAAAAAB4/PyQZd1-7S7Q/s320/plus.jpg" border="0" /&gt;As you see, method and operator are equivalent when defining a "symbolic" operator. No syntax ambiguity arises in these cases, and the &lt;strong&gt;method&lt;/strong&gt; keyword is friendlier, in my humble opinion.&lt;/div&gt;&lt;div class="p"&gt;However, &lt;strong&gt;operator&lt;/strong&gt; is required when the operator name is a keyword or an identifier:&lt;/div&gt;&lt;pre class="p"&gt;    &lt;strong&gt;operator True&lt;/strong&gt;(c: Complex): Boolean;&lt;/pre&gt;&lt;div class="p"&gt;Technically, &lt;strong&gt;True&lt;/strong&gt; is an operator in Freya, following C#. We cannot proceed as in Pascal and consider &lt;em&gt;True&lt;/em&gt; an enumerative constant, since in that case, both Freya and C# would require the class name as qualifier: &lt;em&gt;Boolean.True&lt;/em&gt;.&lt;/div&gt;&lt;div class="p"&gt;Even when the operator name is a keyword, there's no syntactic ambiguity. The real reason for keeping the &lt;strong&gt;operator&lt;/strong&gt; keyword is this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;    operator&lt;/strong&gt; Explicit(c: Complex): Double;&lt;br /&gt;&lt;strong&gt;    operator&lt;/strong&gt; Implicit(d: Double): Complex;&lt;/pre&gt;&lt;div class="p"&gt;Though both &lt;strong&gt;explicit&lt;/strong&gt; and &lt;strong&gt;implicit&lt;/strong&gt; are C# keywords, they are not keywords in Freya. We could have allowed this:&lt;/div&gt;&lt;pre class="p"&gt;   &lt;strong&gt;method&lt;/strong&gt; Explicit(c: Complex): Double;&lt;/pre&gt;&lt;div class="p"&gt;But I thought it would be dangerous, since you cannot tell whether the programmer intention was declaring a full featured operator or a regular method called &lt;em&gt;Explicit&lt;/em&gt;.&lt;/div&gt;&lt;div class="p"&gt;Operator implementation is straightforward, as usual:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;implementation for&lt;/strong&gt; Complex &lt;strong&gt;is&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;operator&lt;/strong&gt; Implicit(d: Double): Complex;&lt;br /&gt;    &lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;        Result := &lt;strong&gt;new&lt;/strong&gt; Complex(d, 0.0);&lt;br /&gt;    &lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-114175329052908536?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/114175329052908536/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=114175329052908536' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/114175329052908536'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/114175329052908536'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/03/operating-with-operators.html' title='Operating with operators'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpCwJY1ULwI/AAAAAAAAAB4/PyQZd1-7S7Q/s72-c/plus.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113736209069072156</id><published>2006-01-15T22:46:00.000+01:00</published><updated>2008-12-11T19:25:01.343+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='interface'/><title type='text'>More on interface delegations</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 7px;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpEelI1UL2I/AAAAAAAAACo/fqFjPR6jqAg/s320/mask.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;As I explained&lt;/span&gt; &lt;a href="http://freyalang.blogspot.com/2005/04/implementing-interface-mappings.html" title="Implementing interface mappings"&gt;here&lt;/a&gt;, Freya allows the programmer to implement an interface type by delegating the implementation of methods from the interface to an instance of an already existing implementation. This trick is borrowed from Delphi, and it's an effective and easy technique for reusing interface implementations. Then, I used this example:&lt;/div&gt;&lt;pre class="p"&gt;&lt;font color="#006600"&gt;// WARNING: THE SYNTAX HAS CHANGED!!!&lt;/font&gt;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  Foo = &lt;b&gt;class&lt;/b&gt;(IEnumerable)&lt;br /&gt;  &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Foo &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  &lt;font color="#006600"&gt;// A private instance field.&lt;/font&gt;&lt;br /&gt;  &lt;b&gt;var&lt;/b&gt; Items: &lt;b&gt;array of&lt;/b&gt; String := ['Freya', 'rules'];&lt;br /&gt;&lt;br /&gt;  &lt;font color="#006600"&gt;// Just an implementation detail!&lt;/font&gt;&lt;br /&gt;  &lt;b&gt;interface&lt;/b&gt; IEnumerable &lt;b&gt;is&lt;/b&gt; Items;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;Our &lt;i&gt;Foo&lt;/i&gt; class must implement the &lt;i&gt;IEnumerable&lt;/i&gt; interface type, and it does it by delegating the implementation to a string array; all array types implement that interface, don't they? The delegation member contains a mention to the private field &lt;i&gt;Items&lt;/i&gt;... but our real interest is the value stored in that field and, since our field is already initialized, it's highly probable that &lt;i&gt;Items&lt;/i&gt; won't be referenced from other methods in this class. Why, then, should we need to declare &lt;i&gt;Items&lt;/i&gt; at all?&lt;/div&gt;&lt;div class="p"&gt;This is a new alternative technique for delegating interface implementation:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Foo &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  &lt;b&gt;interface&lt;/b&gt; IEnumerable := ['Freya', 'rules!'];&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;Now we can merge all what we need for the delegation in a single clause! Just think in all that can be done with this new feature. We have used a string list in our example, but it will be more common to find a regular constructor call as initializer:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Foo &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  &lt;b&gt;interface&lt;/b&gt; IEnumerator := &lt;b&gt;new&lt;/b&gt; DriveEnumerator;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;In short, now we have two kinds of delegation clauses. One of them refers to a field or property in the same class. In this case, you can initialize the field or property in a constructor, or using a field or property initializer. The second and shorter form assigns directly a value in the delegation clause using an initializer. In this case, the compiler declares a hidden field inside the class and takes care of its initialization.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113736209069072156?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113736209069072156/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113736209069072156' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113736209069072156'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113736209069072156'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/01/more-on-interface-delegations.html' title='More on interface delegations'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpEelI1UL2I/AAAAAAAAACo/fqFjPR6jqAg/s72-c/mask.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113717376229166623</id><published>2006-01-13T18:00:00.000+01:00</published><updated>2008-12-11T19:25:01.424+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='return'/><category scheme='http://www.blogger.com/atom/ns#' term='optimization'/><title type='text'>Enhancing code quality</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 4px; border-style: none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpCyI41ULyI/AAAAAAAAACI/jxyqnfhrHmg/s320/q.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;It's an open question&lt;/span&gt; whether a compiler writer targeting the CLR should waste some time optimizing generated code. After all, the JIT compiler in .NET 2.0 is almost a miracle of software engineering. In any case, I think it pays, at least when we talk about very simple optimizations with an immediate impact in code.&lt;/div&gt;&lt;div class="p"&gt;If you're compiling a Pascal-like language as Freya, function result assignment may introduce extra bytes in the code stream. Consider this simple access method:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#006600;"&gt;// From a generic stack implementation.&lt;/span&gt;&lt;br /&gt;&lt;b&gt;property&lt;/b&gt; Top: X;&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;  Result := Items[Count - 1];&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;This was the output from the initial compiler implementation, as reported by &lt;a href="http://www.aisto.com/roeder/dotnet" target="_blank" title="Reflector"&gt;Reflector&lt;/a&gt;:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#006600;"&gt;  // Code size: 22 bytes&lt;/span&gt;&lt;br /&gt;  &lt;strong&gt;.maxstack&lt;/strong&gt; 3&lt;br /&gt;  &lt;strong&gt;.locals init&lt;/strong&gt;(!0 local1)&lt;br /&gt;  ldarg.0&lt;br /&gt;  ldfld !0[] Stack`1&lt;!0&gt;::Items&lt;br /&gt;  ldarg.0&lt;br /&gt;  ldfld int32 Stack`&lt;!0&gt;::Count&lt;br /&gt;  ldc.i4.1&lt;br /&gt;  sub&lt;br /&gt;  ldelem.any !0&lt;br /&gt;  stloc.0&lt;br /&gt;  ldloc.0&lt;br /&gt;  ret&lt;/pre&gt;&lt;div class="p"&gt;What's that &lt;i&gt;local1&lt;/i&gt; local variable? That's simply a direct and naive implementation of the old and good &lt;i&gt;Result&lt;/i&gt;! As you can see, there's a silly store/load code sequence at the end of the method.&lt;/div&gt;&lt;div class="p"&gt;I've just added to the Freya compiler a very simple and fast algorithm to detect proper function returns, in true C/C# fashion. Now, the compiler generates this code for the same Freya method:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#006600;"&gt;  // Code size: 20 bytes&lt;/span&gt;&lt;br /&gt;  &lt;strong&gt;.maxstack&lt;/strong&gt; 3&lt;br /&gt;  ldarg.0&lt;br /&gt;  ldfld !0[] Stack`1&lt;!0&gt;::Items&lt;br /&gt;  ldarg.0&lt;br /&gt;  ldfld int32 Stack`&lt;!0&gt;::Count&lt;br /&gt;  ldc.i4.1&lt;br /&gt;  sub&lt;br /&gt;  ldelem.any !0&lt;br /&gt;  ret&lt;/pre&gt;&lt;div class="p"&gt;There's no need now for a local variable, and the code stream is two bytes shorter. How will this affect the JIT output? Well, it's highly probable that the JIT compiler would have detected the redundant sequence in the first example and removed it. However, with the new compiled code, the JIT compiler has less work to perform and the application will load faster. You should note that our algorithm is able to detect the return pattern in code like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; Silly(Value: Integer): String;&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;  Console.WriteLine(Value);&lt;br /&gt;  &lt;strong&gt;case&lt;/strong&gt; Value &lt;strong&gt;of&lt;/strong&gt;&lt;br /&gt;    v1..v2: Result := &lt;span style="color:#330000;"&gt;'Whatever'&lt;/span&gt;;&lt;br /&gt;    v3: Result := &lt;span style="color:#330000;"&gt;'Oops'&lt;/span&gt;;&lt;br /&gt;    v150..v300:&lt;br /&gt;      &lt;strong&gt;if &lt;/strong&gt;Value = 200 &lt;strong&gt;then&lt;/strong&gt;&lt;br /&gt;        Result := &lt;span style="color:#330000;"&gt;'Yikes'&lt;/span&gt;&lt;br /&gt;      &lt;strong&gt;else&lt;/strong&gt;&lt;br /&gt;        Result := &lt;span style="color:#330000;"&gt;'Uh-oh'&lt;/span&gt;;&lt;br /&gt;  &lt;strong&gt;else&lt;/strong&gt;&lt;br /&gt;    Result := &lt;span style="color:#330000;"&gt;'That''s all...'&lt;/span&gt;&lt;br /&gt;  &lt;strong&gt;end&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Let's see another simple and useful optimization. How would you translate this?&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; Value &lt;&gt; 0 &lt;strong&gt;then&lt;/strong&gt;&lt;br /&gt;  DoSomething;&lt;/pre&gt;&lt;div class="p"&gt;My initial implementation would have missed the fact we are comparing for equality with the zero constant, and would have generated something like this:&lt;/div&gt;&lt;pre class="p"&gt;  &lt;span style="color:#006600;"&gt;// Load Value...&lt;/span&gt;&lt;br /&gt;  ldc.i4.0&lt;br /&gt;  beq @@1&lt;br /&gt;  &lt;span style="color:#006600;"&gt;// DoSomething...&lt;/span&gt;&lt;br /&gt;@@1:&lt;/pre&gt;&lt;div class="p"&gt;Actually, this is a lot better, and it's just what we're doing now:&lt;/div&gt;&lt;pre class="p"&gt;  &lt;font color=#006600&gt;// Load Value...&lt;/font&gt;&lt;br /&gt;  brtrue @@1&lt;br /&gt;  &lt;font color=#006600&gt;// DoSomething...&lt;/font&gt;&lt;br /&gt;@@1:&lt;/pre&gt;&lt;div class="p"&gt;Despite the fact that the CLR offers a "decent" type for &lt;em&gt;Boolean&lt;/em&gt;, IL code still allows you to use a non null constant as an equivalent of &lt;b&gt;true&lt;/b&gt;. In this case, we're saving just a byte. But remember: the shorter code, the faster it loads...&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113717376229166623?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113717376229166623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113717376229166623' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113717376229166623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113717376229166623'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/01/enhancing-code-quality.html' title='Enhancing code quality'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpCyI41ULyI/AAAAAAAAACI/jxyqnfhrHmg/s72-c/q.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113665915657235739</id><published>2006-01-07T18:46:00.000+01:00</published><updated>2007-07-07T22:46:48.396+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='debugging'/><title type='text'>Debugging Freya applications</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;I have just added&lt;/span&gt; PDB file generation to the Freya compiler, to support debugging. Piece of cake! It has been extremely easy… so far.&lt;/div&gt;&lt;ol style="line-height: normal"&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Mark the dynamic assembly with &lt;em&gt;DebuggableAttribute&lt;/em&gt;, passing both the &lt;em&gt;DisableOptimizations&lt;/em&gt; and &lt;em&gt;Default&lt;/em&gt; constants to the attribute constructor.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Create the dynamic module as "debuggable", passing &lt;strong&gt;true&lt;/strong&gt; as the last parameter for &lt;em&gt;AssemblyBuilder.DefineDynamicModule&lt;/em&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Associate an &lt;em&gt;ISymbolDocumentWriter&lt;/em&gt; to each source file in the project. Use the &lt;em&gt;DefineDocument&lt;/em&gt; method from &lt;em&gt;MethodBuilder&lt;/em&gt; for this task.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Since every node in the Abstract Syntax Tree has a &lt;em&gt;SourceRange&lt;/em&gt; field pointing to its source file, each AST node, including statement nodes, may access their corresponding &lt;em&gt;ISymbolDocumentWriter&lt;/em&gt; instance.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Code generation for statements is achieved by calling the &lt;em&gt;Generate&lt;/em&gt; virtual method defined at the root &lt;em&gt;AstStatement&lt;/em&gt; class. I just inserted a call to &lt;em&gt;ILGenerator.MarkSequencePoint&lt;/em&gt; at the very beginning of the base &lt;em&gt;Generate&lt;/em&gt; method. This &lt;em&gt;Reflection.Emit&lt;/em&gt; method requires an &lt;em&gt;ISymbolDocumentWriter&lt;/em&gt; as its first parameter, and a range inside the source file... and both things can be extracted from the &lt;em&gt;SourceRange&lt;/em&gt; stored at the node.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Additionally, local variables declared by the programmed must apply the &lt;em&gt;SetLocalSymInfo&lt;/em&gt; method to their local variable builders, since IL code does not contain metadata about the names of local variables.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="p"&gt;This is all what you need to load a Freya source file in Microsoft's CLR Debugger, as shown in this image:&lt;/div&gt;&lt;p align="center"&gt;&lt;a href="http://www.marteens.com/freya/images/dbgfull.gif" target="_blank"&gt;&lt;img src="http://www.marteens.com/freya/images/dbgsml.gif" align="center" border="0" alt="A Freya debugging session with Microsoft's CLR Debugger" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="p"&gt;You can find more information on this topic at &lt;a href="http://blogs.msdn.com/jmstall/archive/2005/02/03/366429.aspx" target="_blank"&gt;Mike Stall&lt;/a&gt;'s .NET Debugging blog. Of course, this is just a first step towards integrating the compiler in the Visual Studio 2005 IDE.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113665915657235739?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113665915657235739/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113665915657235739' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113665915657235739'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113665915657235739'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/01/debugging-freya-applications.html' title='Debugging Freya applications'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113621615134726535</id><published>2006-01-02T15:51:00.000+01:00</published><updated>2008-12-11T19:25:01.664+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='initializer'/><category scheme='http://www.blogger.com/atom/ns#' term='creation'/><title type='text'>Object initializers</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;Take a look at&lt;/span&gt; this code:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// C# 3.0&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; a = &lt;strong&gt;new&lt;/strong&gt; Point { X = 0, Y = 1 };&lt;/pre&gt;&lt;div class="p"&gt;This fragment has been copied from the C# 3.0 specification preview. According to that document, the above instruction should be interpreted as:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// C# 3.0&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; a = &lt;strong&gt;new&lt;/strong&gt; Point();&lt;br /&gt;a.X = 0;&lt;br /&gt;a.Y = 1;&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 6px; border-style: none;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/RpI9AI1UL7I/AAAAAAAAADQ/1p1_kuDyvqA/s320/oinit.jpg" border="0" /&gt;So, we have a "normal" instance construction followed by property or field assignments. Does this remind you any other existing C# feature? Sure, that's how custom attributes are instantiated. Custom attributes mix positional parameters corresponding to constructor parameters, and named parameters, which must be translated as post-construction assignments. We could extend the syntax from custom attributes to object creation:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// A proposal&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; a = &lt;strong&gt;new&lt;/strong&gt; Point(X = 0, Y = 1);&lt;/pre&gt;&lt;div class="p"&gt;Of course: I know object initializers were proposed for C# 3.0 as part of the support for anonymous types. In C# 3.o, you could drop the "constructor part" like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// Anonymous types in C# 3.0&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; p1 = &lt;strong&gt;new&lt;/strong&gt; {&lt;br /&gt;    Name = "Lawnmower", Price = 495.00 };&lt;/pre&gt;&lt;div class="p"&gt;But then, why not this?&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// Anonymous types in C# 3.0: a proposal&lt;/span&gt;&lt;br /&gt;v&lt;strong&gt;ar&lt;/strong&gt; p1 = &lt;strong&gt;new&lt;/strong&gt;(&lt;br /&gt;    Name = "Lawnmower", Price = 495.00 );&lt;/pre&gt;&lt;div class="p"&gt;Is this a good idea? I still have to check the 3.0 proposal, to see whether there is any other reason to prefer the curly braces initializers (nested initializers, for instance). In any case, I think it's better for Freya to adopt the "custom attribute initialization syntax" for object initializers: the literal translation of C#'s curly braces would be a &lt;strong&gt;begin&lt;/strong&gt;/&lt;strong&gt;end&lt;/strong&gt; pair. So, we could initialize a Point object in Freya this way:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#009900;"&gt;// A proposal for Freya 1.0&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; p := &lt;strong&gt;new&lt;/strong&gt; Point(X := 0, Y :=0);&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt; tb := &lt;strong&gt;new&lt;/strong&gt; DataTable(&lt;br /&gt;  'Customers', CaseSensitive := &lt;strong&gt;true&lt;/strong&gt;);&lt;/pre&gt;&lt;div class="p"&gt;How do you like it?&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113621615134726535?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113621615134726535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113621615134726535' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113621615134726535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113621615134726535'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2006/01/object-initializers.html' title='Object initializers'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/RpI9AI1UL7I/AAAAAAAAADQ/1p1_kuDyvqA/s72-c/oinit.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113477626273899973</id><published>2005-12-17T00:32:00.000+01:00</published><updated>2008-12-11T19:25:01.702+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='genericity'/><title type='text'>It works!</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 5px;border-style:none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpJAEY1UL8I/AAAAAAAAADY/vAr5ecCg-1g/s320/model.jpg" border="0" /&gt;&lt;span class="p"&gt;I've just connected the&lt;/span&gt; code required for generating generic type definitions in the Freya compiler... and it works fine! Freya has been consuming imported generic types for a while, but now we can also compile our own generic type definitions.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113477626273899973?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113477626273899973/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113477626273899973' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113477626273899973'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113477626273899973'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/12/it-works.html' title='It works!'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpJAEY1UL8I/AAAAAAAAADY/vAr5ecCg-1g/s72-c/model.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113457570359226340</id><published>2005-12-14T16:06:00.000+01:00</published><updated>2007-07-09T18:46:46.174+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='locals'/><category scheme='http://www.blogger.com/atom/ns#' term='declaration'/><title type='text'>Using a statement</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;Does &lt;em&gt;StringBuilder&lt;/em&gt; implements&lt;/span&gt; &lt;em&gt;IDisposable&lt;/em&gt;? No, it doesn't. And, what about &lt;em&gt;SqlDataReader&lt;/em&gt;? Can you remember which classes implement a given interface? Is there any simple way to find it without interrupting your workflow to check the inline help? Then, why does &lt;strong&gt;using&lt;/strong&gt; fails to compile when the instance type doesn't implement that interface?&lt;/div&gt;&lt;div class="p"&gt;In Freya, this is no longer a requirement. When the instance type in a &lt;strong&gt;using&lt;/strong&gt; statement does not implement &lt;em&gt;IDisposable&lt;/em&gt;, the compiler does not complain, but no finalization is generated for the statement:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;using var&lt;/b&gt; sb := &lt;b&gt;new&lt;/b&gt; StringBuilder &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    sb.Append(month);&lt;br /&gt;    sb.Append('/');&lt;br /&gt;    sb.Append(day);&lt;br /&gt;    sb.Append('/');&lt;br /&gt;    sb.Append(year);&lt;br /&gt;    Console.WriteLine(sb);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Another new experimental feature is shown in the previous example: inline variable declaration, in true C# 3.0 fashion. Actually, this kind of simple type inference was already present in Freya. What's new is the presence of &lt;strong&gt;var&lt;/strong&gt; to make clear we are introducing a (block scoped) local variable.&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;for var&lt;/b&gt; i := 0 &lt;b&gt;to&lt;/b&gt; list.Count - 1 &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;    &lt;em&gt;// See note below...&lt;/em&gt;&lt;br /&gt;    &lt;b&gt;with var&lt;/b&gt; item := list[i] &lt;strong&gt;do&lt;/strong&gt;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Console.Write(item);&lt;br /&gt;        Console.Write(': ');&lt;br /&gt;        Console.WriteLine(integer(item));&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Without &lt;strong&gt;var&lt;/strong&gt;, it would be hard to tell whether we are using an already declared control variable, or just asking for a new local control variable in statements like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;em&gt;// See note below...&lt;/em&gt;&lt;br /&gt;&lt;b&gt;for&lt;/b&gt; i := low &lt;b&gt;to&lt;/b&gt; high &lt;b&gt;do&lt;/b&gt; ...&lt;/pre&gt;&lt;div class="p"&gt;Have you noticed the change in the &lt;strong&gt;with&lt;/strong&gt; statement? We have sacrificed the old semantics of the traditional Pascalian &lt;strong&gt;with&lt;/strong&gt;. Now, this statement is used to introduce new local variables and an associated scope for them, but there's no assumed prefix as in Pascal and Delphi.&lt;/div&gt;&lt;div class="p" style="background-color: #d0d0ff; border-top: 1px dotted #a0a0bf; border-bottom: 1px dotted #a0a0bf; line-height: normal; padding: 4px 8px 4px 8px;"&gt;&lt;b&gt;Updated&lt;/b&gt;:The &lt;b&gt;with&lt;/b&gt; statement has been dropped from Freya, since it could be substituted by our "extended" &lt;b&gt;using&lt;/b&gt; statement. However, we had to make another change in the semantics of &lt;b&gt;using&lt;/b&gt;: value type variables declared at the header are no longer read only. That's not a great deal, since &lt;i&gt;IDisposable&lt;/i&gt; is almost always implemented by classes instead of records. Finally, Freya, as C#, requires a dedicated variable in &lt;b&gt;for&lt;/b&gt; statement. You cannot "recycle" anymore an already existing local, as in Delphi.&lt;/div&gt;&lt;div class="p"&gt;And this is another example of type inference for local variables:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;for var&lt;/b&gt; color &lt;b&gt;in&lt;/b&gt; [Colors.White, Colors.Blue] &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; color &lt;b&gt;in&lt;/b&gt; [Colors.Red, Colors.Blue] &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        Console.WriteLine('Match!');&lt;br /&gt;        Break;&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Brackets are used to construct arrays, and the &lt;strong&gt;in&lt;/strong&gt; operator in the conditional statement translates as a call to &lt;em&gt;ICollection&lt;&gt;.Contains&lt;/em&gt;, in the most general case. In this simple example, the compiler optimizes the check and inlines the expression.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113457570359226340?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113457570359226340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113457570359226340' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113457570359226340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113457570359226340'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/12/using-statement.html' title='Using a statement'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-113144924992701529</id><published>2005-11-08T12:13:00.000+01:00</published><updated>2008-12-11T19:25:01.932+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='typecast'/><title type='text'>Forced typecasts</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 4px; border-style:none;" src="http://3.bp.blogspot.com/_QWaXq7W8dVI/RpEjz41UL3I/AAAAAAAAACw/hobAKbkMQqU/s320/drop.jpg" border="0" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;The design of the&lt;/span&gt; typecasting operators for Freya has been troublesome:&lt;/div&gt;&lt;ul style="line-height: normal;"&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Delphi uses the &lt;strong&gt;as&lt;/strong&gt; operator as a "forced typecast": it checks first whether the conversion is possible, and when it's not, it throws an exception.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;The other typecast operation is an unchecked conversion (actually, it's a compile time cast): just do what I say, even when it's plainly wrong. Syntactically, you must use the type name as a monoparametric function.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;There's no "atomic" tentative typecast in Delphi. To simulate it, test the source type using the &lt;strong&gt;is&lt;/strong&gt; operator, and if the answer is affirmative, proceed with the compile time typecast.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="p"&gt;On the contrary, this is what happens in C#:&lt;/div&gt;&lt;ul style="line-height: normal;"&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Tentative casting is implemented by the &lt;strong&gt;as&lt;/strong&gt; operator.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;The widely known compile-time typecast inherited from C, is now the "forced" typecast operator.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;There's also an is operator in C#, but there is no compile-time, unchecked typecast.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="p"&gt;I felt inclined from the first time to use &lt;strong&gt;as&lt;/strong&gt; as in C#, but I was worried about the confusion this could cause in a Delphi programmer. And then, how should I implement the functional style typecast?&lt;/div&gt;&lt;div class="p"&gt;Our final design resembles the design decisions in C#:&lt;/div&gt;&lt;ul style="line-height: normal;"&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Tentative casting in Freya is implemented by the &lt;strong&gt;as&lt;/strong&gt; operator.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;The functional style typecast means a forced typecast in Freya: if the conversion is not possible, it throws an exception.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Of course, we keep the meaning of the &lt;strong&gt;is&lt;/strong&gt; operator.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="p"&gt;Let's see some code:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;br /&gt;   o: Object;&lt;br /&gt;   b: Button;&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;   o := &lt;strong&gt;new&lt;/strong&gt; Label;&lt;br /&gt;&lt;span style="color:#009900;"&gt;   &lt;i&gt;// A tentative typecast.&lt;/i&gt;&lt;br /&gt;   &lt;i&gt;// A nil reference is stored.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;   b := o &lt;strong&gt;as&lt;/strong&gt; Button;&lt;br /&gt;&lt;span style="color:#009900;"&gt;   &lt;i&gt;// A forced typecast.&lt;/i&gt;&lt;br /&gt;   &lt;i&gt;// An exception is raised.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;   b := Button(o);&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Of course, I'm writing this after implementing the support for this operations in the Freya compiler.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-113144924992701529?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/113144924992701529/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=113144924992701529' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113144924992701529'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/113144924992701529'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/11/forced-typecasts.html' title='Forced typecasts'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_QWaXq7W8dVI/RpEjz41UL3I/AAAAAAAAACw/hobAKbkMQqU/s72-c/drop.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-112120991931127509</id><published>2005-07-13T00:54:00.000+02:00</published><updated>2007-07-07T22:48:49.750+02:00</updated><title type='text'>There's a time and a place...</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;I've been lazy&lt;/span&gt; for a while. Actually, I've been working in a new book on ASP.NET v2.0 and some courses. Anyway, I haven't found enough time for Freya, but I'm back...&lt;/div&gt;&lt;div class="p"&gt;&lt;a href="http://www.marteens.com/freya" target="_blank"&gt;&lt;img alt="The new logo" src="http://www.marteens.com/freya/biglogo.jpg" align="left" border="0" style="margin: 2px 8px 0 0;" /&gt;&lt;/a&gt;We have a new "logo" for Freya! Nothing to be proud of, but we have finally a logo. The capital F has been drawn with the French Script font. It has been sheared, rotated and bevelled using &lt;a href="http://www.xara.com"&gt;Xara X1 &lt;/a&gt;(heartily recommended!). There's a full update of the Freya's home on the way, and it should be ready by the end of this week.&lt;/div&gt;&lt;div class="p"&gt;I still have to move the compiler source to the Visual Studio beta 2; right now, it compiles with the Visual C# Express beta 2. Next, I will implement the code to enforce accessibility levels. That's a funny feature of the CLR: even if your compiler doesn't check your access rights regarding a class member, the runtime will detect the violation and will signal it as a runtime error. There's no way for cheating encapsulation in the framework.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-112120991931127509?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/112120991931127509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=112120991931127509' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/112120991931127509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/112120991931127509'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/07/theres-time-and-place.html' title='There&apos;s a time and a place...'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111616584817765339</id><published>2005-05-15T15:55:00.000+02:00</published><updated>2007-11-30T00:53:13.555+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='method'/><category scheme='http://www.blogger.com/atom/ns#' term='member'/><category scheme='http://www.blogger.com/atom/ns#' term='declaration'/><title type='text'>The Merging Pot</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;Since Freya is a split&lt;/span&gt; language, the compiler must merge different parts of the source code. We have a handful of syntactic structures that require merging:&lt;/div&gt;&lt;ul style="line-height: normal;"&gt;&lt;li&gt;First, each class declaration must find its corresponding implementation section. Both parts should be located in the same source file.&lt;/li&gt;&lt;li&gt;Each event, property and method must match its implementation.&lt;/li&gt;&lt;li&gt;Methods, events and properties from the implementation that don’t match with a declared class member, are added as private members of the class.&lt;/li&gt;&lt;li&gt;Finally, there are features that always belong to the implementation section: class constructors, and interface delegations.&lt;/li&gt;&lt;/ul&gt;&lt;div class="p"&gt;Easy? Well, not at all. Suppose we have this declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; WhatEver(I: Integer): String;&lt;/pre&gt;&lt;div class="p"&gt;Does this method match with the following implementation?&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; WhatEver(I: System.Int32): System.String;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Now you can see the problem: a type reference may omit the namespace thanks to a using clause, and several primitive types have synonyms. That’s the reason why we can’t merge method implementations when we match a class with its implementation section. We must register namespaces and types, both from the referenced assemblies and user declarations.&lt;/div&gt;&lt;div class="p"&gt;Please note that we face a similar challenge when identifying a class implementation section. This is the easiest case:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;namespace&lt;/b&gt; Freya.DataStructures;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    MyClass = &lt;b&gt;class&lt;/b&gt;;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; MyClass &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;But, what happens when we find this?&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Freya.DataStructures.MyClass &lt;b&gt;is&lt;/b&gt;&lt;/pre&gt;&lt;div class="p"&gt;To avoid complications, Freya requires that the type reference in the implementation header should not include the namespace part.&lt;/div&gt;&lt;div class="p"&gt;Another Freya feature complicates things: nested type references. You can declare a nested type along with the enclosing class, as in C#, but you can also put a simple stub inside the enclosing class declaration and declare the actual type later.&lt;/div&gt;&lt;pre class="p"&gt;LinkedList = &lt;b&gt;class&lt;/b&gt;[X]&lt;br /&gt;&lt;b&gt;protected&lt;/b&gt;&lt;br /&gt;    Node = &lt;b&gt;class&lt;/b&gt;;&lt;br /&gt;    ...&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;LinkedList[X].Node = &lt;b&gt;class&lt;/b&gt;&lt;br /&gt;    ...&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;The implementation section for the linked node class should be like this:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; LinkedList[X].Node &lt;b&gt;is&lt;/b&gt;&lt;/pre&gt;&lt;div class="p"&gt;Have you noticed I didn’t mention merging any namespaces? I could declare types belonging to the namespace &lt;em&gt;Freya.DataStructures&lt;/em&gt; in several source files? Should we merge those “partial” namespace sections? No, we shouldn’t. Each namespace section may have different using clauses, and these clauses plays an important role in the resolution algorithm.&lt;/div&gt;&lt;div class="p"&gt;Actually, we’re missing another merging algorithm: gathering parts from a &lt;em&gt;partial class&lt;/em&gt;. So far, we haven’t added partial class support to Freya. We’re planning their design and implementation in the short term.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111616584817765339?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111616584817765339/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111616584817765339' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111616584817765339'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111616584817765339'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/05/merging-pot.html' title='The Merging Pot'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111610534966450628</id><published>2005-05-14T23:12:00.000+02:00</published><updated>2007-11-30T00:54:35.915+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='event'/><category scheme='http://www.blogger.com/atom/ns#' term='property'/><category scheme='http://www.blogger.com/atom/ns#' term='field'/><title type='text'>On properties and events</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;The simplest way to declare&lt;/span&gt; an event in C#, only requires from you to provide the event declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;event&lt;/strong&gt; EventHandler Click;&lt;/pre&gt;&lt;div class="p"&gt;Then, the C# compiler adds the implementation:&lt;/div&gt;&lt;ol style="line-height: normal;"&gt;&lt;li&gt;A private field of the corresponding delegate type.&lt;/li&gt;&lt;li&gt;A method called &lt;em&gt;add_Click&lt;/em&gt; for adding handlers to the delegate chain.&lt;/li&gt;&lt;li&gt;A symmetric &lt;em&gt;remove_Click&lt;/em&gt; method, for removing handlers.&lt;/li&gt;&lt;/ol&gt;&lt;div class="p"&gt;Outside the declaring class, the only operations you can perform with the event is adding and removing event handlers, using the now famous shortcut:&lt;/div&gt;&lt;pre class="p"&gt;myClass.Click += myClickHandler;&lt;/pre&gt;&lt;div class="p"&gt;By the way, you can’t fire the event from outside the declaring class! That’s encapsulation. However, things are different inside the class. The compiler generates a private field, and it doesn’t let you access that field... apparently. Actually, whenever you mention the event identifier from inside the declaring class, the compiler handles the identifier as if it were the private field. You’re free to do with the event identifier whatever it’s allowed with a delegate type field.&lt;/div&gt;&lt;div class="p"&gt;Events in Freya are identical to C# events. We also implemented a variant of this trick to be used with properties (credits belong to the authors of &lt;a href="http://www.chromesville.com"&gt;Chrome&lt;/a&gt;, another .NET language inspired in Delphi). When you’re designing a class, you often declare fields that should be transformed later into properties… or, on the contrary, you start with properties that map directly to a naked field with the same type. How much code do you need to write in this very simple and frequent case? A lot, if you’re using C#. You must declare the field, then the property and most of the times, both access methods.&lt;/div&gt;&lt;div class="p"&gt;In Freya, you only need to declare the property:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;property&lt;/strong&gt; Caption: string;&lt;/pre&gt;&lt;div class="p"&gt;If we don’t provide an implementation for &lt;em&gt;Caption&lt;/em&gt;, Freya will generate a private field and two access methods with proper signatures. Outside the declaring class, you will only see the property. However, inside the class, any reference to the property will be translated as a reference to the underlying field. In our previous example, &lt;em&gt;Caption&lt;/em&gt; is a read/write property. If you want a read only property, you only need to add the &lt;strong&gt;readonly&lt;/strong&gt; keyword to the declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;property&lt;/strong&gt; Caption: string; &lt;strong&gt;readonly&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;You could even declare an initializer for the property:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;property&lt;/strong&gt; Caption: string := 'Form1';&lt;/pre&gt;&lt;div class="p"&gt;That’s an internal access, of course, and the compiler just moves the initializer to the private field. Of course, you can also “initialize a property” when the property has an explicit setter, but then, the initialization code calls the setter method when creating a new object instance.&lt;/div&gt;&lt;div class="p"&gt;As a final remark, we allow grouping related properties in a single declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;property&lt;/strong&gt; X, Y: Double := 0;&lt;/pre&gt;&lt;div class="p"&gt;The initializer applies to both properties, of course, and it should be evaluated only once.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111610534966450628?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111610534966450628/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111610534966450628' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111610534966450628'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111610534966450628'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/05/on-properties-and-events.html' title='On properties and events'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111610129867386910</id><published>2005-05-14T22:06:00.000+02:00</published><updated>2008-12-11T19:25:02.109+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='method'/><category scheme='http://www.blogger.com/atom/ns#' term='implementation'/><category scheme='http://www.blogger.com/atom/ns#' term='declaration'/><title type='text'>Just an implementation detail...</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 4px;border-style:none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpJ2nY1UMAI/AAAAAAAAAD4/P2G85IP8skg/s320/mglass.jpg" border="0" /&gt;&lt;span class="p"&gt;One of my favorite&lt;/span&gt; features in Freya is the syntax of compiling units. Freya was initially inspired in Delphi, so it inherited what I call the &lt;em&gt;“split organization”&lt;/em&gt;: declarations and implementations belong to different sections. Every compiling unit in Delphi has just an interface and an implementation section. Freya features a mutation of this structure. Every file in Freya is divided into one or more declaration sections, and zero or more implementation sections. Declaration sections are very similar to interface sections in Delphi… except that Freya doesn’t use the&lt;strong&gt; interface&lt;/strong&gt; keyword. Declaration sections always start with public or private, and each section may contain several type declarations.&lt;/div&gt;&lt;div class="p"&gt;Implementation sections deviate more from the Delphi’s way. Each implementation section is associated to a single type declaration. Thus, all methods from the same type are always grouped together.&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;namespace&lt;/b&gt; Freya.Compiler;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    Class01 = ...&lt;br /&gt;    Class02 = ...&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Class01 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Class02 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;Besides forcing a better grouping of method bodies, there’s another immediate advantage in this organization: you don’t need to repeat the type name in the headers of method implementations, as in Delphi and C++:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Class01 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Method01: string; ...&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;iterator&lt;/b&gt; Items: WhatEver; ...&lt;/pre&gt;&lt;div class="p"&gt;But this is only the beginning. This syntax allows what I call the &lt;em&gt;&lt;span style="color:#006600;"&gt;“just an implementation detail”&lt;/span&gt;&lt;/em&gt; attitude. Let’s say you need a private field for &lt;em&gt;Class01&lt;/em&gt;. Why should you include its declaration in the class declaration? It’s just an implementation detail, after all! Consequently, Freya allows you to declare the field in the implementation section of &lt;em&gt;Class01&lt;/em&gt;:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Class01 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;var&lt;/b&gt; Field01: Double;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; Method01: string ...&lt;/pre&gt;&lt;div class="p"&gt;This trick can be repeated with constants, methods, constructors and even properties. The rule is: whenever you find a member in an implementation section that hasn’t be declared with the class, the compiler will consider it as a private member of that class.&lt;/div&gt;&lt;div class="p"&gt;More? In &lt;a href="http://freyalang.blogspot.com/2005/04/implementing-interface-mappings.html"&gt;a previous post&lt;/a&gt;, I demonstrated interface implementation by delegation:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    Class02 = &lt;b&gt;class&lt;/b&gt;(IEnumerable);&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Class02 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;var&lt;/b&gt; strings: &lt;b&gt;array of&lt;/b&gt; string =&lt;br /&gt;        ['Freya', 'rules!'];&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;interface&lt;/b&gt; IEnumerable = strings;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;The implementation of an interface is a private business, so its place is the implementation section for the class. What about explicit method implementations, as in C#? Another implementation detail! You don’t have to declare the method in the class declaration (actually, you can’t):&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;implementation for&lt;/b&gt; Class02 &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    &lt;b&gt;method&lt;/b&gt; IEnumerable.GetEnumerator: IEnumerator;&lt;br /&gt;    &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;        ...&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;By the way, there’s an interesting link between how we designed this Freya feature and Quenya! But that’s a different story, and it deserves another post...&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111610129867386910?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111610129867386910/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111610129867386910' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111610129867386910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111610129867386910'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/05/just-implementation-detail.html' title='Just an implementation detail...'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpJ2nY1UMAI/AAAAAAAAAD4/P2G85IP8skg/s72-c/mglass.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111585298663483999</id><published>2005-05-12T01:02:00.000+02:00</published><updated>2008-12-11T19:25:02.220+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='creation'/><title type='text'>Creation statements</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;A minor but useful&lt;/span&gt; enhancement: take a look to this common code written in C#:&lt;/div&gt;&lt;pre class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 4px;border-style:none;" src="http://3.bp.blogspot.com/_QWaXq7W8dVI/RpJtOo1UL_I/AAAAAAAAADw/Y9m07mFHvxk/s320/tophat.jpg" border="0" /&gt;Dictionary&amp;lt;&lt;b&gt;int&lt;/b&gt;,&lt;b&gt;string&lt;/b&gt;&amp;gt; d =&lt;br /&gt;    &lt;b&gt;new&lt;/b&gt; Dictionary&amp;lt;&lt;b&gt;int&lt;/b&gt;,&lt;b&gt;string&lt;/b&gt;&amp;gt;();&lt;/pre&gt;&lt;div class="p"&gt;Why do we have to write the type name twice? Well, put the blame in polymorphism. In our example, the declared type of the local variable and the type of the created instance are the same. But we could have specified a class descending from Dictionary, if any, in the right side expression. Easy, huh?&lt;/div&gt;&lt;div class="p"&gt;However, most of the times both types will be the same. Could we avoid the duplication? If you're using Freya, the answer is YES:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;br /&gt;  d: Dictionary[Integer,String];&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;  &lt;strong&gt;new&lt;/strong&gt; d;&lt;br /&gt;  &lt;span style="color:#006600;"&gt;// Now, a parameterized constructor:&lt;/span&gt;&lt;br /&gt;  &lt;strong&gt;new&lt;/strong&gt; d(1024);&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;A useful time saver, isn't it?&lt;/div&gt;&lt;hr class="p"&gt;&lt;div class="p" style="background-color: #dfdfff;"&gt;&lt;b&gt;Updated&lt;/b&gt;: We have dropped the &lt;b&gt;new&lt;/b&gt; statement after the publication of the first draft for C# 3.0. Now we have variable declarations with implicit types, as in the C# 3.0 proposal.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111585298663483999?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111585298663483999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111585298663483999' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111585298663483999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111585298663483999'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/05/creation-statements.html' title='Creation statements'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_QWaXq7W8dVI/RpJtOo1UL_I/AAAAAAAAADw/Y9m07mFHvxk/s72-c/tophat.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111559458429661841</id><published>2005-05-09T01:09:00.000+02:00</published><updated>2008-12-11T19:25:02.394+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='array'/><category scheme='http://www.blogger.com/atom/ns#' term='constant'/><category scheme='http://www.blogger.com/atom/ns#' term='literal'/><title type='text'>Arrays, sets, brackets and curly braces</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;I'm afraid there're&lt;/span&gt; some big changes on the way, in order to accommodate both array and set literal constructors in Freya. We gave priority to the implementation of arrays, in order to match C#'s functionality as soon as possible. You can write "literal arrays" in Freya using a bracket-delimited list:&lt;/div&gt;&lt;pre class="p"&gt;[1, 2, i]&lt;/pre&gt;&lt;div class="p"&gt;That would be the equivalent of C#'s:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;new int&lt;/strong&gt;[] { 1, 2, i }&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:0 0 10px 10px;border-style:none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpJpe41UL-I/AAAAAAAAADo/JCLtV77XENY/s320/road.jpg" border="0" /&gt;Freya's notation is shorter, but we have to check yet how it behaves together with method overloading. C#'s on-the-fly arrays has a clear hint about the array type. I'm already inferring the array type from the item types. This is not a problem with assignments. As in C#'s, Freya arrays are covariant. When a covariant array assignment is detected, the compiler checks whether the right side is a literal array constructor, as above, in order to perform the conversion at compile time.&lt;/div&gt;&lt;div class="p"&gt;Why do I mention method overloading? Well, overload resolution has a very detailed specification for C#, and most of it has been borrowed by Freya. I still have to prove that the potential ambiguity introduced by these array expressions doesn't break the algorithm. I hope it won't, since all examples tried by hand have succeeded.&lt;/div&gt;&lt;div class="p"&gt;And, what about sets? One of the goals for Freya is preserving the Set data type from Pascal, with some minor changes, such as introducing an imaginary &lt;em&gt;Set[X]&lt;/em&gt; generic class, and a big change: Freya sets won't be limited to integer subranges and enumerative types.&lt;/div&gt;&lt;div class="p"&gt;One of our problems is how we will write literal set constructors. Pascal uses brackets as delimiters, and Freya could do the same. That would bring more ambiguity to bracket delimited expressions, and I'm not happy with the preliminary results. That's the reason why I'm thinking about reintroducing curly braces as set delimiters (the role they already play in mathematical notation!). This would imply changes in multiline comments. Delphi borrowed single line comments from C++, so I don't see any reason why I shouldn't use C# delimiters for multiline comments in Freya:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#006600;"&gt;/* A Freya multiline comment */&lt;/span&gt;&lt;br /&gt;&lt;b&gt;if&lt;/b&gt; i &lt;strong&gt;in &lt;/strong&gt;{1,2,3} &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; j &lt;strong&gt;in&lt;/strong&gt; [1, 2, 3] &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;        &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;/pre&gt;&lt;div class="p"&gt;Note that we already allow the in operator along with arrays. The second condition is translated this way:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; [1,2,3].IndexOf(j) &lt;&gt; -1 &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;/pre&gt;&lt;div class="p"&gt;Of course, since this example features a constant array, the actual translation would be:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;if&lt;/b&gt; j = 1 &lt;strong&gt;or&lt;/strong&gt; j = 2 &lt;strong&gt;or&lt;/strong&gt; j = 3 &lt;b&gt;then&lt;/b&gt;&lt;br /&gt;     &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;/pre&gt;&lt;div class="p"&gt;Note that, thanks to our new and sounder precedence rules, we didn't need any parenthesis in the Boolean expression.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111559458429661841?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111559458429661841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111559458429661841' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111559458429661841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111559458429661841'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/05/arrays-sets-brackets-and-curly-braces.html' title='Arrays, sets, brackets and curly braces'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpJpe41UL-I/AAAAAAAAADo/JCLtV77XENY/s72-c/road.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111487147747764652</id><published>2005-04-30T16:24:00.000+02:00</published><updated>2008-12-11T19:25:02.673+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='constant'/><title type='text'>More about constants</title><content type='html'>&lt;div class="p"&gt;&lt;img style="float:right; margin:0 0 0px 8px; border-style: none; padding: 0;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/RsyaU6r_U4I/AAAAAAAAAEY/ghT7J8ylRAw/s320/plank.jpg" border="0" alt="" /&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Constants implementation&lt;/span&gt; finished... It has been hard, because Freya must admit this kind of declarations:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;const&lt;/b&gt; Pi = 3.14;&lt;/pre&gt;&lt;div class="p"&gt;It is the constant type what's missing in this example, as it happens in Pascal. C# always requires the type declaration:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;public const double&lt;/b&gt; Pi = 3.14;&lt;/pre&gt;&lt;div class="p"&gt;Of course, Freya also admits this syntax:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;const&lt;/b&gt; Pi: Double := 3.14;&lt;/pre&gt;&lt;div class="p"&gt;The problem with implicit types in constants is that you face two challenges at the same time: finding the data type and then, evaluating the expression. It's already solved, but it could have been easier without this kind of declarations.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111487147747764652?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111487147747764652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111487147747764652' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111487147747764652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111487147747764652'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/04/more-about-constants.html' title='More about constants'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/RsyaU6r_U4I/AAAAAAAAAEY/ghT7J8ylRAw/s72-c/plank.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111478243565797938</id><published>2005-04-29T15:26:00.000+02:00</published><updated>2008-12-11T19:25:03.209+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='constant'/><title type='text'>Constant symbols</title><content type='html'>&lt;div class="p"&gt;&lt;span class="p"&gt;I'm working right now&lt;/span&gt; in the implementation of constant symbols. This is how constants are declared in Freya:&lt;/div&gt;&lt;pre class="p"&gt;MathHelper = &lt;strong&gt;class&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;br /&gt;    &lt;strong&gt;const&lt;/strong&gt; Pi: Double := 3.141592653589;&lt;br /&gt;    &lt;strong&gt;const&lt;/strong&gt; E = 2.718281828459;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Constant can be declared inside a method implementation too:&lt;/div&gt;&lt;pre class="p"&gt;&lt;strong&gt;method &lt;/strong&gt;TrySave;&lt;br /&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;br /&gt;    MAX_ATTEMPTS = 3;&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 4px;border-style:none;" src="http://1.bp.blogspot.com/_QWaXq7W8dVI/RpJB-I1UL9I/AAAAAAAAADg/qwmfkP7jKeI/s320/pi.jpg" border="0" /&gt;Local constants are easy to implement, since they only exist at compile time, and member constants don't use them in their own definitions. On the other hand, member constants require a careful implementation. The expression in a constant declaration may contain references to other constants, as long as these references don't cause circular dependences. This is a simple example:&lt;/div&gt;&lt;pre class="p"&gt;Trigonometry = &lt;strong&gt;static class&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;br /&gt;    &lt;strong&gt;const&lt;/strong&gt; PiOver2 = MathHelper.Pi / 2.0;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;br /&gt;&lt;br /&gt;MathHelper = &lt;strong&gt;class&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;br /&gt;    &lt;strong&gt;const &lt;/strong&gt;Pi: Double := 3.141592653589;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/pre&gt;&lt;div class="p"&gt;I think I'll handle constant definitions right before performing "body resolution" on the &lt;em&gt;Abstract Syntax Tree&lt;/em&gt;. Probably, I'll gather all constants from the compiling project in a single list, to execute a &lt;em&gt;topological sort&lt;/em&gt; on this list. I have a vague idea about a brute force algorithm for topological sort, but I'm afraid I'll have to check my copy of &lt;a href="http://www.amazon.com/exec/obidos/ASIN/0201485419/callindrmarte-20"&gt;The Art of Computer Programming&lt;/a&gt; for a better approach.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111478243565797938?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111478243565797938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111478243565797938' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111478243565797938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111478243565797938'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/04/constant-symbols.html' title='Constant symbols'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_QWaXq7W8dVI/RpJB-I1UL9I/AAAAAAAAADg/qwmfkP7jKeI/s72-c/pi.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111476896156292143</id><published>2005-04-29T11:11:00.000+02:00</published><updated>2007-07-07T22:55:09.892+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='event'/><category scheme='http://www.blogger.com/atom/ns#' term='method'/><category scheme='http://www.blogger.com/atom/ns#' term='implementation'/><title type='text'>Raison d'être</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;There are a lot of programming&lt;/span&gt; languages already running in the .NET Framework. The CLR leaves almost no freedom to the designer of the object model. The "official" .NET languages are quite good and comprehensive. If you enjoy curly braces and esoteric operators, C# is your language. If you hate curly braces, you could choose a face lifted VB.NET, with its new support for operator overloading and generic types. Is there any chance for Freya in this hard competition?&lt;/div&gt;&lt;div class="p"&gt;First of all, don't get fooled: VB.NET is still a line oriented language, and that's not a minor issue. Though the newest VB.NET has almost nothing to do with Vintage Basic, I think it's not a real alternative for those, like me, who prefer a language inspired in the Algol family. The natural alternative should have been Delphi, a language also designed by Anders Hejlsberg but, alas, Borland decided that Delphi.NET should keep the compatibility with existing "native" Delphi code, and the result is a hybrid scaring monster.&lt;/div&gt;&lt;div class="p"&gt;Freya has, indeed, a winning card in the sleeve: it has been designed for .NET v2.0. You can spell it this way: Freya supports generic types since its first version, and this allows a more sound and complete type system. For instance, Freya array types are no longer special types in any sense: they are just closed generic types built over a mythical generic Array[X] class. I say "mythical" because there's no such class in the CLR: System.Array is a non generic class. Another example? Take the retorted syntax for managing delegate types in .NET v1.x languages:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;new&lt;/b&gt; EventHandler(button1_Click)&lt;/pre&gt;&lt;div class="p"&gt;There you have a constructor using a non existing type: what's the type of the constructor parameter? Sure, it is a "method group". Could you use a "method group" elsewhere in your own classes? No, you can't. It's true that C# v2.0 has simplified this syntax, so you could omit the explicit constructor call. In Freya, you can always use a method name as it were a delegate type value.&lt;/div&gt;&lt;div class="p"&gt;Also, there are several minor syntactic details favoring Freya, but I will only mention one of them here: the distinctive marker for class members, a common feature in the Algol/Pascal family languages:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;method&lt;/b&gt; DoThis(UsingThis: Integer);&lt;br /&gt;&lt;b&gt;method&lt;/b&gt; ReturnThat: String;&lt;br /&gt;&lt;span style="color:#006600;"&gt;// The class name may be ommitted&lt;/span&gt;&lt;br /&gt;&lt;b&gt;constructor&lt;/b&gt; MyClass;&lt;br /&gt;&lt;b&gt;destructor&lt;/b&gt; MyClass;&lt;br /&gt;&lt;b&gt;iterator&lt;/b&gt; LoopOverThis: DataRow;&lt;/pre&gt;&lt;div class="p"&gt;On the contrary, C/C++/C# has to fiddle with a cryptic and limiting syntax that imposes serious limitations in C# and even in the old good C++. How do you write a destructor in C++/C#?&lt;/div&gt;&lt;pre class="p"&gt;~ MyClass() {}&lt;/pre&gt;&lt;div class="p"&gt;This is probably the ugliest thing in language design you will ever meet! And how C# manages to declare an iterator? Actually, there is not such a thing in C#! Sure, we have something called "iterator blocks". How do we identify an iterator block? Well, you must read the whole block looking for &lt;strong&gt;yield&lt;/strong&gt; statements. Nice, huh? And then, you have to tell between &lt;strong&gt;yield break&lt;/strong&gt; and &lt;strong&gt;yield return&lt;/strong&gt;. Now, let's see an iterator in Freya:&lt;/div&gt;&lt;pre class="p"&gt;&lt;span style="color:#006600;"&gt;// This is real code, drawn&lt;br /&gt;// from the Freya's compiler&lt;/span&gt;&lt;br /&gt;&lt;b&gt;iterator&lt;/b&gt; Members[X(AstMember)]: X;&lt;br /&gt;&lt;span style="color:#006600;"&gt;// A generic iterator, with a&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#006600;"&gt;// base class constraint in the&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#006600;"&gt;// generic type parameter.&lt;/span&gt;&lt;br /&gt;&lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;  &lt;b&gt;for&lt;/b&gt; M: AstMember &lt;b&gt;in&lt;/b&gt; AllMembers &lt;b&gt;do&lt;/b&gt;&lt;br /&gt;  &lt;b&gt;begin&lt;/b&gt;&lt;br /&gt;    Result := M &lt;strong&gt;as&lt;/strong&gt; AstMember;&lt;br /&gt;    &lt;b&gt;if&lt;/b&gt; Result &lt;&gt; &lt;b&gt;nil then&lt;/b&gt; Yield;&lt;br /&gt;  &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/pre&gt;&lt;div class="p"&gt;Of course, there are more reasons than those I have presented here. For instance, I'm fond of the Freya solution for class declarations/implementations and the whole attitude I call &lt;em&gt;"just an implementation detail"&lt;/em&gt;. But there's a time and a place...&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111476896156292143?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111476896156292143/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111476896156292143' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111476896156292143'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111476896156292143'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/04/raison-dtre.html' title='Raison d&apos;être'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111471223727068165</id><published>2005-04-28T19:39:00.000+02:00</published><updated>2008-12-11T19:25:03.619+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='interface'/><title type='text'>Implementing interface mappings</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Freya has three techniques&lt;/span&gt; for implementing interface types:&lt;/div&gt;&lt;ol style="line-height: normal;"&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Implicit implementation: a public method in the class with the proper signature.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Explicit implementation: an implicitly private method, with a compound name including the interface type name and the interface method name, just as in C#.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:90%;"&gt;Implementation by delegation: this is a feature borrowed from Delphi. It’s a simple but effective technique for reusing implementations, and it makes easy to change the behavior of an instance by plugging a different implementation, at runtime.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="p"&gt;I'll show you an example of implementation by delegation:&lt;/div&gt;&lt;pre class="p"&gt;&lt;b&gt;using&lt;/b&gt; System, System.Collections;&lt;br /&gt;&lt;b&gt;public&lt;/b&gt;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// All Foo features are private&lt;/span&gt;&lt;br /&gt;    Foo = &lt;b&gt;class&lt;/b&gt;(IEnumerable)&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;implementation for&lt;/b&gt; Foo &lt;b&gt;is&lt;/b&gt;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// A private instance field.&lt;/span&gt;&lt;br /&gt;    &lt;b&gt;var&lt;/b&gt; Items: Array[String] = ['Freya', 'rules'];&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#006600;"&gt;// Just an implementation detail!&lt;/span&gt;&lt;br /&gt;    &lt;b&gt;interface&lt;/b&gt; IEnumerable = Items;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;.&lt;/pre&gt;&lt;div class="p"&gt;&lt;img style="float:right; margin:1px 0 1px 5px; border-style:none;" src="http://2.bp.blogspot.com/_QWaXq7W8dVI/RpFZqo1UL5I/AAAAAAAAADA/62neuZt97dA/s320/iworld.jpg" border="0" /&gt;In this example, all calls to methods from &lt;i&gt;IEnumerable&lt;/i&gt; methods are dispatched to the implementation of &lt;i&gt;IEnumerable&lt;/i&gt; already available in &lt;i&gt;Items&lt;/i&gt;. This is possible because there’s an implicit conversion from any array type to &lt;i&gt;IEnumerable&lt;/i&gt;.&lt;/div&gt;&lt;div class="p"&gt;Native Delphi implemented this feature with a clever trick, by modifying the typecast operation. Our current implementation is probably not as efficient as native Delphi’s one, but it’s easy to code. When an interface delegation is found by the compiler, it synthesizes the required methods in the implementing class. Since these are very short methods, chances are high that the CIL loader will translate them as inline method calls.&lt;/div&gt;&lt;div class="p"&gt;We are trying another complementary solution: whenever possible, those method thunks call the target implementation using “tail calls”. Tail calls are supported by the CIL through an instruction prefix that can precede the “call” and “callvirt” operation codes. This prefix instructs the CLR to discard the current stack frame before jumping to the target method. There are no guarantees that this hint will be honored by the CIL loader, however. We still have to run the corresponding benchmarks before deciding the best translation for this useful feature.&lt;/div&gt;&lt;div class="p" style="background-color: #d0d0ff; border-top: 1px dotted #a0a0bf; border-bottom: 1px dotted #a0a0bf; line-height: normal; padding: 4px 8px 4px 8px;"&gt;&lt;b&gt;Update&lt;/b&gt;: I didn't realize then, but tail calls have no effect when performed on virtual methods, so, after some tests, this "optimization" was discarded.&lt;/div&gt;&lt;div class="p"&gt;By the way, tail calls are only generated when the method signature does not include any reference or out parameters. That’s because a reference could point to a variable in the current stack frame, so the CLR has no way to verify whether a given call is safe or not. When all parameters are passed by value, tail calls are considered a safe operation.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111471223727068165?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111471223727068165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111471223727068165' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111471223727068165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111471223727068165'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/04/implementing-interface-mappings.html' title='Implementing interface mappings'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_QWaXq7W8dVI/RpFZqo1UL5I/AAAAAAAAADA/62neuZt97dA/s72-c/iworld.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-12502111.post-111469802483609532</id><published>2005-04-28T16:07:00.000+02:00</published><updated>2008-12-11T19:25:03.890+01:00</updated><title type='text'>Freya says Hi!</title><content type='html'>&lt;div class="p"&gt;&lt;span style="font-size: 108%; font-variant: small-caps;"&gt;Hello World!&lt;/span&gt;&lt;/div&gt;&lt;div class="p"&gt;This blog is dedicated to the Freya Programming Language, a new object oriented language specially designed for running on the .NET Framework, version 2.0 and higher, when available.&lt;/div&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;border-style:none;" src="http://4.bp.blogspot.com/_QWaXq7W8dVI/RpEk-I1UL4I/AAAAAAAAAC4/LFNBNyRAgQU/s320/freya.jpg" border="0" /&gt;&lt;div class="p"&gt;In the next few days, I'll post more information about Freya features and, mainly, about its ongoing implementation. I will appreciate any comments, suggestions and proposals.&lt;/div&gt;&lt;div class="p"&gt;Freya is a join effort by Moebius Ootics (&lt;a href="http://www.moebiusootics.com"&gt;www.moebiusootics.com&lt;/a&gt;) and Intuitive Sight, a.k.a IntSight (&lt;a href="http://www.intsight.com"&gt;www.intsight.com&lt;/a&gt;), my own company.&lt;/div&gt;&lt;div class="p" style="text-align: right;"&gt;Best regards, and keep in touch!&lt;/div&gt;&lt;div class="p" style="text-align: right;"&gt;Ian Marteens&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/12502111-111469802483609532?l=freyalang.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://freyalang.blogspot.com/feeds/111469802483609532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=12502111&amp;postID=111469802483609532' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111469802483609532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/12502111/posts/default/111469802483609532'/><link rel='alternate' type='text/html' href='http://freyalang.blogspot.com/2005/04/freya-says-hi.html' title='Freya says Hi!'/><author><name>Ian Marteens</name><uri>http://www.blogger.com/profile/01802552330071620377</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://www.marteens.com/ianm.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_QWaXq7W8dVI/RpEk-I1UL4I/AAAAAAAAAC4/LFNBNyRAgQU/s72-c/freya.jpg' height='72' width='72'/><thr:total>4</thr:total></entry></feed>
