<?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-2458417257543687994</id><updated>2012-01-21T13:48:03.697-07:00</updated><title type='text'>Loyc, etc.</title><subtitle type='html'>The Language Of Your Choice Blog, featuring topics of my choice</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>28</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-3746746327384148093</id><published>2011-11-14T17:08:00.006-07:00</published><updated>2011-11-14T17:50:53.327-07:00</updated><title type='text'>A taste of hardware programming</title><content type='html'>Have you ever tried hardware programming with Verilog or VHDL? During University I had to program FPGAs with Verilog, and it struck me that hardware programming was a lot like software programming, but at the same time much different also. Instead of consuming plentiful RAM, hardware programming consumes a much more scarce resource (transistors) instead. Physics can cause your program to malfunction, but aside from that, I really enjoyed my one little hardware programming course.&lt;br /&gt;&lt;br /&gt;The fundamental difference between hardware and software is that every logic gate inherently operates in parallel, unlike software which inherently operates sequentially, and requires a special mechanism--threads--in order to do tasks in parallel. Traditionally, writing multithreaded programs has been quite difficult, due to the danger of race conditions, the danger of deadlocks, forgetting to lock data structures, and so on. So it was a little surprising that such problems don't necessarily happen in hardware programming. In fact I found that the biggest assignment I was given (to make an "alarm system") was relatively fun and easy. And what made it relatively fun and easy was (1) that the solution was small enough to fit in our FPGAs without much effort, and (2) I didn't have to "update" variables.&lt;br /&gt;&lt;br /&gt;For instance, when the user activates the alarm system, she has 15 seconds to exit the building before the device is "armed". To make this work in a software program, you might write an "event" method and configure some sort of timer to call the method each second. When the event happens, you would check whether we're in a "countdown mode", and if so, decrement a variable representing the number of seconds left, then display the new number of seconds on the screen by &lt;i&gt;explicitly calling a method&lt;/i&gt; to update the screen.&lt;br /&gt;&lt;br /&gt;In a hardware program, it works a bit differently. It's been a few years since I did this in hardware, but I'll give you the gist of it. You don't need a timer "event"; instead, there is a hardware clock running at a known speed, which I'll say is 10 MHz, i.e. 10 million clock cycles per second or 0.1 microsecond per tick. So I created a counter that takes the hardware clock as input and restarts every 1/60 of a second (because part of the user interface updates 60 times per second), and when it restarts, I programmed a particular wire to be "on" (1 bit) during that clock cycle only (IIRC). I used that signal as input to a second counter that restarts every second and similarly sets a particular wire to "on" during one clock cycle per second (out of 10 million). I defined a third counter to represent the countdown, which decreases each second if the countdown is active. The counter is kept in BCD format (binary-coded decimal, 4 bits per decimal digit) so that no math is required to convert the digits into a format suitable for display to the user.&lt;br /&gt;&lt;br /&gt;The "screen" was a pair of 7-segment displays, i.e. a display that can show two characters (the numbers 0123456789 and, if you're creative, the letters AbCdEFgHIJLnoPqrStUy.) The "screen" showed only 16 bits of information total (two digits and two dots), so I simply connected 16 of the FPGA's metal pins directly to the 7-segment display. To draw something on the screen, my hardware program merely had to set those pins to "on" or "off" as appropriate.&lt;br /&gt;&lt;br /&gt;I didn't have to execute any "commands" to make a number appear on the seven-segment (numeric) display. Instead, I wrote "functions" (not really functions, but blocks of code that represent circuits or sets of circuits) that did the following:&lt;ul&gt;&lt;li&gt;Examine the state of the device and, if a countdown is in progress, produce the countdown value as output (the output is 5 bits per digit while the input is 4 bits per digit, so I just set the extra bit to 0). If a countdown is not in progress then something else is selected as output, such as part of a message (e.g. "rEAdy" might be scrolling across the screen. Actually, displaying anything but numbers was not part of the assignment; I just added the ability to display messages for fun.)&lt;/li&gt;&lt;li&gt;Those two 5-bit numbers become the input to an output mapper, which converts each 5-bit number to a 7-bit character.&lt;/li&gt;&lt;li&gt;The 7-bit character is passed directly to the output pins, unless it is modified to display an edit cursor (another visual flourish that was not part of the assignment, and not relevant to this blog post either, for that matter).&lt;/li&gt;&lt;/ul&gt;One cool thing about hardware programming was how "natural" it was to maintain the correct program state and show the desired output on the screen. I just wired the various "functions" together (I honestly don't remember what they call the functions in Verilog) and it all "just worked". I didn't have to do anything to explicitly propagate state from one place to another; for instance I didn't have to issue a "command" to make the screen update.&lt;br /&gt;&lt;br /&gt;Instead I simply &lt;i&gt;declared&lt;/i&gt; that &lt;ul&gt;&lt;li&gt;the thing that does the countdown is wired to the thing that selects a 5-bit output;&lt;/li&gt;&lt;li&gt;that thing is wired to a pair of things that select 7-bit outputs; and&lt;/li&gt;&lt;li&gt;the two 7-bit outputs are wired to the thing that chooses the 16-bit output.&lt;/li&gt;&lt;/ul&gt;Every "function" in the program is continuously "computing" its value, so when the value of the counter changes, the new count shows up on the screen automatically, less than one clock cycle later. The screen updated automatically because it was physically wired to the output of a function, and that function updated automatically because it's really a circuit, and circuits are always updating because they exist in the real world and (as the laws of physics require) run continuously as long as electricity is flowing. It actually takes extra effort to create a circuit that does &lt;i&gt;not&lt;/i&gt; respond immediately.&lt;br /&gt;&lt;br /&gt;The fact that stuff happens automatically, without you having to remember to issue "commands", makes programming easier, and thus hardware programming is at times easier than software programming... until you run into the physical limitations of your device. My "alarm system" had a two-character display, so I simply made a duplicate copy of the "function" that converts the 5-bit representation to 7 bits. That way, the two copies could run in parallel. This approach was viable because the display was only two digits; if the display had 10 digits or more, 10 copies of the function might not fit on the chip. If I wanted to save space, I would create only one instance of the function, and then add a special circuit to "call" the function on each digit in sequence (e.g. a different digit every clock cycle). In that case I would need to create registers to save the result of running the function on each digit. Moreover, the chip doesn't have enough metal pins to represent 10 digits, so the screen would define some communication protocol that the chip would have to follow. Potentially, it could get messy. But, the tasks that students were given were fairly easy and fun.&lt;br /&gt;&lt;br /&gt;Ever since I tasted hardware programming (and probably before then, actually), I have wished that software could offer the same kind of easy, automatic updates, so that variables inside the program automatically propagate to the screen, or to whereever they are supposed to go. Data binding provides part of the answer, but the problem is that you don't usually want to display your program's variables directly on the screen; you want to filter, sort, and format those variables first. If the user modifies what's on the screen, reverse transformations are needed to figure out how the internal variables should be changed. Recently, I discovered that there is a .NET library that (if used correctly) will provide these wonderful automatic updates. That library is called &lt;a href="http://updatecontrols.net/"&gt;Update Controls&lt;/a&gt; (&lt;a href="http://updatecontrols.codeplex.com/"&gt;at CodePlex&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;Functional languages like &lt;a href="http://haskell.org"&gt;Haskell&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/F_Sharp_(programming_language)"&gt;F#&lt;/a&gt; also make programming easier, generally speaking, and like hardware programming, functional languages are sufficiently different from popular "imperative" languages that it feels like something altogether different. However, functional languages don't tend to represent graphical user interfaces (GUIs) in a natural way. For GUIs, all hail Update Controls.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-3746746327384148093?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/3746746327384148093/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=3746746327384148093' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3746746327384148093'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3746746327384148093'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/11/taste-of-hardware-programming.html' title='A taste of hardware programming'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-5826997972703671269</id><published>2011-07-27T11:17:00.008-06:00</published><updated>2011-07-27T12:41:22.244-06:00</updated><title type='text'>I want a conditional dot operator</title><content type='html'>I was writing earlier about &lt;a href="http://loyc-etc.blogspot.com/2010/05/new-features-net-framework-should-have.html"&gt;features I'd like to have in the .NET Framework and C#&lt;/a&gt;, but I forgot a couple. Earlier I wrote about the "&lt;a href="http://loyc-etc.blogspot.com/2010/09/i-want-slide-operator.html"&gt;slide operator&lt;/a&gt;". Now, I'd like to propose the conditional dot or "null dot" operator.&lt;br /&gt;&lt;br /&gt;Some people will say this idea makes the language too complex, but I can't seem to get enough language features. I use just about all of C#'s existing features, including the &lt;a href="http://stackoverflow.com/questions/9033/hidden-features-of-c"&gt;"hidden" ones&lt;/a&gt;. And still I can think of many unsupported features that I know would improve productivity, code clarity, or performance... at least for me.&lt;br /&gt;&lt;br /&gt;I think it's okay for C# to have tons of features, but the IDE needs to help teach people what they mean. For example, Visual Studio should show the name of an operator in a tooltip when mousing over it, and show a help page for it if the user puts the cursor on it and presses F1.&lt;br /&gt;&lt;br /&gt;C# already has a handy "??" operator which lets you choose a default value in case the "first choice" is null:&lt;br /&gt;&lt;pre&gt;Console.WriteLine("Your name is {0}.", firstName ?? "(unknown)");&lt;/pre&gt;I use this feature somewhat often. But something's missing. I would like, in addition, a "conditional dot" operator, another kind of null guard that deals with cases where an object you want to access might be null. For example, let's say your class has a reference to another class, and you'd like to inform it when something happened:&lt;br /&gt;&lt;pre lang="c#"&gt;if (referenceToAnotherClass != null)&lt;br /&gt;    referenceToAnotherClass.OnSomethingHappened(info);&lt;/pre&gt;Of course you can't call the method if the referenceToAnotherClass is null. It would be nice if we could shorten this to something like&lt;br /&gt;&lt;pre&gt;referenceToAnotherClass??.OnSomethingHappened(info);&lt;/pre&gt;&lt;br /&gt;If the method you want to call returns a value, the "??." operator would substitute null for the return value if the class reference is null. For example,&lt;br /&gt;&lt;pre lang="c#"&gt;// firstName might be null; length will be null if firstName is null.&lt;br /&gt;int? length = firstName??.Length;&lt;/pre&gt;It would be very natural to combine the "??." operator with the existing "??" operator:&lt;br /&gt;&lt;pre lang="c#"&gt;// Equivalent to firstName != null ?  firstName.Length : 0&lt;br /&gt;int length = firstName??.Length ?? 0;&lt;/pre&gt;This operator would be most powerful when it is chained together, or used to avoid creating temporary variables:&lt;br /&gt;&lt;pre lang="c#"&gt;// If "DatabaseConnection", "PersonTable", and "FirstRow" can all &lt;br /&gt;// return null, chaining "??." simplifies your code a lot.&lt;br /&gt;var firstName = DatabaseConnection??.Tables.PersonTable??.FirstRow??.Name;&lt;br /&gt;&lt;br /&gt;// Equivalent to:&lt;br /&gt;string firstName = null;&lt;br /&gt;var dbc = DatabaseConnection;&lt;br /&gt;if (dbc != null) {&lt;br /&gt;    var pt = dbc.Tables.PersonTable;&lt;br /&gt;    if (pt != null) {&lt;br /&gt;        var fr = pt.FirstRow;&lt;br /&gt;        if (fr != null)&lt;br /&gt;            firstName = fr.Name;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The operator should also help invoke events:&lt;br /&gt;&lt;pre lang="c#"&gt;public event EventHandler Click;&lt;br /&gt;protected void OnClick()&lt;br /&gt;{&lt;br /&gt;    Click??.(this, EventArgs.Empty);&lt;br /&gt;    // equivalent to:&lt;br /&gt;    if (Click != null)&lt;br /&gt;        Click(this, EventArgs.Empty);&lt;br /&gt;&lt;br /&gt;    // Note: the dot in "??." is still required because "X??(Y)" &lt;br /&gt;    // would be indistinguishable from the null coalescing operator.&lt;br /&gt;}&lt;/pre&gt;Somebody implemented a "&lt;a href="http://www.forkcan.com/viewcode/277/Null-Dot-Operator-Extension-Method"&gt;null dot&lt;/a&gt;" extension method that provides this kind of "operator" in C#, except that it only supports one out of the four cases I just described, as it requires that the function you want to call return a reference type; it doesn't support void or struct return values. It's also slightly clumsy, and since it relies on a lambda, it hurts performance. To work well, this feature needs language support.&lt;br /&gt;&lt;br /&gt;Right now I am working with code that often converts objects to strings (the objects are usually strings, but may be something else). The object is sometimes null, so I write&lt;br /&gt;&lt;pre lang="c#"&gt;string s = (obj ?? "").ToString();&lt;/pre&gt;This works fine, but it's less efficient than it could be, because if obj == null, a virtual call to ToString() will be called on the empty string "". If the "null dot" or "conditional dot" operator existed, I would write this code as&lt;br /&gt;&lt;pre lang="c#"&gt;string s = obj??.ToString() ?? "";&lt;/pre&gt;or even&lt;br /&gt;&lt;pre lang="c#"&gt;string s = obj??.ToString();&lt;/pre&gt;if a null result is acceptable.&lt;br /&gt;&lt;br /&gt;I know that an operator like this exists in some other languages, but I don't which ones at the moment. Anybody remember?&lt;br /&gt;&lt;br /&gt;P.S. Microsoft somehow forgot to include a compound assignment operator, which should work like the other compound assignment operators. &lt;br /&gt;&lt;pre lang="c#"&gt;&lt;br /&gt;twosies += 2; // equivalent to twosies = twosies + 2&lt;br /&gt;doubled *= 2; // equivalent to doubled = doubled * 2&lt;br /&gt;// ensure myList is not null&lt;br /&gt;myList ??= new List&amp;lt;int&amp;gt;(); // myList = myList ?? new List&amp;lt;int&amp;gt;()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=3453924" rel="tag" style="display:none"&gt;CodeProject&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-5826997972703671269?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/5826997972703671269/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=5826997972703671269' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5826997972703671269'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5826997972703671269'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/07/i-want-conditional-dot-operator.html' title='I want a conditional dot operator'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-2123544963427875395</id><published>2011-07-05T11:37:00.003-06:00</published><updated>2011-07-05T11:40:56.002-06:00</updated><title type='text'>C++ vs C# Speed Benchmark, v.2</title><content type='html'>I just finished the &lt;a href="http://www.codeproject.com/KB/cross-platform/BenchmarkCppVsDotNet.aspx"&gt;second edition of my detailed benchmark&lt;/a&gt; comparing C++ and .NET performance. Hope you like it.&lt;br /&gt;&amp;nbsp;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-2123544963427875395?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/2123544963427875395/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=2123544963427875395' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2123544963427875395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2123544963427875395'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/07/c-vs-c-benchmark.html' title='C++ vs C# Speed Benchmark, v.2'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-900768816500616484</id><published>2011-07-05T11:29:00.011-06:00</published><updated>2011-11-10T11:39:09.729-07:00</updated><title type='text'>Why WPF sucks: an overview</title><content type='html'>I was just reading &lt;a href="http://www.simple-talk.com/community/blogs/richard/archive/2011/06/16/101966.aspx"&gt;this article&lt;/a&gt; detailing someone's frustrations with WPF, and thought I should add to the chorus.&lt;br /&gt;&lt;br /&gt;I agree with Mr. Mitchell, I'm fairly sure I will never like WPF. It has a steep learning curve because it's undiscoverable. WinForms was relatively easy to understand; its most complex feature was &lt;a href="http://www.codeproject.com/KB/database/databinding_tutorial.aspx"&gt;data binding&lt;/a&gt;. But WPF relies heavily on XAML, a language in which MS somehow expects you to "just know" what to type to get what you want, and when you somehow figure out what to type, half the time it doesn't work and you get a mysterious unhelpful exception (or no exception, but it still doesn't work.)&lt;br /&gt;&lt;br /&gt;In WinForms, there was an easy-to-use designer and anything you did was translated to easy-to-understand C# (or VB) code; in fact that was the native representation! XAML, however, can't be translated to anything except BAML, so nobody can easily understand what a piece of XAML does, nor can we step through it in the debugger. To make matters worse, XAML relies heavily on dynamic typing (and even the C# part constantly makes you cast "object"s). This prevents IntelliSense from working very well, thwarts the refactoring tools (ever tried renaming a property that was bound in XAML?), and shifts tons of errors from compile-time to run-time.&lt;br /&gt;&lt;br /&gt;In short, WPF programs look pretty, and offer better options for data visualization than WinForms (DataTemplates in ListBoxes are a major improvement), but beyond that they are a huge step in the wrong direction. How could Microsoft think this design was a good idea? If any company but Microsoft or Apple made a GUI architecture that was this difficult to use, no one would want to use it. For my company I started to develop some ideas for a GUI architecture that would have been much leaner and more elegant than WPF, but it looks like I may never write that code. The point is, I have some sense of how a GUI framework &lt;i&gt;should&lt;/i&gt; work, and it's not like WPF.&lt;br /&gt;&lt;br /&gt;I remember my first WPF app. I had an ItemsControl with a ControlTemplate containing a ScrollViewer with a Grid inside (note the learning curve: you can't use WPF very well without some idea what those terms mean.) I wanted to know: "&lt;a href="http://stackoverflow.com/questions/870137/wpf-how-to-attach-mouse-events-to-a-viewmodel"&gt;How do I attach mouse event handlers to an ItemsControl to detect mouse clicks and drags on blank space?&lt;/a&gt;" The answer? In order to get mouse events with meaningful mouse coordinates (i.e. coordinates in scrollable space), it was necessary to obtain a reference to the grid using a strange incantation:&lt;br /&gt;&lt;br /&gt;Grid grid = (Grid)_itemsControl.Template.FindName("Panel", _itemsControl);&lt;br /&gt;&lt;br /&gt;Then I attached event handlers to the grid, and inside the mouse event handlers, get the mouse coordinates w.r.t. the grid using&lt;br /&gt;&lt;br /&gt;Point p = e.GetPosition((IInputElement)sender);&lt;br /&gt;&lt;br /&gt;Plus, in order to get mouse events on the entire surface, the control (actually the grid) must have a background.&lt;br /&gt;&lt;br /&gt;In another case I had to use this incantation:&lt;br /&gt;&lt;br /&gt; var r = VisualTreeHelper.HitTest(_panel, new Point(e.X, e.Y));&lt;br /&gt; var hit = r == null ? null : r.VisualHit as FrameworkElement;&lt;br /&gt;&lt;br /&gt;In WinForms, pretty much all the methods you need are members of the control class you are using. But this example shows that in WPF, sometimes you have to call a static method of some other class to accomplish what you want. Then consider the minimalist documentation (so that you really need to buy a book to learn WPF)... that's undiscoverability at its finest.&lt;br /&gt;&lt;br /&gt;Update: Also, WPF is shockingly inefficient, as you may learn if you need to &lt;a href="http://stackoverflow.com/questions/7097999/wpf-binding-to-listboxitem-isselected-doesnt-work-for-off-screen-items"&gt;use a non-virtualized ListBox in order to get MVVM to work&lt;/a&gt; and the list has more than about 1000 items. Trivial items (short text strings, no DataTemplate) consume about 8 KB of memory per row, or 80 MB for 10000 items, and ListBox takes 4 seconds to handle a single arrow key pressed in a list with this many items.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-900768816500616484?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/900768816500616484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=900768816500616484' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/900768816500616484'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/900768816500616484'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/07/why-wpf-sucks.html' title='Why WPF sucks: an overview'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-5940449343356550001</id><published>2011-04-09T16:07:00.015-06:00</published><updated>2011-04-29T07:42:48.427-06:00</updated><title type='text'>The story of IIterable</title><content type='html'>I like the .NET Framework. To me, C# is a much better programming language than Java or C++, and it offers good performance at the same time. However, some of the fundamental design decisions in the .NET framework bother me. Which brings me to the subject of this post: I'm remaking some of the .NET collections library, built on top of a replacement for IEnumerable&amp;lt;T&amp;gt; called IIterable&amp;lt;T&amp;gt;.&lt;br /&gt;&lt;br /&gt;You see, I'm a bit of a performance nut. So it bothers me that the &lt;a href="http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx"&gt;IEnumerator&amp;lt;T&amp;gt;&lt;/a&gt; interface requires two interface calls to retrieve each element of a collection: MoveNext() and Current. Therefore, I invented a replacement and named it Iterator&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;   public delegate T Iterator&amp;lt;out T&amp;gt;(ref bool ended);&lt;/pre&gt;&lt;br /&gt;It had a corresponding interface called IIterable&amp;lt;T&amp;gt;, which is analogous to IEnumerable&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;   public interface IIterable&amp;lt;out T&amp;gt;&lt;br /&gt;   {&lt;br /&gt;      Iterator&amp;lt;T&amp;gt; GetIterator();&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;Microbenchmarks show that this iterator has (at most) half the overhead of IEnumerator, as you would expect. More on that later.&lt;br /&gt;&lt;br /&gt;When an iterator is called, it returns the next value in the sequence, and sets ended to true if there are no more elements. It should be noted that Iterator&amp;lt;T&amp;gt; was originally defined as follows:&lt;br /&gt;&lt;pre&gt;   public delegate bool Iterator&amp;lt;out T&amp;gt;(out T current);&lt;/pre&gt;&lt;br /&gt;This version was supposed to behave like IEnumerator.MoveNext(), returning true on success and false if there were no more elements in the sequence. It also happened to return the next value in the sequence, eliminating the need for IEnumerator.Current. Code that used the original Iterator&amp;lt;T&amp;gt; might look like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;   T current;&lt;br /&gt;   for (var moveNext = list.GetIterator(); moveNext(out current);)&lt;br /&gt;   {&lt;br /&gt;      ...&lt;br /&gt;   }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This was very similar to code that uses IEnumerator directly (instead of using a foreach loop):&lt;br /&gt;&lt;pre&gt;   for (var e = list.GetEnumerator(); e.MoveNext();)&lt;br /&gt;   {&lt;br /&gt;      T current = e.Current;&lt;br /&gt;      ...&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;Unfortunately, the .NET Framework did not allow this signature because the CLR does not support true "out" arguments--"out" arguments are the same as "ref" arguments but with a special attribute on them, so at the CIL level they still permit the caller to supply an input value. That makes them incompatible with the &amp;lt;out T&amp;gt; part of the declaration: since they technically accept an input value, they must be invariant, not covariant. Thus I had to change the definition as written above:&lt;br /&gt;&lt;pre&gt;   public delegate T Iterator&amp;lt;out T&amp;gt;(ref bool ended);&lt;/pre&gt;&lt;br /&gt;That's unfortunate, because using the iterator is much clumsier this way. for-loops like the one above must be written like this instead:&lt;br /&gt;&lt;pre&gt;   bool ended = false;&lt;br /&gt;   for (var it = list.GetIterator();;)&lt;br /&gt;   {&lt;br /&gt;      T current = it(ref ended);&lt;br /&gt;      if (ended)&lt;br /&gt;          break;&lt;br /&gt;      ...&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;This clumsiness is avoided using an extension method:&lt;br /&gt;&lt;pre&gt;   public static bool MoveNext&amp;lt;T&amp;gt;(this Iterator&amp;lt;T&amp;gt; it, out T value)&lt;br /&gt;   {&lt;br /&gt;      bool ended = false;&lt;br /&gt;      value = it(ref ended);&lt;br /&gt;      return !ended;&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;Unfortunately, there is a performance penalty for calling MoveNext(), which eliminates most of the performance gain you get from using Iterator in the first place.&lt;br /&gt;&lt;br /&gt;Anyway, these "Iterators" are returned from IIterable&amp;lt;T&amp;gt;.GetIterator. There's more to it than this--I actually created a whole series of collection interfaces to fix other things I don't like about the design of .NET collection interfaces--but for this post I'm just going to focus on IIterable&amp;lt;T&amp;gt; and Iterator&amp;lt;T&amp;gt;.&lt;br /&gt;&lt;br /&gt;I spent the last two days working on an implementation of LINQ-to-Iterators, and then discovered that there is a problem. A rather serious problem.&lt;br /&gt;&lt;br /&gt;Iterator&amp;lt;T&amp;gt; and IIterable&amp;lt;T&amp;gt; and various other interfaces derived from it are covariant: Iterator&amp;lt;out T&amp;gt; and IIterable&amp;lt;out T&amp;gt;. This makes it possible to write pointless code such as this:&lt;br /&gt;&lt;pre&gt;   IIterable&amp;lt;object&amp;gt; Combine(IIterable&amp;lt;string&amp;gt; a, IIterable&amp;lt;Uri&amp;gt; b)&lt;br /&gt;   {&lt;br /&gt;      return a.Concat&amp;lt;object&amp;gt;(b);&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;That is, IIterable&amp;lt;string&amp;gt; and IIterable&amp;lt;Uri&amp;gt; is implicitly convertible to IIterable&amp;lt;object&amp;gt;. It requires C# 4.0, but in theory you can do this with any version of the .NET Framework since 2.0. With a helper method written in C# 4.0, it's even possible to write code that does the same thing in C# 2.0.&lt;br /&gt;&lt;br /&gt;However, as I began to implement LINQ-to-Iterable I realized that there is one major problem. In practice, any collection that implements IIterable&amp;lt;T&amp;gt; will also implement IEnumerable&amp;lt;T&amp;gt;, for fairly obvious reasons: Microsoft and third-party libraries expect IEnumerable&amp;lt;T&amp;gt;, and the foreach loop expects a GetEnumerator method.&lt;br /&gt;&lt;br /&gt;But if a class implements both IIterable&amp;lt;T&amp;gt; and IEnumerable&amp;lt;T&amp;gt;, that class would suddenly have weird problems with LINQ. Why? Well, IEnumerable&amp;lt;T&amp;gt; supports LINQ, and once I finish LINQ-to-Iterable, IIterable&amp;lt;T&amp;gt; will support LINQ too, and that's precisely the problem. Let's consider what happens if I try to run a LINQ query on my Deque&amp;lt;T&amp;gt; collection that implements IIterable&amp;lt;T&amp;gt; and IEnumerable&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;   Deque&amp;lt;int&amp;gt; list = ...;&lt;br /&gt;   var odds = Iterable.CountForever(3, 2);&lt;br /&gt;   var primes = from p in list&lt;br /&gt;                where (p &amp;gt; 2 &amp;&amp; (p &amp; 1) == 1) || p == 2&lt;br /&gt;                where !odds.TakeWhile(n =&amp;gt; n * n &amp;lt;= p).Any(n =&amp;gt; p % n == 0)&lt;br /&gt;                select p;&lt;/pre&gt;&lt;br /&gt;The compiler complains because it doesn't know which LINQ implementation to use: "Multiple implementations of the query pattern were found for source type 'Loyc.Runtime.Deque&amp;lt;int&amp;gt;'. Ambiguous call to 'Where'." The problem disappears if I remove "using System.Linq" from the top of my source file, or if I do the query against "(IIterable&amp;lt;int&amp;gt;)list" instead of just "list". However, it's an inconvenience at best. At worst, if the user doesn't understand why the error occurred and how to solve it, it's a showstopper.&lt;br /&gt;&lt;br /&gt;I solved this problem by changing the definition of IIterable as follows:&lt;br /&gt;&lt;pre&gt;   public interface IIterable&amp;lt;out T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;br /&gt;This forces all implementations of IIterable to also implement IEnumerable, but it solves the problem because the compiler no longer considers the choice ambiguous: if a class implements IIterable&amp;lt;T&amp;gt;, the compiler will choose LINQ-to-iterable without complaint. The reason is that IIterable is now considered more specific, and the compiler prefers more specific method signatures.&lt;br /&gt;&lt;h4&gt;The variance dilemma&lt;/h4&gt;Unfortunately, this causes a new problem: it refuses to compile for any .NET Framework version before 4.0! This is because Microsoft made a very strange decision: even though CLR version 2.0 supports generic variance, IEnumerable&amp;lt;T&amp;gt; is defined as invariant before version 4.0.&lt;br /&gt;&lt;br /&gt;Interesting, is it not, how the interaction of seemingly unrelated issues--the way extension method resolution works in C# 3.0 (2006) and the decision Microsoft made in 2005 to mark IEnumerable&amp;lt;T&amp;gt; as invariant--combine to screw up my LINQ implementation?&lt;br /&gt;&lt;br /&gt;I could only think of two ways to work around this problem.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;(1) Restrict covariance to .NET 4.0, i.e. in .NET 2.0, make IIterable&amp;lt;T&amp;gt; invariant.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;If I choose this option, not only do we lose covariance in .NET 2.0 and .NET 3.5 (which is sad because it's a cool feature), but it also forces me to release at least two DLLs, one for .NET 2.0 (which relies on LinqBridge for some features), and one for .NET 4.0. Probably a .NET 3.0 version is needed too, for those that don't want a dependency on LinqBridge.&lt;br /&gt;&lt;br /&gt;If this problem didn't exist then you could have at least used the same DLL for .NET 3 and 4. Technically you could reference the .NET 3 DLL in .NET 4 and lose variance, but this would have the side effect of breaking interoperability with any program that uses the .NET 4 DLL instead.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;(2) Don't derive IIterable&amp;lt;T&amp;gt; from IEnumerable&amp;lt;T&amp;gt;.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This option allows IIterable&amp;lt;T&amp;gt; to remain covariant, but causes conflicts with LINQ-to-Enumerable as I already described. I fear that a lot of developers, when confronted with the "Ambiguous call to 'Where'" error, will immediately say "screw this" and refuse to use my collection classes.&lt;br /&gt;&lt;br /&gt;Therefore, my decision is to keep the following definition of IIterable&amp;lt;T&amp;gt;:&lt;br /&gt;&lt;pre&gt;public interface IIterable&amp;lt;out T&amp;gt; : IEnumerable&amp;lt;T&amp;gt; { ... }&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;Blah, blah, blah&lt;/h4&gt;I have a couple of comments about the design of Iterator&amp;lt;T&amp;gt;. Firstly, unlike IEnumerator, Iterator&amp;lt;T&amp;gt; does not implement IDisposable (indeed, it can't, since it's a delegate). My main rationale for this is that if your collection's iterator (as opposed to the collection itself) needs to implement IDisposable, you may as well continue using IEnumerator&amp;lt;T&amp;gt;. Certainly if Microsoft had chosen to use my "fast iterator" implementation when it first gave birth to .NET, it should have included IDisposable:&lt;br /&gt;&lt;pre&gt;   interface IEnumerator&amp;lt;out T&amp;gt; : IDisposable&lt;br /&gt;   {&lt;br /&gt;      bool MoveNext(out T current);&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;Of course, back in the original .NET Framework of 2002, they weren't especially concerned with performance yet. And they hadn't invented generics yet. Plus, to make this interface variance-ready, they would have had to formally define "out" parameters as equivalent to return values. And while they were at it they could have maybe implemented &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/details/90909/need-covariant-return-types-in-c-all-net-langage"&gt;return value inheritance covariance&lt;/a&gt;. But I digress...&lt;br /&gt;&lt;br /&gt;Also, the C# language makes Iterator&amp;lt;T&amp;gt; much easier to implement as a delegate. For example, the implementation of Iterator.CountForever is very simple:&lt;br /&gt;&lt;pre&gt;   public static Iterator&amp;lt;int&amp;gt; CountForever(int start, int step)&lt;br /&gt;   {&lt;br /&gt;      start -= step;&lt;br /&gt;      return (ref bool ended) =&amp;gt; start += step;&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;Although it would be possible to write a helper class that converts a lambda function to the hypothetical "IIterator&amp;lt;T&amp;gt;" interface, this would incur an extra interface invocation above the cost of invoking the lambda, not to mention an extra memory allocation. Therefore, IIterator&amp;lt;T&amp;gt; would generally not be any faster than IEnumerator&amp;lt;T&amp;gt; (without special compiler support to eliminate the overhead).&lt;br /&gt;&lt;br /&gt;Secondly, you may have noticed that "ref bool ended" is "ref" instead of "out". My reasoning, again, is that if you're using Iterator instead of IEnumerator it's because you want to squeeze out every last drop of performance. Therefore, to save time, Iterators are not required to clear "ended" on every iteration; they only need to set it once, at the end.&lt;br /&gt;&lt;br /&gt;One more thing. After I release this library, if you want to implement IIterable&amp;lt;T&amp;gt;, you can make your job easier by using IterableBase&amp;lt;T&amp;gt; or ListSourceBase&amp;lt;T&amp;gt; as your base class, which provides implementations of GetEnumerator(). If your collection class already has GetEnumerator, and you want to add IIterable&amp;lt;T&amp;gt;, you &lt;i&gt;could&lt;/i&gt; add this method:&lt;br /&gt;&lt;pre&gt;   public Iterator&amp;lt;T&amp;gt; GetIterator()&lt;br /&gt;   {&lt;br /&gt;      return GetEnumerator().AsIterator();&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;But that's not necessarily a good idea, because the iterator itself will be slightly slower than the IEnumerator it was derived from. Only if a multi-step LINQ-to-Iterable query is built on top of this would there be any performance benefit. (I'll get around to some benchmarks later).&lt;br /&gt;&lt;h4&gt;The performance of Iterator&lt;/h4&gt;I wrote some simple loops that call IEnumerator&amp;lt;T&gt; (MoveNext and Current) on the one hand, and Iterator&amp;lt;T&gt; on the other. The collections being enumerated just count numbers:&lt;br /&gt;&lt;pre&gt;   public static IEnumerator&amp;lt;int&gt; Counter1(int limit)&lt;br /&gt;   {&lt;br /&gt;      for (int i = 0; i &amp;lt; limit; i++)&lt;br /&gt;         yield return i;&lt;br /&gt;   }&lt;br /&gt;   public static Iterator&amp;lt;int&gt; Counter2(int limit)&lt;br /&gt;   {&lt;br /&gt;      int i = -1;&lt;br /&gt;      return (ref bool ended) =&gt;&lt;br /&gt;      {&lt;br /&gt;         if (++i &amp;lt; limit)&lt;br /&gt;            return i;&lt;br /&gt;         ended = true;&lt;br /&gt;         return 0;&lt;br /&gt;      };&lt;br /&gt;   }&lt;/pre&gt;&lt;br /&gt;On my machine, in a Release build, it basically takes 7.1 seconds to count to a billion if I use IEnumerator&amp;lt;int&gt;, 3.552 if I invoke Iterator&amp;lt;int&gt; directly, and 6.1 seconds if I call the Iterator&amp;lt;int&gt;.MoveNext extension method. The raw Iterator is thus about twice as fast as IEnumerator, although my test is imperfect since it ignores loop overhead and the content of Counter1 and Counter2. (It should also be noted that results vary from run to run, and seemingly irrelevant changes to the benchmark code also change the results. Sometimes my benchmark reports that IEnumerator requires 250% as much time, and sometimes only 170%, depending on the weather and the test PC.)&lt;br /&gt;&lt;h4&gt;Conclusion&lt;/h4&gt;With a performance difference of only 3.5 seconds in a billion iterations, it's fair to ask whether IIterable is really worth it. Well, in most circumstances it probably isn't. But LINQ queries tend to be composed of many simple components, which means that the time spent invoking MoveNext, Current, and your lambdas may be a large fraction of the total runtime. In the future I'll try to quantify the difference.&lt;br /&gt;&lt;br /&gt;If you do find that your LINQ-to-objects queries are slow, it often means you're doing it wrong: maybe you're using an O(N^2) algorithm when you should have picked a O(N log N) algorithm. But I believe in "&lt;a href="http://blogs.msdn.com/b/brada/archive/2003/10/02/50420.aspx"&gt;the pit of success&lt;/a&gt;": developers shouldn't have to work much harder to write fast code. Microoptimizations, like writing out a LINQ query longhand with "foreach" or "for" loops, and calling through a List&amp;lt;T&gt; reference instead of IList&amp;lt;T&gt;, should virtually never be necessary. Instead, the low-level code they use as their foundation should be as fast as possible.&lt;br /&gt;&lt;br /&gt;It's a hobby of mine to explore how the most basic components of the .NET Framework can be adjusted for better performance, in order to slide high-level code closer to that "performance pit of success". I don't really expect you to go out of your way to use IIterable&amp;lt;T&gt;, because most of the time the performance difference is quite small. However, those who want to finally write a video game, compiler, simulation or other computationally intensive program in a truly high-level language like C# with LINQ--instead of juggling pointers in C++ and writing "for (int i = 0; i &amp;lt; (int)collection.size(); i++)" on the chalkboard another thousand times--will appreciate libraries like the one I'm developing.&lt;br /&gt;&lt;br /&gt;Microsoft, if you're reading this: yes, I would consider a job offer. Put me with the cool kids that are designing C# 5.0. I'll tack on support for IIterable via yield return, and needless to say I have many other ideas for improvement. Also, if you pay me to write Loyc and rename it C# 6.0, I promise not to complain.&lt;br /&gt;&lt;br /&gt;Thanks for reading!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-5940449343356550001?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/5940449343356550001/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=5940449343356550001' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5940449343356550001'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5940449343356550001'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/04/iiterable-dilemma.html' title='The story of IIterable'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-3377634974219935624</id><published>2011-04-04T10:45:00.040-06:00</published><updated>2011-04-09T14:14:37.327-06:00</updated><title type='text'>Update Controls</title><content type='html'>I've felt for a long time that something was wrong with the way we do GUI development. For instance, suppose we have a form with a list of people and some information about them:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://4.bp.blogspot.com/-Dl_fqw2oUSE/TaC7i0WeR8I/AAAAAAAAACg/oxwJxvjQrNI/s1600/PointlessForm.png" border="0" alt="A pointless person editor"/&gt;&lt;br /&gt;&lt;small&gt;&lt;i&gt;(This used to be an HTML form but CodeProject wouldn't render it.)&lt;/i&gt;&lt;/small&gt;&lt;br /&gt;&lt;br /&gt;Suppose that the "serial number" box should be disabled if the user does not have a bike. Traditionally, we would write an event handler for when the value of the checkbox changes, and update the Enabled property when it does. Some code bases would also have code when the current person changes, and we must also handle the situation that the list of persons is empty.&lt;br /&gt;&lt;br /&gt;GUI-specific data binding can solve this particular problem most of the time; typically you can bind the Enabled property on the serial number box to the value of the checkbox. However, it might not handle the case that no person is selected. And there are many other scenarios where data binding is insufficient:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Suppose we would like the ListBox of persons to show a summary of each person (based on multiple properties of the person)--well, traditional data binding can't aggregate different properties of an object.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Suppose we have an options dialog box that controls the appearance of the main window, we would like any changes to the options to immediately alter the main window's appearance--well, traditional data binding doesn't work across different windows.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Suppose we are showing a shopping cart and we'd like to keep a label up-to-date that shows the total number of items and the total price--well, data binding can't aggregate properties of multiple objects.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Suppose we'd like to display a certain panel only if the application is running in "administrator" mode, AND an item is selected, AND a check box is checked--well, traditional data binding can't derive a value from multiple unrelated sources of information.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Suppose we'd like to filter a list based on a user's search string--in general, data binding doesn't help with filtering.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;In these cases, if data binding helps at all, it requires you to jump through hoops, or add event handlers for edge cases, or at least keep dependencies in mind when you invoke your INotifyPropertyChanged events. For example, suppose you're using WPF and your ListBox is bound to a Person.ListLabel property that returns a summary string with the name and rank of a Person. If nothing else, you'll have to make sure you raise the PropertyChanged event for "ListLabel" when the name or rank is changed. That is, the code for the Name and Rank properties must explicitly state that ListLabel depends on them. (Now, you can avoid creating the ListLabel property by using a DataTemplate, but if you've programmed WPF then you have probably encountered the need to fire multiple PropertyChanged events for a single property change.)&lt;br /&gt;&lt;br /&gt;Ideally, the actual code of a program should be closely related to the behavior rules it is supposed to follow. More generally, the solution space should resemble the problem space; your code should express your business rules. Usually, behavior rules can be described in a simple way:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;"The serial number field is enabled only if 'Has a bike' is checked and a person is selected".&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"The list box will show the name and rank of each person."&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"The font size of the main dialog is to match the font size selected on the options dialog."&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"In the shopping cart, the total price is shown under the list of items."&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"The list only shows items that contain the text typed in the 'Filter' box."&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Notice how all of these requirements are declarative. They describe the desired results, without considering how to keep the user interface up-to-date when changes occur. What must the program do when the user selects a different person or a different invoice? What must it do when the server tells us there a new person, or that a new item was created remotely in the shopping cart? The requirements don't say anything about that. Wouldn't it be nice if we didn't have to write code to handle these circumstances either?&lt;br /&gt;&lt;h4&gt;Introducing Update Controls&lt;/h4&gt;In a University undergrad project I experimented with starting to solve this problem, using a concept I called "propagating variables". I didn't entirely figure out how to make it work, though. So I was delighted to find that someone had solved this problem in .NET land, with a framework called &lt;a href="http://updatecontrols.net"&gt;Update Controls&lt;/a&gt;. Update Controls is a different way of programming, so it takes some practice, but I am delighted with how it streamlines my code.&lt;br /&gt;&lt;br /&gt;UpdateControls not only keeps your GUI up-to-date, but it also separates the GUI from the rest of the code better than any framework I have seen before. UpdateControls supports not only WPF but WinForms and Silverlight, too, and it encourages you to write your code in such a way that the view is replacable. I'm using UpdateControls for WinForms right now, because I find it easier, but if I ever want to upgrade to WPF, I will be able to do so with minimal changes to my Model and ViewModel code. Moreover, I could easily design a program that supports both a WinForms user interface and a Silverlight one at the same time: two views, one ViewModel. If somebody decides to create "UpdateControls for &lt;a href="http://monotouch.net/"&gt;MonoTouch&lt;/a&gt;" someday, I could add an iPhone version, sharing the same viewmodel between Silverlight, WinForms and MonoTouch.&lt;br /&gt;&lt;br /&gt;By strictly separating the ViewModel from the View, and placing all nontrivial logic in the ViewModel, you can effectively write unit tests for your GUI by writing tests for the ViewModel. Just make sure that the code in the View is so simple that no tests are needed, and that the output you want to test for is computed by the ViewModel instead of the View. You still have to manually test the view to make sure it &lt;i&gt;looks&lt;/i&gt; right, but its &lt;i&gt;logic and behavior&lt;/i&gt; can be unit-tested.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The Presentation and Navigation Models&lt;/h4&gt;Moreover, I am finding that the "Update Controls" way of doing things leads to clean, maintainable code. I especially like the &lt;a href="http://updatecontrols.net/doc/tips/navigationmodel"&gt;Presentation Model&lt;/a&gt; concept. I think of the presentation model as a kind of "file system" of GUI state. That is, the presentation model is the root of a tree structure from which you can reach all of your application's important state information: the list of open documents, the currently selected item in each listbox, the options in your Options dialog, and so forth. The view (Forms and Controls) merely reflects the state of the presentation model.&lt;br /&gt;&lt;br /&gt;Conceptually the presentation model (PM) is broken down into two parts: the model, and the navigation model. I am not sure where the view-models fit into this pattern; heck, maybe this "file system" idea isn't exactly what Micheal Perry (author of Update Controls) was thinking of when he described the pattern.&lt;br /&gt;&lt;br /&gt;But I don't think it's necessary to follow the pattern exactly as described in his blog. The key idea is that you can put all important state information in a big tree (analogous to a file system, or to the DOM in web programming), filled with the two basic Update Controls primitives, Independents and Dependents (known collectively as Precedents). Placing state in the UC-enabled presentation model makes your code very maintainable, because&lt;br /&gt;&lt;ul&gt;&lt;li&gt;You no longer need to answer questions like "what events do I have to fire when I change this?" or "what properties change when this property changes?"&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If your state is in the presentation model, you no longer have to worry about what physical control(s) display a piece of state information--because controls don't depend on each other--and you can even move controls between different dialog boxes without breaking anything (other than the controls being moved).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;A tree structure is a good basis for a mental model of the application. In applications that hold state in many different ways, in separate "islands", it may be hard to keep track of everything. But the PM concept gives you a way to mentally visualize your program state and to communicate about state variables (because everything important has a named location in the tree.)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Have you ever fiddled with the console variables (CVars) in a video game such as Quake or Crysis? It's cool how you can change a variable and the game's output instantly matches the changes you make. Update Controls and the presentation model bring this kind of magic to your own code. You just define the state variables, stick them in a tree, and write simple snippets of code that describe how a GUI control depends on the tree.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The Presentation Model tree&lt;/h4&gt;The tree I'm describing isn't anything fancy. It's just an ordinary collection of classes, organized and nested in whatever way is most natural for the application. For instance, I'm writing an application that displays a collection of objects on a form. Well, actually there might be more than one collection of objects, with each collection shown on a different tab on the user interface; but I decided that my presentation model would only represent one data set (I create extra instances for multiple data sets). This data set has two fundamentally different kinds of objects, but for this discussion it doesn't matter what they are, so let's call them &lt;b&gt;Apples&lt;/b&gt; and &lt;b&gt;Bananas&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;The user can have either a "live view" which shows the current state of the objects, or a historical view, which shows the state of the objects at some point in the past. Plus, there is an options dialog, and the Presentation Model has a reference to the options. Note that there are no GUI objects in the presentation model; it provides access to the options, not to the dialog! It has four main properties (HModel, ApplesVM, BananasVM, Options) which, in turn, contain other properties, forming a tree:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;b&gt;PresentationModel&lt;/b&gt; (class)&lt;br /&gt;.&lt;b&gt;HModel&lt;/b&gt;      (HistoryBrowserModel class: represents the model at one instant in time)&lt;br /&gt;   .&lt;b&gt;Model&lt;/b&gt;       (Model class: holds the core data model)&lt;br /&gt;      .&lt;b&gt;Apples&lt;/b&gt;      (list of FruitObject with state and history of each apple)&lt;br /&gt;      .&lt;b&gt;Bananas&lt;/b&gt;     (list of FruitObject with state and history of each banana)&lt;br /&gt;         (properties of FruitObject are not shown because they are not relevant&lt;br /&gt;          to this blog entry. In reality, my app is not fruit-related.)&lt;br /&gt;   .&lt;b&gt;StartTime&lt;/b&gt;   (DateTime: minimum value of the time slider in the GUI)&lt;br /&gt;   .&lt;b&gt;EndTime&lt;/b&gt;     (DateTime: maximum value of the time slider in the GUI;&lt;br /&gt;                 derived automatically from time stamps on the FruitObjects)&lt;br /&gt;   .&lt;b&gt;ViewTime&lt;/b&gt;    (DateTime: current time being viewed in the GUI)&lt;br /&gt;   .&lt;b&gt;LiveMode&lt;/b&gt;    (bool: if true, ViewTime will be locked to EndTime)&lt;br /&gt;   .&lt;b&gt;Apples&lt;/b&gt;      (a projection of Model.Apples at the current ViewTime)&lt;br /&gt;   .&lt;b&gt;Bananas&lt;/b&gt;     (a projection of Model.Bananas at the current ViewTime)&lt;br /&gt;.&lt;b&gt;ApplesVM&lt;/b&gt;    (FruitListViewModel class)&lt;br /&gt;   .&lt;b&gt;CompleteList&lt;/b&gt; (list of FruitViewModels derived from HModel.Apples)&lt;br /&gt;   .&lt;b&gt;Filter      &lt;/b&gt; (user-defined filter that reduces the set of objects to display)&lt;br /&gt;   .&lt;b&gt;FilteredList&lt;/b&gt; (list of FruitViewModels limited by the Filter)&lt;br /&gt;   .&lt;b&gt;SelectedItem&lt;/b&gt; (currently selected FruitViewModel, or if multiple apples are&lt;br /&gt;                 selected, a fake ViewModel that provides a combined view)&lt;br /&gt;.&lt;b&gt;BananasVM&lt;/b&gt;   (another instance of FruitListViewModel)&lt;br /&gt;   .&lt;b&gt;CompleteList&lt;/b&gt;&lt;br /&gt;   .&lt;b&gt;Filter&lt;/b&gt;&lt;br /&gt;   .&lt;b&gt;FilteredList&lt;/b&gt;&lt;br /&gt;   .&lt;b&gt;SelectedItem&lt;/b&gt;&lt;br /&gt;.&lt;b&gt;Options&lt;/b&gt;     (shared between all instances of PresentationModel)&lt;br /&gt;   .&lt;b&gt;ServerAddress&lt;/b&gt;&lt;br /&gt;   ... (more options)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;What I'm most proud of is the time slider. Basically, the time slider is bound to the value of _pm.HModel.ViewTime (where _pm is the presentation model). When the user moves the slider, an event written by me updates _pm.HModel.ViewTime. UpdateControls  figures out the rest:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;It knows that _pm.HModel.Apples and _pm.HModel.Bananas depend on ViewTime and automatically updates them.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;It also knows that _pm.ApplesVM.CompleteList and _pm.BananasVM.CompleteList depend on _pm.HModel.Apples and _pm.HModel.Bananas so it updates the CompleteLists too.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;In turn, the FilteredList properties depend on the CompleteList properties so they are updated.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Finally, the Update Controls in the tab (ListBoxes, etc.) know that they depend on FilteredList, so they update themselves too. Like magic!&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;All I had to do was correctly associate my data with Update Controls primitives such as Independent, Dependent, Independent&amp;lt;T&gt;, Dependent&amp;lt;T&gt;, DependentList&amp;lt;T&gt; and IndependentList&amp;lt;T&gt;; and to implement GetHashCode and Equals on wrapper objects such as ViewModels (it's important to tell UpdateControls that two wrappers are equal if the wrapped objects are equal.) UpdateControls automatically detects dependencies between properties that use these primitives, and updates all Dependents on-demand (i.e. when their values are requested).&lt;br /&gt;&lt;h4&gt;Has a bike?&lt;/h4&gt;So let's say you've designed a presentation model "PModel" for your application and it represents the form shown at the beginning of this blog entry. You're using Windows Forms. So, first, you need to give the form a reference to the presentation model:&lt;br /&gt;&lt;pre&gt;PModel _pm;&lt;br /&gt;public Form1(PModel pm)&lt;br /&gt;{&lt;br /&gt;    InitializeComponent();&lt;br /&gt;    _pm = pm;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Somewhere else&lt;br /&gt;Application.Run(new Form1(new PModel()));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Then, to ensure that the "serial number" checkbox (an instance of UpdateCheckBox) is enabled at the right time, use the WinForms designer to create an event handler for the "GetEnabled" event, and put the following code in it:&lt;br /&gt;&lt;pre&gt;return _pm.SelectedPerson != null &amp;&amp; _pm.SelectedPerson.HasBike;&lt;/pre&gt;&lt;br /&gt;Similar code snippets must be written for the control's Text property and for properties of all the other controls on the dialog. I'm not going to explain right now how to actually use Independents and Dependents to create the presentation model; I would refer you to the &lt;a href="http://updatecontrols.net/doc/"&gt;Update Controls blog&lt;/a&gt; for that information. Right now I just want to point out the two key advantages of this approach over conventional event-driven code:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The code closely follows the requirement: "The serial number field is enabled only if 'Has a bike' is checked and a person is selected". If the requirements change, it's easy to update the code to match them.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;If you used Update Controls correctly (assuming no bugs in UC itself), there is no way the program can malfunction with respect to this requirement. It will always behave as that one line of code describes. &lt;i&gt;The UC Way automatically leads to fewer bugs&lt;/i&gt;--although if you're new to UC, bugs that do occur tend to be serious head-scratchers.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;I should point out one disadvantage to UpdateControls: it will permeate your codebase, changing the way you write both your ViewModels and your Models, so you'll be making a major commitment. And while UpdateControls integrates with WPF very closely, the WinForms version requires a special "Update" version of every control that you want to stay up-to-date automatically, and only a few properties currently support automatic updates. If you need to use 3rd-party Model classes in an UpdateControls app, you'll have to write &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;decorator&lt;/a&gt; classes to augment the model classes with "Independent sentries". Similarly, to use 3rd-party controls in a WinForms app, you'll need to create a derived class or a special container that has private "Dependent sentries" and updates them during Application.Idle events. For example, I had to write an UpdateTrackBar class to represent my time slider, because UpdateControls didn't come with one.&lt;br /&gt;&lt;br /&gt;Another issue, currently, is that the way UpdateControls handles lists is not scalable. Normally lists have a single "independent sentry" that controls updates to the lists. Adding, removing, or modifying a single item will cause each dependency to scan the entire list. If you are filtering a million-item list down to 100 items that you show on-screen, that million items will be scanned and filtered every time any item is added, removed, or changed, even if that item is not one of the 100 items displayed.&lt;br /&gt;&lt;br /&gt;I'm trying to think of a way to make it more scalable, but I have no solutions to offer yet. There is one mitigating factor: UpdateControls updates dependencies in a kind of "lazy" fashion. If you change several items at once (e.g. in a single method or for-loop) in the million-item list, each of its dependencies is typically updated only once (in WinForms, dependencies will be updated in the next Application.Idle event; in WPF they will be updated in a posted message. Early updates occur on-demand, however, e.g. if your code explicitly accesses the filtered list.)&lt;br /&gt;&lt;br /&gt;Still, I am quite certain that using a framework like UpdateControls will become an industry standard practice someday. &lt;br /&gt;&lt;br /&gt;To get started with UpdateControls yourself, read its &lt;a href="http://updatecontrols.net/doc/"&gt;blog&lt;/a&gt;, check out examples such as &lt;a href="http://updatecontrols.net/doc/mvvm-and-linq-to-sql"&gt;Noteworthy&lt;/a&gt;, and write a small app to try it out. Noteworthy is odd--a "blog editor" that doesn't have a body text field?--but it seems to be the best example available at the moment. If you have any questions about UpdateControls, it's probably best to ask them on the &lt;a href="http://updatecontrols.codeplex.com/discussions"&gt;discussions list&lt;/a&gt;. &lt;a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=3453924" rel="tag" style="display:none"&gt;CodeProject&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-3377634974219935624?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://updatecontrols.net/cs/index.shtml' title='Update Controls'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/3377634974219935624/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=3377634974219935624' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3377634974219935624'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3377634974219935624'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2011/04/update-controls.html' title='Update Controls'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-Dl_fqw2oUSE/TaC7i0WeR8I/AAAAAAAAACg/oxwJxvjQrNI/s72-c/PointlessForm.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-5159722831869243573</id><published>2010-09-16T12:15:00.013-06:00</published><updated>2010-09-16T13:29:43.890-06:00</updated><title type='text'>I want a "slide" operator</title><content type='html'>I've been writing imperative code for a long time and I noticed that I often extract a value from a variable, then change the variable immediately afterward. This is evident in numerous iterative algorithms, such as the fibonnaci algorithm:&lt;pre&gt;{&lt;br /&gt;  int a = 0, b = 1, tmp;&lt;br /&gt;  for(;;) {&lt;br /&gt;    print(u);&lt;br /&gt;    tmp = b;&lt;br /&gt;    b = a + b;&lt;br /&gt;    a = tmp;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;Notice how "a" gets the old value of b, and "b" gets the old value of a + b.&lt;br /&gt;&lt;br /&gt;Another pattern is that you want to do something the first time some code is reached:&lt;br /&gt;&lt;pre&gt;if (!initialized) {&lt;br /&gt;    initialized = true;&lt;br /&gt;    DoSomething();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Again, we read the value of the variable and then change it.&lt;br /&gt;&lt;br /&gt;What if the user clicks a column on a ListView, then clicks a second column? Then the list should be sorted first by the second column, then by the column that was clicked earlier. I wrote some code for this earlier today:&lt;pre&gt;public void SortByColumn(int column)&lt;br /&gt;{&lt;br /&gt; if (_primarySortColumn == column) {&lt;br /&gt;  _secondarySortAscending = !_secondarySortAscending;&lt;br /&gt;  _primarySortAscending = !_primarySortAscending;&lt;br /&gt; } else {&lt;br /&gt;  _secondarySortColumn = _primarySortColumn;&lt;br /&gt;  _secondarySortAscending = _primarySortAscending;&lt;br /&gt;  _primarySortColumn = column;&lt;br /&gt;  _primarySortAscending = true;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;Here, the pattern occurs in the "else" clause.&lt;br /&gt;&lt;br /&gt;Since this pattern happens so much, I think there should be an operator for it, let's say "&amp;lt;-". A natural name for it would be the "shift" operator, but I'll call it the "slide" operator or maybe the "rotate" operator, to distinguish it from the bitwise left shift "&lt;&lt;". Using this operator, fibonnaci becomes&lt;pre&gt;{&lt;br /&gt;  int a = 0, b = 1;&lt;br /&gt;  for(;;) {&lt;br /&gt;    print(a);&lt;br /&gt;    a &amp;lt;- b &amp;lt;- a + b;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;The operator is the same as the assignment operator, except that the result of the expression is the old value of the left-hand side, not the new value. So "a &amp;lt;- b &amp;lt;- a + b" is equivalent to "a &amp;lt;- (b &amp;lt;- a + b)", which is equivalent to "temp = b, b = a + b, a = temp". Note that "a &amp;lt;- b &amp;lt;- a + b" is also equivalent to "a = b &amp;lt;- a + b"; since the old value of "a" is discarded, it doesn't matter whether we use "=" or "&amp;lt;-" to change it.&lt;br /&gt;&lt;br /&gt;The initialization code could be expressed more briefly with the slide operator:&lt;pre&gt;if (!(initialized &amp;lt;- true))&lt;br /&gt;    DoSomething();&lt;br /&gt;&lt;/pre&gt;And similarly for the sorting code:&lt;pre&gt;_secondarySortColumn &amp;lt;- _primarySortColumn &amp;lt;- column;&lt;br /&gt;_secondarySortAscending &amp;lt;- primarySortAscending &amp;lt;- true;&lt;/pre&gt;&lt;br /&gt;The semantics of the slide operator imply creation of a temporary variable, but probably in some cases the temporary can be eliminated if the compiler can determine that the order of operations doesn't matter, or that the temporary variable isn't being used.&lt;br /&gt;&lt;br /&gt;The operator could also be used for swapping values:&lt;pre&gt;a &amp;lt;- b &amp;lt;- a&lt;/pre&gt;&lt;br /&gt;A second temporary variable is needed in case the left-hand side is expensive or has side-effects, in which case it should only be evaluated once. For example, if the code is&lt;pre&gt;savedValue &amp;lt;- GetActiveItem().Value &amp;lt;- savedValue&lt;/pre&gt;then GetActiveItem() shouldn't be called twice, so another temporary must be introduced (the exact details depend on the programming language):&lt;pre&gt;temp1 = GetActiveItem();&lt;br /&gt;temp2 = temp1.Value;&lt;br /&gt;temp1.Value = savedValue;&lt;br /&gt;savedValue = temp2;&lt;/pre&gt;Now when can I get my slide operator in a real language?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-5159722831869243573?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/5159722831869243573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=5159722831869243573' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5159722831869243573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5159722831869243573'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/09/i-want-slide-operator.html' title='I want a &quot;slide&quot; operator'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-3086902079731498791</id><published>2010-08-11T14:27:00.025-06:00</published><updated>2010-08-12T18:45:10.991-06:00</updated><title type='text'>DI and Pervasive services</title><content type='html'>I have been learning recently about Dependency Injection for loose coupling (see for example &lt;a href="http://www.codeproject.com/KB/architecture/DependencyInjection.aspx"&gt;these&lt;/a&gt; &lt;a href="http://www.codeproject.com/KB/architecture/di-with-autofac.aspx"&gt;articles&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;DI is really very simple. When a class X is designed for DI, it means that&lt;ol&gt;&lt;li&gt;X itself avoids specifying what other &lt;i&gt;classes&lt;/i&gt; it depends on. For example, it avoids calling "new Y()" or using a singleton class, and it accesses the services it needs through interfaces (or, occasionally, abstract classes) rather than references to concrete types.&lt;/li&gt;&lt;li&gt;X's dependencies are exposed explicitly in the constructor, or by properties of X, so that whoever creates/manages X can choose the dependencies.&lt;/li&gt;&lt;/ol&gt;Because X doesn't choose its own dependencies, a third party can do so on its behalf. This makes it possible to swap out a service that X needs for some other service without changing X itself, which may allow X to be used in more situations than it was designed for. For example, if X needs a database service, it would take a reference to some database-related interface (e.g. IQueryable&amp;lt;T&amp;gt; or whatever, I don't know, I don't use databases much myself) in its constructor. Now X may have initially been intended to use a MySQL database, but because X doesn't open the database connection itself, you may be able to switch to PostgreSQL or Oracle without changing X at all. For unit testing you might not want to use a real database, so you could use a "&lt;a href="http://en.wikipedia.org/wiki/Mock_object"&gt;mock object&lt;/a&gt;" that contains simulated data, based on LINQ-to-XML or something.&lt;br /&gt;&lt;br /&gt;Often, there are often many levels of dependency. For example, a program for editing documents may have a main window, which contains a document editor, and toolbars/menus that depend on the document editor. The editor in turn depends on a document object and various user interface services. The document may depend on services for undo/redo, various data structures, and disk access, while the user interface services may depend on other data structures, drawing services, spatial analysis algorithms... who knows.&lt;br /&gt;&lt;br /&gt;Often, in an app designed with DI, all the different components are wired together in a single place if possible, so that you can look in one place to see how components are connected and dependent on each other.&lt;br /&gt;&lt;br /&gt;As the application grows more complex, the code that initializes all these objects via dependency injection also grows more complex. Eventually, you may get to a point where an IoC/DI framework like &lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt; or &lt;a href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; or (my tentative favorite) &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt; can simplify all that initialization work.&lt;br /&gt;&lt;br /&gt;But there are some services that are pervasive, services that you would have to pass to a hundred different constructors if you want to use DI "properly". Some examples are localization (to provide French and Spanish translations), logging (almost any component might want to write a diagnostic message), profiling (to gather performance statistics), and possibly "config options" (so end-users or admins can configure multiple components through a command-line, xml file or other source). In a compiler, a service for error/warning messages might be used all over the place. In &lt;a href="http://sourceforge.net/projects/loyc/"&gt;Loyc&lt;/a&gt;, there might ultimately be hundreds of components that need to create AST Nodes or use other "pervasive" services.&lt;br /&gt;&lt;br /&gt;It looks to me like passing such common services to constructors is more trouble than it's worth. If a component of Loyc may produce a warning message, is user-configurable, creates new AST nodes, and needs localization support, that's 4 constructor arguments just for "pervasive" services, never mind the more important, "meaty" services that it might need, like a parser, graph algorithm or whatever. If you use constructor injection, dozens of constructors are starting to smell.&lt;br /&gt;&lt;br /&gt;How can we avoid the burden of passing around lots of pervasive service references? I'm not sure what the best way is. I have an idea in mind, but it is not appropriate for all cases. My idea involves a global singleton that can be swapped out temporarily while performing a specific task. I tentatively call it the Ambient Service Pattern.&lt;br /&gt;&lt;br /&gt;For instance, in a compiler, the error/warning service might print to the console by default, or to an output window, but certain types of analysis might be "transactional" or "tentative": if an error occurs, the operation is aborted, and no error is printed, although if a warning occurs, it is buffered and printed if the operation succeeds. A concrete example of this scenario is the C++ rule known as &lt;a href="http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error"&gt;SFINAE&lt;/a&gt;. Template substitution may produce an error, but if that error occurs during overload resolution, it is not really an error and no message should be printed. Given a global error service, we can model this rule by switching to a special error service, performing the operation, and then switching back afterward.&lt;br /&gt;&lt;br /&gt;To implement this pattern, use a thread-local variable alongside the interface to manage the service:&lt;pre&gt;interface IService { ... }&lt;br /&gt;&lt;br /&gt;class Service&lt;br /&gt;{&lt;br /&gt;    static ThreadLocalVariable&amp;lt;IService&amp;gt; _cur =&lt;br /&gt;       new ThreadLocalVariable&amp;lt;IService&amp;gt;();&lt;br /&gt;&lt;br /&gt;    // Current service&lt;br /&gt;    public static Cur { get { return _cur.Value; } }&lt;br /&gt;&lt;br /&gt;    // Use to install a new service temporarily, or to install the&lt;br /&gt;    // initial service when the program begins&lt;br /&gt;    public static PushedTLV&amp;lt;IService&amp;gt; Push(IService newValue)&lt;br /&gt;        { return new PushedTLV&amp;lt;IService&amp;gt;(_cur, newValue); }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Depending on the app, there could be different threads doing independent work, which is why the thread-local variable is needed.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note&lt;/b&gt;: if you need to support threads that fork, there is a problem because .NET thread-local variables &lt;a href="http://stackoverflow.com/questions/2106427/inheriting-threadstatic-values-to-implement-dynamic-scoping-in-c-net-in-multith"&gt;cannot inherit their value from the parent thread&lt;/a&gt;. To work around this problem in Loyc I made a whole &lt;a href="http://loyc.svn.sourceforge.net/viewvc/loyc/Src/Runtime/ThreadEx.cs?revision=104&amp;view=markup"&gt;infrastructure&lt;/a&gt; for thread creation, with a ThreadEx to wrap the standard Thread class, and a ThreadLocalVariable&amp;lt;T&amp;gt; class to be used instead of [ThreadStatic], which registers itself in a global weak reference collection so that when a thread is created with ThreadEx, the values of all ThreadLocalVariables can be propagated from the parent thread to the child thread (custom propagation behavior is also supported). Obviously, this workaround is a huge pain in the ass since all code must agree to use ThreadEx and the new .NET stuff like the "Parallel Extensions" won't propagate the thread local variables. I guess to properly support .NET 4, I should try to find a new solution based on &lt;a href="http://msdn.microsoft.com/en-us/library/dd642243.aspx"&gt;ThreadLocal&amp;lt;T&amp;gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;PushedTLV is a helper class that changes the value of a thread-local variable until it is disposed. Use it like this:&lt;pre&gt;using (var old = Service.Push(newService)) {&lt;br /&gt;    // Perform an operation that will use the new service&lt;br /&gt;}&lt;br /&gt;// old service is automatically restored&lt;/pre&gt;&lt;br /&gt;An unfortunate consequence of this pattern is that the interface &lt;i&gt;appears&lt;/i&gt; to be coupled to the thread-local variable. Sure, you could still write a class X that has a constructor-injected IService, but this might confuse those that indirectly use X: if someone changes Service.Cur, they might assume all code that needs an IService will use Service.Cur, but X could be using a different IService.&lt;br /&gt;&lt;br /&gt;While the Ambient Service Pattern doesn't work like traditional dependency injection, it still follows the spirit of DI because components remain independent from a specific implementation of the pervasive service.&lt;br /&gt;&lt;br /&gt;In the version of the pattern seen here, the service provided acts as a singleton. Of course, if the service is something that can be instantiated on-demand (e.g. a data structure), the thread-local variable would hold a factory instead of a singleton. The "Push()" method would switch to a different factory instead of a different instance, and there would be a "New()" method instead of a "Cur" property.&lt;br /&gt;&lt;br /&gt;I wonder if the .NET Framework itself should adopt this kind of pattern. Consider what you do to open a file: you call File.Open() or make a FileStream, right? Now what if management decides "we need our app to be able to open files from an ftp site". Rather than change the code that calls File.Open() (and what if a third-party library is calling it?), wouldn't it be more elegant if you could swap in a new file management service that understands FTP sites (and perhaps maintains a local disk cache)? And hey, what if you want to change your console app to use a graphical console with icons and proportional fonts? What if you decide Debug.WriteLine needs to store a log somewhere?&lt;br /&gt;&lt;br /&gt;Here's my implementation of PushedTLV, but as I mentioned, it's based on my own special ThreadLocalVariable class.&lt;pre&gt;/// &amp;lt;summary&gt;Designed to be used in a "using" statement to alter a &lt;br /&gt;/// thread-local variable temporarily.&amp;lt;/summary&gt;&lt;br /&gt;public class PushedTLV&amp;lt;T&amp;gt; : IDisposable&lt;br /&gt;{&lt;br /&gt;    T _oldValue;&lt;br /&gt;    ThreadLocalVariable&amp;lt;T&amp;gt; _variable;&lt;br /&gt;&lt;br /&gt;    public PushedTLV(ThreadLocalVariable&amp;lt;T&amp;gt; variable, T newValue)&lt;br /&gt;    {&lt;br /&gt;        _variable = variable;&lt;br /&gt;        _oldValue = variable.Value;&lt;br /&gt;        variable.Value = newValue;&lt;br /&gt;    }&lt;br /&gt;    public void Dispose()&lt;br /&gt;    {&lt;br /&gt;        _variable.Value = _oldValue;&lt;br /&gt;    }&lt;br /&gt;  &lt;br /&gt;    public T OldValue { get { return _oldValue; } }&lt;br /&gt;    public T Value { get { return _variable.Value; } }&lt;br /&gt;}&lt;/pre&gt;Do you have another idea for managing pervasive services with minimum fuss? Do tell.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Now on &lt;a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=3453924" rel="tag"&gt;CodeProject&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-3086902079731498791?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/3086902079731498791/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=3086902079731498791' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3086902079731498791'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/3086902079731498791'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/08/pervasive-services-and-di.html' title='DI and Pervasive services'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-9165744851037998735</id><published>2010-06-10T13:26:00.013-06:00</published><updated>2010-06-10T16:18:15.727-06:00</updated><title type='text'>Cω stream flattening</title><content type='html'>The Microsoft research language Cω (C-omega), which predates C# 3.0 (and even 2.0?), extended C# with a number of interesting features, of which C# 3.0 includes several comparable features. To me the most interesting thing in Cω was streams, which are roughly just lists of things in IEnumerables. For instance, the stream "int*" basically means "IEnumerable&amp;lt;int&gt;", but they allowed you to manipulate lists of things without using explicit loops:&lt;pre&gt;int* FromTo(int from, int to)&lt;br /&gt;{&lt;br /&gt;    for (i = from; i &lt;= to; i++) yield return i;&lt;br /&gt;}&lt;br /&gt;void Example()&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(string.Join(", ",&lt;br /&gt;        ToArray(FromTo(1, 10)[it % 2 == 0].ToString())));&lt;br /&gt;    // Output: 2, 4, 6, 8, 10&lt;br /&gt;}&lt;/pre&gt;Luckily, LINQ provides similar features in C# 3.0, just with a different syntax:&lt;pre&gt;IEnumerable&amp;lt;int&gt; FromTo(int from, int to)&lt;br /&gt;{&lt;br /&gt;    for (int i = from; i &lt;= to; i++) yield return i; &lt;br /&gt;}&lt;br /&gt;void Ex()&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(string.Join(", ",&lt;br /&gt;        (from i in FromTo(1, 10) where i % 2 == 0 select i).ToString()).ToArray()));&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;There's one thing missing from C# 3 that I would really like, though: flattening. Quite simply, you can combine Cω streams easily with no performance hit. The Cω guys &lt;a href="http://research.microsoft.com/en-us/um/people/gmb/papers/ecoop-corrected.pdf"&gt;illustrated this in a paper&lt;/a&gt; with a silly, but illustrative, alternative to FromTo():&lt;pre&gt;int* FromToB(int b, int e)&lt;br /&gt;{&lt;br /&gt;    if (b&gt;e) yield break;&lt;br /&gt;    yield return b;&lt;br /&gt;    yield return FromTo2(b+1,e);&lt;br /&gt;}&lt;/pre&gt;They explain tersely:&lt;blockquote&gt;Without flattening we would be forced to copy the stream produced by the recursive invocation, leading to a quadratic instead of a linear number of yields:&lt;pre&gt;int* FromToC(int b, int e)&lt;br /&gt;{&lt;br /&gt;    if (b&gt;e) yield break;&lt;br /&gt;    yield return b;&lt;br /&gt;    foreach (int i in FromTo3(b+1,e)) yield return i;&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;&lt;br /&gt;Basically, FromToC does the same thing as FromToB, except that it is extremely slow. The reason is that when you enumerate over FromToC, a new enumerator is created on every iteration, and because the enumerators are nested, each enumerator that exists must be called in sequence to enumerate a &lt;i&gt;single&lt;/i&gt; value.&lt;br /&gt;&lt;br /&gt;This is perhaps better illustrated by the following practical example, which gives you a list of all subfolders of a folder:&lt;pre&gt;public IEnumerable&lt;string&gt; SubFolders(string path)&lt;br /&gt;{&lt;br /&gt;    yield return path;&lt;br /&gt;    foreach (string dir in Directory.GetDirectories(path))&lt;br /&gt;        foreach (string subfolder in SubFolders(dir))&lt;br /&gt;            yield return subfolder;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Suppose you write&lt;pre&gt;var folders = SubFolders(@"C:\").GetEnumerator();&lt;br /&gt;while (folders.MoveNext())&lt;br /&gt;    Console.WriteLine(folders.Current);&lt;/pre&gt;&lt;br /&gt;Imagine that somewhere in the middle of this process, the enumerator returns "C:\Windows\System32\drivers". This does not happen directly. First the MoveNext() method of the enumerator for "C:\" is called. That enumerator calls MoveNext() on its child enumerator, "C:\Windows". The child enumerator then calls MoveNext() on &lt;i&gt;its&lt;/i&gt; child, "C:\Windows\System", which advances to "C:\Windows\System32\drivers". Thus, for all subfolders that are 3 levels deep, the MoveNext() method is actually called 3 times. I hope you can see that the code will get slower as the directory structure gets deeper--not just because the strings are longer, but because the MoveNext() gets called one extra time for every level of nesting.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;Stream flattening&lt;/i&gt; is a feature of Cω that removes all the intermediate calls to MoveNext(), so that MoveNext() and Current are only called once for every folder returned, while at the same time making the code simpler:&lt;pre&gt;public IEnumerable&lt;string&gt; SubFolders(string path)&lt;br /&gt;{&lt;br /&gt;    yield return path;&lt;br /&gt;    foreach (string dir in Directory.GetDirectories(path))&lt;br /&gt;        yield return SubFolders(dir);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Stream flattening and the "*" syntax make it more convenient to write coroutines. Coroutines are useful for numerous purposes, but perhaps the most obvious is game programming. In games you need to program numerous "actors", each with its own behavior. Actors' behavior can be described in a linear way. For instance an enemy might have logic like this:&lt;pre&gt;- Wander around until player is within 10 metres&lt;br /&gt;- Attack the player while my health &gt; 25%&lt;br /&gt;- Retreat until distance from player exceeds 50 metres&lt;br /&gt;- Regenerate health at 1% per second&lt;br /&gt;- If at any time my health reaches 0%,&lt;br /&gt;  - run death animation&lt;br /&gt;  - wait 10 seconds&lt;br /&gt;  - despawn the body&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Logic like this could be expressed straightforwardly using streams... something like this:&lt;pre&gt;class Enemy : AbstractEnemy&lt;br /&gt;{&lt;br /&gt;    public override IAction* EnemyLogic()&lt;br /&gt;    {&lt;br /&gt;        // The game engine resumes this method every frame&lt;br /&gt;        foreach(var a in NormalBehavior()) {&lt;br /&gt;            yield return a;&lt;br /&gt;            if (Health &lt;= 0)&lt;br /&gt;                break;&lt;br /&gt;        }&lt;br /&gt;        yield return DeathAndUnspawn();&lt;br /&gt;    }&lt;br /&gt;    IAction* NormalBehavior()&lt;br /&gt;    {&lt;br /&gt;        for(;;) {&lt;br /&gt;            while(Distance &gt; 10) {&lt;br /&gt;                yield return Wander();&lt;br /&gt;                Health = Math.Min(Health + 0.01 * TimePerFrame, 1.0);&lt;br /&gt;            }&lt;br /&gt;            while(Health &gt; 0.25)&lt;br /&gt;                yield return Attack();&lt;br /&gt;            while(Distance &lt; 50)&lt;br /&gt;                yield return Retreat();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    IAction Attack() { ... }&lt;br /&gt;    IAction Retreat() { ... }&lt;br /&gt;    IAction Wander() { ... }&lt;br /&gt;&lt;br /&gt;    IAction* DeathAndUnspawn()&lt;br /&gt;    {&lt;br /&gt;        yield return DeathAnimation();&lt;br /&gt;        yield return Wait(10.0);&lt;br /&gt;        yield return Despawn();&lt;br /&gt;    }&lt;br /&gt;    IAction* DeathAnimation() { ... }&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;While you can certainly do this in regular C#, streams improve performance and make the syntax more convenient.&lt;br /&gt;&lt;br /&gt;Of course, &lt;b&gt;neither Cω streams nor C# generators were actually designed for coroutines&lt;/b&gt;; any function that contains code that can be "paused" must return some sort of IEnumerable, which can be inconvenient sometimes. For one thing, a pausable function cannot easily return a value to its immediate caller, only to its "highest-level" caller (such as the game engine).&lt;br /&gt;&lt;br /&gt;But, since the .NET framework doesn't support genuine coroutines, this is the only option right now. Mono recently introduced &lt;a href="http://www.mono-project.com/Continuations"&gt;continuations that it calls "tasklets"&lt;/a&gt;, which can also be used to write coroutines (but only under Mono, not Microsoft .NET); Since they apparently duplicate and restore the entire call stack, though, I am concerned that Tasklets might have poor performance.&lt;br /&gt;&lt;br /&gt;I don't actually know how the flattening is implemented; I'll be sure to update this entry if I figure it out.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-9165744851037998735?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/9165744851037998735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=9165744851037998735' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/9165744851037998735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/9165744851037998735'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/06/c-stream-flattening.html' title='Cω stream flattening'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-6099906112596278547</id><published>2010-05-21T13:38:00.037-06:00</published><updated>2012-01-21T13:48:03.710-07:00</updated><title type='text'>New features the .NET framework should have</title><content type='html'>I recently wrote a class called &lt;a href="http://www.codeproject.com/KB/recipes/cptrie.aspx"&gt;CPTrie&lt;/a&gt; that stores a sorted collection of strings or integers in less space than a Dictionary or SortedDictionary. It took a long time to develop this data structure in .NET while minimizing memory and CPU usage. I am fairly convinced that without some of .NET's restrictions, I could have made an even more compact data structure.&lt;br /&gt;&lt;br /&gt;Allow me to suggest a few features that would make .NET better by allowing it to do certain tasks more efficiently, or by giving coders more flexible design options.&lt;br /&gt;&lt;br /&gt;It is already easier to write efficient code in .NET than Java, of course, thanks to features like structs (value types), Generics and compiled expression trees. But it could be better.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Slices&lt;/h4&gt;I love the type safety that .NET provides, but people seem to forget that type safety doesn't have to be a straightjacket. They think, for instance, that pointers automatically kill type safety, apparently forgetting that "references" are nothing but neutered pointers. Pointers aren't inherently dangerous, rather it is certain operations that are dangerous, like reading or writing past the end of an array. If your language can prevent you from doing dangerous things with pointers then they suddenly become safe.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://golang.org/"&gt;Google Go&lt;/a&gt; recognizes this, and provides a safe form of pointer called a slice. A slice is a pointer to an array that stores the array size with the pointer. This is not the same as a .NET array reference, which always points to the beginning of the array. A slice can point to a subset of an array, and if .NET had slices, all functions of the form&lt;br /&gt;&lt;blockquote&gt;int Sum(int[] array, int startIndex, int endIndex) {...}&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;could be replaced simply with&lt;br /&gt;&lt;blockquote&gt;int Sum(int[..] array) {...}&lt;/int&gt;&lt;/blockquote&gt;&lt;br /&gt;And you would probably call the function with a nice syntax like&lt;br /&gt;&lt;blockquote&gt;int result = Sum(array[startIndex..endIndex]);&lt;br /&gt;// or Sum(array) to pass the whole array&lt;br /&gt;&lt;/blockquote&gt;Plus, these functions often come with a "convenience overload" to process the entire array...&lt;br /&gt;&lt;blockquote&gt;void Foo(int[] array) { Foo(array, 0, array.Length); }&lt;/blockquote&gt;...which would not be necessary with slices.&lt;br /&gt;&lt;br /&gt;Slices can be more efficient than the traditional triplet (array, start, end) for three reasons:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;A slice is two values (pointer + length). Passing it around is faster than passing around three values.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Triplets use 50% more memory (relevant if you need to store many of them)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;All functions that access an array through a triplet must check the array index on every access. For instance, in today's .NET, Sum() might be written like this:&lt;br /&gt;&lt;pre&gt;void Sum(int[] array, int startIndex, int endIndex)&lt;br /&gt;{&lt;br /&gt;    // I avoid using the "less than" sign because Blogger is still&lt;br /&gt;    // terribly buggy after several years. Lazy bums.&lt;br /&gt;    if (0&gt;startIndex || startIndex&gt;endIndex || endindex&gt;array.Length)&lt;br /&gt;       throw new ArgumentException();&lt;br /&gt;&lt;br /&gt;    int total = 0;&lt;br /&gt;    for (int i = startIndex; array.Length &gt; i; i++)&lt;br /&gt;       total += array[i];&lt;br /&gt;    return total;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;With slices, the code is shorter and the JIT optimizer could do a better job:&lt;br /&gt;&lt;pre&gt;void Sum(int[..] slice)&lt;br /&gt;{&lt;br /&gt;  int total = 0;&lt;br /&gt;  for (int i = 0; slice.Length &gt; i; i++)&lt;br /&gt;     total += slice[i];&lt;br /&gt;  return total;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Microsoft .NET can detect that a for loop goes from 0 to the array length, and optimize out the range check. It should be easy to do the same for slices, which could make a method like this significantly faster, yet still perfectly type-safe. Meanwhile, the JIT is unable to optimize the first loop (the one that uses an array) because it is too complex, and there may be additional overhead because the JIT can only hold 2 or 3 variables in registers.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;Slices would be even more useful if you could cast them in type-safe ways: for instance you should be able to cast a int[4] slice to a short[8] slice, and you should be able to cast a short[7] to an int[3] (there is the possibility of an alignment exception in the latter case on certain processors, but it is still "safe" in the sense that memory corruption is impossible). Slice casts would be extremely useful for parsing various binary data blobs (e.g. binary files, network packets), which are difficult to deal with in .NET right now.&lt;br /&gt;&lt;br /&gt;Such casts are perfectly safe, provided that casts involving reference types are prohibited.&lt;br /&gt;&lt;br /&gt;There's one disadvantage of allowing the slice cast: the optimizer would have to assume that a value-type slice may point to the same physical memory as a different kind of slice or array. But C has the same problem and it still wins the speed race.&lt;br /&gt;&lt;br /&gt;One roadblock to implementing slices in .NET may be a challenge to the garbage collector. In .NET today all references point to the beginning of an object. Slices can point to the middle of an object, which means that the garbage collector needs a way to find the beginning of an object given a pointer to the middle. If this is not possible, slices will have to be expanded to a triplet: a third value is required, pointing to the beginning of the object being kept alive by the slice.&lt;br /&gt;&lt;br /&gt;A slice in triplet form would still have a performance advantage over normal (array, index, length) triplets, because while the pointer to the beginning of the object would have to be passed around, it never has to be &lt;i&gt;accessed&lt;/i&gt; except by the garbage collector. On x86 this means it can be held only on the stack and not in a register, saving scarce registers for other things.&lt;br /&gt;&lt;br /&gt;Programmers should be able to get the slice for an entire array given a slice of just part of the array. However, if this is allowed, note the security implication: anyone returning a slice to a partial array must be aware that the receiver can get access to the entire array.&lt;br /&gt;&lt;br /&gt;Slices that point to null are also possible. .NET would have to guarantee that null slices always have a Length of 0. Note that null slices are advantageous over null array references, because you can access the Length field of a slice without risking a NullReferenceException, so you need not write code to handle both "null" slices and "zero-length" slices.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Fixed-size arrays&lt;/h4&gt;In some cases it would be really nice to have zero-overhead fixed-size arrays. For instance, I have seen people representing points like this:&lt;pre&gt;class Point {&lt;br /&gt;    private float[] _coords = new float[3];&lt;br /&gt;    public float X { get { return _coords[0]; } set { _coords[0] = value; } }&lt;br /&gt;    public float Y { get { return _coords[1]; } set { _coords[1] = value; } }&lt;br /&gt;    public float Z { get { return _coords[2]; } set { _coords[2] = value; } }&lt;br /&gt;    public float this[int n] {&lt;br /&gt;                     get { return _coords[n]; } set { _coords[n] = value; }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This is very inefficient if you need to keep track of large numbers of points, since the 12 bytes of coordinate data is accompanied by 20 bytes of overhead: 8 bytes for the class and 12 bytes for the array. Moreover, all access to the X, Y and Z properties is guarded by an array range check, even though the subscripts and array size are constants. Only the indexer should require a range check.&lt;br /&gt;&lt;br /&gt;To solve these problems, .NET should offer "safe" fixed-size arrays that hold the array directly in the container class or struct. This would save 12 bytes of overhead and allow faster access to the array, as well as faster object construction.&lt;br /&gt;&lt;br /&gt;You can already use fixed-size arrays in .NET, but they are considered "unsafe" and have the following disadvantages:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;They require you to sprinkle the "unsafe" keyword all over the place.&lt;/li&gt;&lt;li&gt;You have to set a compiler option to allow "unsafe" code.&lt;/li&gt;&lt;li&gt;Your code cannot run in secure environments, such as inside a web browser.&lt;/li&gt;&lt;li&gt;Fixed-size arrays cannot hold references to classes, or structs (only primitives are allowed).&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Provided that there are array index range checks, there is really nothing unsafe about a fixed-size array, and no reason they can't hold references or structs. The reason for these silly restrictions (I think) is that the CLR doesn't have built-in support for fixed-size arrays, so C# resorts to pointer arithmetic, which is unverifiable to the CLR (and in that sense "unsafe").&lt;br /&gt;&lt;br /&gt;Fixed-size arrays would work very nicely with slices, so that you could offer access to the array via a property:&lt;pre&gt;class Point {&lt;br /&gt;    private fixed float _coords[3];&lt;br /&gt;    ...&lt;br /&gt;    public float[..] Coords { get { return _coords; } }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;However, this would only be legal inside classes. A struct could not return a slice of itself, since a struct could cease to exist before the slice does.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Go interfaces&lt;/h4&gt;In Go, classes do not have to explicitly say what interfaces they implement, and anybody that notices a pattern in the methods of two otherwise unrelated classes can declare that they both implement a certain interface.&lt;br /&gt;&lt;br /&gt;Go's interfaces provide compile-time type checking, and since Go is built heavily around these "implicit" interfaces, I assume Google found a way to give them high performance similar to .NET interfaces. As such I think Microsoft would be smart to consider adding support for Go-style interfaces, which in turn would allow a fast implementation of the Go language on the CLR.&lt;br /&gt;&lt;br /&gt;Go-style interfaces would allow developers to work around limitations in the way Microsoft designed interfaces. For instance, there are a lot of read-only collection classes (or collections like Stack where modifications are constrained), but Microsoft didn't bother to make a IReadOnlyCollection interface, so we're stuck writing four unsupported methods (Add, Clear, Remove, CopyTo) in addition to the three that matter (GetEnumerator, Count, Contains).&lt;br /&gt;&lt;br /&gt;With Go interfaces, you can define your own private IReadOnlyCollection interface which all the BCL collections, and any collections made by third parties, automatically implement.&lt;br /&gt;&lt;br /&gt;(I am not, of course, suggesting that traditional interfaces be abandoned. They are too well entrenched and, I have learned, are faster in some cases anyhow.)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Update&lt;/b&gt;: it turns out this feature would be insufficient to support Go. In Go you can define new methods for classes defined elsewhere (or for primitive types, pointers, etc.) These could be compared to extension methods, but Go is not object-oriented and it's not really the same. In Go, as I understand it, an interface can bundle together methods defined in multiple modules that happen to operate on the same data type. I don't understand how it works - I haven't been able to find information on their dispatch mechanism. Anyway, the point is, if MS really wanted to support Go on .NET they'd have to study it more carefully than I have.&lt;br /&gt;&lt;br /&gt;Go interfaces are comparable to Visual Basic 9's new "dynamic interfaces", except that in Go you get a error when you try to cast an object to an interface that the object does not support. In VB an exception occurs when you call an unsupported method on the interface, not during the cast.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Return type covariance&lt;/h4&gt;If class Circle is derived from class Shape, then the following code is perfectly safe and should be legal:&lt;pre&gt;&lt;br /&gt;abstract class ShapeFactory {&lt;br /&gt;    abstract public Shape New();&lt;br /&gt;}&lt;br /&gt;abstract class CircleFactory {&lt;br /&gt;    override public Circle New() { ... }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;I have encountered many situations where covariance would be useful, and it pains me to have to find a different design or &lt;a href="http://loyc-etc.blogspot.com/2008/09/simulating-covariant-return-types-in-c.html"&gt;implement a workaround&lt;/a&gt; just because Microsoft won't add this trivial feature.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Thread-local variable inheritance&lt;/h4&gt;Just a small pet peeve. When you create a child thread, it loses all the values of thread-local variables from the parent. There should be an option to copy values from the parent thread. I heard a rumor that somebody has a patent on this trivial idea, though.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Update&lt;/b&gt;: My own &lt;a href="http://loyc-etc.blogspot.com/2010/08/pervasive-services-and-di.html"&gt;Ambient Service Pattern&lt;/a&gt; cannot work reliably without this feature.&lt;br /&gt;&lt;br /&gt;Microsoft doesn't actually have to infringe that ridiculous patent to support the feature, either: they can simply (1) fire an event on each child thread when it starts, while blocking the parent thread, and (2) provide a way for the child thread to access thread-local variables of the parent thread. This way, Microsoft is merely giving developers the tools they need to make it possible to infringe the patent. Possible is a big improvement over impossible, friends.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Binary-comparing two value types&lt;/h4&gt;Another trivial feature that Microsoft left out of .NET is the ability to bitwise-compare two values. While developing the &lt;a href="http://www.codeproject.com/KB/collections/vlist.aspx"&gt;VList data structures for .NET&lt;/a&gt;, I really wanted this feature. There's no reason the 'ceq' opcode shouldn't support it, and as far as I can tell the &lt;a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm"&gt;ECMA-335 standard&lt;/a&gt; does not state whether 'ceq' can be used on arbitrary value types. However, C# doesn't allow it so I assume it's not supported.&lt;br /&gt;&lt;br /&gt;It's true that bitwise equality isn't "perfect". For instance, floating point has a "positive zero" and a "negative zero" that compare equal, but are bitwise unequal. However, if two value types ARE bitwise equal then they are the same for all practical purposes, and it's hard to think why you would waste time calling Equals() (which, in structs, uses reflection by default!)&lt;br /&gt;&lt;br /&gt;In the case of reference types you can call object.ReferenceEquals to find out if two references are the same, but what if you are writing generic code that handles both values &lt;b&gt;and&lt;/b&gt; references? In that case you cannot use object.ReferenceEquals, which is why a bitwise comparison operator would be so useful.&lt;br /&gt;&lt;br /&gt;A concrete example of this occurs in my VList code, which has a "Smart Select" method:&lt;pre&gt;&lt;br /&gt;public FVList&amp;lt;T&gt; SmartSelect(Func&amp;lt;T, T&gt; map)&lt;br /&gt;&lt;/pre&gt;This method of FVList passes each element of the FVList to the map() function. If, for &lt;span style="font-weight:bold;"&gt;every&lt;/span&gt; element of the list, map() returns the same value as it was given, SmartSelect() can simply return the original list instead of a new one. The ability to do this is very important for efficient functional programming; unfortunately, .NET screws it up by making it difficult to tell whether map() returned its argument unchanged or not. We have to use virtual function calls, and maybe even reflection, to tell whether the output is the same as the input. That's stupid.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Integers in pointers&lt;/h4&gt;Okay, what I'm about to suggest may sound crazy. You have been warned.&lt;br /&gt;&lt;br /&gt;In .NET, all references have 0s in the bottom two bits of the pointer. This is also true for C++ pointers to heap objects, and the &lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt; language interpreter and the &lt;a href="http://judy.sourceforge.net/"&gt;Judy array&lt;/a&gt; library use this fact to store a kind of safe union in a pointer:&lt;pre&gt;union {&lt;br /&gt;    Object* pointer;&lt;br /&gt;    int integer;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;If the two lowest bits are 0, the value is a pointer, otherwise it is an integer of some kind. A similar technique is to use the lowest bit to discriminate a union between two pointer types:&lt;pre&gt;union {&lt;br /&gt;    Object0* pointer0; // if low bit is 0&lt;br /&gt;    Object1* pointer1; // if low bit is 1&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;While abusing pointers like this in C is dangerous (since you could easily dereference an integer by mistake, etc.), formalizing the technique can make it safe (just as the Ruby language is safe from memory corruption despite using this kind of union extensively).&lt;br /&gt;&lt;br /&gt;In C#, the second union would be almost pointless since objects are self-typed, but it might be useful instead if you could use the low bit of a pointer as a boolean flag. For instance, .NET's SortedDictionary uses the following node structure:&lt;pre&gt;internal class Node&lt;br /&gt;{&lt;br /&gt;    private bool isRed;&lt;br /&gt;    private T item;&lt;br /&gt;    private Node left;&lt;br /&gt;    private Node right;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;There's something to be said for a straightforward implementation like this, but if somebody's using this class extensively with a large data set, they might appreciate the memory savings (up to 17%) that would come from hiding the "isRed" flag inside the 'left' or 'right' reference.&lt;br /&gt;&lt;br /&gt;Of course, this technique is pretty obscure, and is rarely needed, yet occasionally it is the only efficient technique to do the job. In Ruby this feature is key to the type system that unifies integers and objects. In Ruby "everything is an object", but storing integers on the heap is very inefficient. Although all Ruby variables are implemented as pointers, if you store a small integer in a variable, rather than allocating a heap object, Ruby just encodes the integer right into the pointer. Can you think of an approach that is remotely as efficient? Similarly, the Judy array's ability to store data compactly is heavily reliant on the fact that it can change the meaning of a "pointer" based on its low bits. I wanted to port Judy to .NET, and struggled for quite some time before concluding that approximating Judy is impossible in .NET.&lt;br /&gt;&lt;br /&gt;To minimize changes to the CLR, I would propose a single pointer union be defined, in which the 2 low bits are given the following meanings:&lt;ul&gt;&lt;li&gt;00: A pointer with a "false flag"&lt;/li&gt;&lt;li&gt;10: A pointer with a "true flag"&lt;/li&gt;&lt;li&gt;01 and 11: An integer (shift right 1 to get its value)&lt;/li&gt;&lt;/ul&gt;To store these values, the .NET BCL would define a special structure:&lt;pre&gt;struct PointerOrInteger&amp;lt;T&gt; where T:class&lt;br /&gt;{&lt;br /&gt;    object _ptr;                 // not directly accessible&lt;br /&gt;    bool   IsInteger { get; }    // returns ((int)_ptr &amp; 1) != 0&lt;br /&gt;    T      Pointer { get; set; } // getter returns IsInteger ? null : _ptr &amp; ~3&lt;br /&gt;    IntPtr Integer { get; set; } // getter returns IsInteger ? (IntPtr)_ptr &gt;&gt; 1 : 0&lt;br /&gt;    int    Int32   { get; set; } // getter returns IsInteger ? (int)   _ptr &gt;&gt; 1 : 0&lt;br /&gt;    bool   Flag    { get; set; } // getter returns (_ptr &amp; 2) != 0&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The garbage collector &lt;i&gt;could&lt;/i&gt; use reflection/metadata to detect the presence of PointerOrInteger, in order to single it out for special treatment. However, it might be simpler if the GC assumed any pointer could be PointerOrInteger and therefore tested low two bits of every pointer before following it. If the two bits are 0, it can be followed normally; otherwise it is presumably a PointerOrInteger and the pointer must be retrieved through the Pointer property.&lt;br /&gt;&lt;br /&gt;The disadvantage of this feature would be that the garbage collector would have to be aware of these special pointer types; code to deal with them might slow down the GC slightly, even in code that doesn't use the special pointers. However, this feature could probably be added without modifying CIL or the JIT, as the code to manipulate them could be neatly tucked away in PointerOrInteger.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;SIMD Vector types&lt;/h4&gt;Microsoft ought to copy SIMD support from Mono. &lt;a href="http://tirania.org/blog/archive/2008/Nov-03.html"&gt;It's all explained here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;New features C# should have&lt;/h3&gt;The above features would need CLR changes, but many useful features need only language support.&lt;br /&gt;&lt;h4&gt;Default interface implementations&lt;/h4&gt;Interfaces should be able to have default implementations. For instance, ICollection.Contains(T) could have a default implementation like this:&lt;pre&gt;&lt;br /&gt;bool Contains(T item) default {&lt;br /&gt;    var c = EqualityComparer&amp;lt;T&gt;.Default;&lt;br /&gt;    foreach(T t in this)&lt;br /&gt;        if (item.Equals(t))&lt;br /&gt;            return true;&lt;br /&gt;    return false;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Half the time this is exactly how one implements the Contains() method. Why require every collection author to write the same code? Of course, &lt;a href="http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf"&gt;traits&lt;/a&gt; could also help us avoid code duplication and would be more powerful, so let me stick that on my list:&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Traits&lt;/h4&gt;&lt;a href="http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf"&gt;Read about them in this PDF&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Units&lt;/h4&gt;The traditional type system (roughly speaking) classifies data by its structure: an array holds data in a certain way; a linked list holds data in a different way. There is an orthogonal concept of type, the "unit" type, which says something about the data itself. For example, an integer may contain a quantity measured in bytes, kilobytes, kilometres, sectors or pixels. A double has a different physical structure than an int, but it could have the same unit type.&lt;br /&gt;&lt;br /&gt;It is safe to add or subtract two values as long as they have the same units. I can add 10 bytes to 5 bytes and get 15 bytes, but if I add 5 bytes to 10 DWORDs there is probably an error in my code. Every engineer and scientist knows that if you multiply "2 kg" times "2 m/s^2", the result is "4 N", not "4 lbf" and code that computes the latter result might have destroyed the &lt;a href="http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#The_metric.2Fimperial_mix-up"&gt;Mars Climate Orbiter&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;A unit type checker like the one I wrote for &lt;a href="http://boo.codehaus.org/"&gt;boo&lt;/a&gt; would semi-automatically detect such errors and emit a warning message. Unit checking would work best if it had some support from the Microsoft BCL (Base Class Library). That's because a unit checker needs to track units as they pass through the BCL. For instance, if I write "Math.Abs(-7`pixels`), the unit checker has to be told that the return value of Math.Abs() has the same units as its argument. It would be nice if the BCL had attributes on its methods to provide this kind of information.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Tuples&lt;/h4&gt;C# should contain language support for tuples and "don't care" return values:&lt;pre&gt;&lt;br /&gt;Tuple&amp;lt;int,int&gt; DivMod(int n, int d)&lt;br /&gt;{&lt;br /&gt;    return (n/d, n%d);&lt;br /&gt;}&lt;br /&gt;void Example()&lt;br /&gt;{&lt;br /&gt;    int div, mod, five;&lt;br /&gt;    (div, mod) = DivMod(10, 3);&lt;br /&gt;    (five, _) = DivMod(25, 5); // "_" means "Don't care" (provided "_" is undefined)&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Note that Tuple classes come with .NET Framework 4.0. For unpacking tuples, the compiler should not require the official Tuple class; instead it should allow any data type that has properties named Item1, Item2, etc. This would allow small tuples to be returned more efficiently in a structure:&lt;pre&gt;struct Pair&amp;lt;A,B&gt; {&lt;br /&gt;    public A Item1 { get; set; }&lt;br /&gt;    public B Item2 { get; set; }&lt;br /&gt;}&lt;br /&gt;Pair&amp;lt;int,int&gt; DivMod(int n, int d)&lt;br /&gt;{&lt;br /&gt;    return new Pair&amp;lt;int,int&gt; { Item1=n/d, Item2=n%d };&lt;br /&gt;}&lt;br /&gt;void Example()&lt;br /&gt;{&lt;br /&gt;    int div, mod;&lt;br /&gt;    (div, mod) = DivMod(10, 3);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Update&lt;/b&gt;: there are various other ways this idea could work, too. Some have proposed extending the concept of anonymous classes so they can be returned from methods, but this approach is problematic when independent DLLs are involved. Suppose one DLL return a (string Key, string Value) pair and an independent DLL accepts a pair with exactly the same signature. You should be able to pass the tuple directly from one DLL to the other, but that wouldn't work in practice because .NET doesn't support any kind of "&lt;a href="http://en.wikipedia.org/wiki/Structural_type_system"&gt;structural typing&lt;/a&gt;".&lt;br /&gt;&lt;br /&gt;For that reason I think an approach based primarily on the standard Tuple type would be best, with support for user-defined types when they are needed.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Inline variable declarations&lt;/h4&gt;The tuple feature would be more elegant if C# allowed variables to be declared where they are first used:&lt;pre&gt;void Example()&lt;br /&gt;{&lt;br /&gt;    (var div, var mod) = DivMod(10, 3);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;When I first wrote this article, I said I would like to be able to just create variables implicitly like in boo:&lt;pre&gt;int ReadNumber()&lt;br /&gt;{&lt;br /&gt;    do { line = Console.ReadLine(); }&lt;br /&gt;    while (!int.TryParse(line, out value));&lt;br /&gt;    return value;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;No doubt, however, this feature would create even more uproar than the "var" statement did. I think, though, that syntax like "int.TryParse(line, out var value)" is a reasonable compromise.&lt;br /&gt;&lt;br /&gt;I have another idea for making code more concise and easier to optimize, namely, a ":" or ":=" operator to create "cached" values. You see, it's really easy to write code that repeats work unnecessarily:&lt;br /&gt;&lt;pre&gt;int GetFoo(object key, int seed)&lt;br /&gt;{&lt;br /&gt;    if (BTree[key].Record != null)&lt;br /&gt;        return BTree[key].Record.GetFoo(seed);&lt;br /&gt;    else&lt;br /&gt;        return seed;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Sure, the expression BTree[key].Record might be expensive to compute twice, but you'd have to do extra work to refactor it into a special local variable. Nobody wants that! So many/most developers will take the performance hit, and not worry unless it starts to get slow on their 3 GHz workstation with 8 GB of RAM. Too bad half of the users are on netbooks.&lt;br /&gt;&lt;br /&gt;I strongly believe C# should make it easy to write fast code. This kind of case is the most common optimization I can think of that cannot be automated: only the developer knows if a result is safe to cache. So I propose this syntax for caching values:&lt;pre&gt;int GetFoo(object key, int seed)&lt;br /&gt;{&lt;br /&gt;    if (r:BTree[key].Record != null)&lt;br /&gt;        return r.GetFoo(seed);&lt;br /&gt;    else&lt;br /&gt;        return seed;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The "x:y" operator would simply be a more compact form of "var x = y" with a higher precedence than most other operators, to make it easy to use inside expressions.&lt;br /&gt;Regrettably, "x:" cannot be allowed at the beginning of a statement because it could be mistaken for a goto-label (you'd have to use var x = y in that case.)&lt;br /&gt;&lt;br /&gt;I chose this syntax because it minimizes clutter. It looks good without any whitespace around the colon, which is important to convey the idea that the operator has high precedence. High precedence, in turn, is important to reduce the number of cases where you need parenthesis, since parenthesis increase typing effort and reduce readability.&lt;br /&gt;&lt;br /&gt;When used in an if-statement or loop expression, it's hard to say if the scope of the new variable (or value--maybe it should be read-only?) should extend outside the if-statement or loop expression. Perhaps if the user writes "var x = y" the scope should be limited to the loop (because the for and foreach loops already work that way), but if the user writes "x:y" then the scope should extend to the containing block. After all, there are many cases where you might want to use the cached value later on.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Cω Streams&lt;/h4&gt;This I'll make into a &lt;a href="http://loyc-etc.blogspot.com/2010/06/c-stream-flattening.html"&gt;separate post&lt;/a&gt;.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Now on &lt;a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=3453924" rel="tag"&gt;CodeProject&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-6099906112596278547?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/6099906112596278547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=6099906112596278547' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6099906112596278547'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6099906112596278547'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/05/new-features-net-framework-should-have.html' title='New features the .NET framework should have'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-6571226677945802827</id><published>2010-05-06T11:00:00.025-06:00</published><updated>2011-07-06T11:45:54.231-06:00</updated><title type='text'>Why C# is better than C++</title><content type='html'>I could phrase this as "why I hate C++" or "&lt;a href="http://cplusplussucks.com/"&gt;why C++ sucks&lt;/a&gt;", but let's try the more positive spin, "why C# is better". The reasons are so numerous and compelling that there is only one reason to use C++ instead, and that is better performance.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;C# compiles much faster&lt;/li&gt;&lt;li&gt;IntelliSense is much more reliable and faster (press F12 in Visual Studio to see the definition of any symbol)&lt;/li&gt;&lt;li&gt;Automatic memory management cuts your development time in half &lt;span style="font-style: italic;"&gt;all by itself&lt;/span&gt; - not just because you write less code, but also because you'll never have to track down "double free" problems and you'll very rarely have memory leaks (and if do you have memory leaks, &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;amp;displaylang=en"&gt;CLR profiler&lt;/a&gt; can help track them down)&lt;/li&gt;&lt;li&gt;No weird errors caused by #include order or #defines&lt;/li&gt;&lt;li&gt;No more buffer overflows or other C-related security vulnerabilities&lt;/li&gt;&lt;li&gt;Debugging is much easier; you can execute arbitrary expressions and call your own functions and properties from within Visual Studio's debugger (and &lt;a href="http://www.icsharpcode.net/opensource/sd/"&gt;SharpDevelop&lt;/a&gt;, I expect)&lt;/li&gt;&lt;li&gt;Strings are handled the same way in all code (no more converting between various string representations)&lt;/li&gt;&lt;li&gt;C# has anonymous inner functions with type inference (but the newest version of C++ has so-called "lambdas" too)&lt;/li&gt;&lt;li&gt;GUIs are easier to make in C# (at least if you use WinForms. I found WPF very hard to learn)&lt;/li&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx"&gt;LINQ&lt;/a&gt; (Language INtegrated Query)&lt;/li&gt;&lt;li&gt;"yield return" statement for writing generators and coroutines (&lt;a href="http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html"&gt;approximate C equivalent&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;You can create and compile code at run-time using Reflection.Emit, Dynamic Methods or (easiest) LambdaExpression.Compile. These use the JIT engine to produce new machine code at run-time.&lt;/li&gt;&lt;li&gt;The .NET standard libraries ("BCL") have more capabilities than those of C++, and the STL is more cumbersome (object-&gt;my_vector.erase(object-&gt;my_vector.begin() + index), anyone?)&lt;/li&gt;&lt;li&gt;Unlike C++ templates, C# generics are guaranteed to work for all type parameters, do not bloat your code size, and can be used by modules linked dynamically (mind you, C++ templates can do some things that are hard/impossible for generics, but advanced use of templates is difficult)&lt;/li&gt;&lt;li&gt;The MS C# compiler gives much better error messages than the MS C++ compiler&lt;/li&gt;&lt;li&gt;You can mix different programming languages much more easily in .NET. &lt;a href="http://www.wired.com/thisdayintech/2010/10/1014cplusplus-released/all/1"&gt;Bjarne Stroustrup said&lt;/a&gt;, "I consider the idea of one language, one programming tool, as the one and only best tool for everyone and for every problem infantile"--yet C++ isn't designed to inter-operate with any language other than C. You can use &lt;a href="http://swig.org/"&gt;SWIG&lt;/a&gt; if necessary, but it's got a big learning curve and C++ can't take credit for it anyway. On Windows, COM is a possible solution, but it's a huge pain to write COM classes in C++.&lt;/li&gt;&lt;li&gt;There are various tools for analyzing and modifying .NET assemblies/programs after they are compiled, e.g. PostSharp provides aspect-oriented programming and Microsoft Code Contracts let you specify preconditions, postconditions and invariants in your classes.&lt;/li&gt;&lt;li&gt;You can easily see how the standard libraries work in their binary form, using Reflector (the source code of the BCL is also available).&lt;/li&gt;&lt;li&gt;You can write "safe" code that can run directly in a web browser (Silverlight)&lt;/li&gt;&lt;li&gt;It is possible to write a C# program that targets a mobile device (ARM) and run the &lt;span style="font-style: italic;"&gt;same binary&lt;/span&gt; on your desktop PC&lt;/li&gt;&lt;li&gt;You'll no longer have to manage *.h files and write every function declaration twice. Well, you can save yourself work by leaving short"inline" functions in the header file, but you'll pay for it later with slower compile times.&lt;br /&gt;&lt;br /&gt;The need to use a different syntax for member functions in the header file than the implementation is a huge pet peeve of mine. Consider the difference between the header file declaration "virtual std::string Name(bool longForm = false);" and the cpp file equivalent "string ClassName::Name(bool longForm) { ... }". In C++ I'm expected to &lt;i&gt;manually&lt;/i&gt; remove "virtual", "= false" and the semicolon, but add "ClassName::". Plus you might want to remove "std::" if you're "using namespace std" in the cpp file. Doing all this a few dozen times in a day can drive me mad, and of course the two copies make maintenance harder too.&lt;/li&gt;&lt;li&gt;Dynamic linking and reflection make it easy to support plug-in architectures, and to use 3rd party libraries without compiling them yourself.&lt;/li&gt;&lt;li&gt;No more dependency-detection glitches where you change a struct or virtual function table but not all dependencies are recompiled, leading to bizarre and unpredictable run-time behavior. In C# I never see such glitches, and if they did happen you would get a run-time exception rather than weird crashes or strange behavior.&lt;/li&gt;&lt;li&gt;C# IDEs support automatic refactoring and Visual C# underlines syntax and semantic errors as soon as you make them. No such luck in C++!&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Unfortunately, I still have to use C++! It is still the performance king and the standard on Windows platforms, and I maintain a performance-critical project for WinCE, on which .NET performs poorly.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Some say C# is slower, but I find that if you write code carefully (more like you would in C), you can make C# almost as fast as C. The Microsoft .NET JIT compiler does not optimize nearly as well as a C compiler, but I find that C# code generally runs faster than a debug build of equivalent C/C++ code.&lt;br /&gt;&lt;br /&gt;Unfortunately, whereas C++ code may be 20 times slower when it runs on mobile (ARM-based) devices compared to a desktop PC, C# on the Compact Framework seems to be closer to 100 times slower than the same C# code running on a desktop PC. Microsoft needs to put a lot more work into their ARM version! Also note that Compact Framework has fewer features (e.g. no support for run-time code generation).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update&lt;/span&gt;: Long after writing this blog post I made a &lt;a href="http://www.codeproject.com/KB/cross-platform/BenchmarkCppVsDotNet.aspx"&gt;C++ vs C# benchmark&lt;/a&gt;. The conclusions were exactly as I expected/feared: C# is often almost as fast as C++ (but about half as fast in certain cases), while the Compact Framework is 3-11 times slower than C++.&lt;br /&gt;&lt;br /&gt;Now as much as I hate C++, and prefer C# over Java, the .NET Framework is far from perfect. Some parts of the BCL were badly designed, and by now it is getting to be extremely bloated. Also, I think the .NET framework needs some new features. Chief among my requests would be &lt;a href="http://golang.org/"&gt;Go&lt;/a&gt;-style slices and interfaces, and return type covariance (of course). This would bring a lot of C's big-fiddling prowess to C# without compromising type safety. You know, that deserves &lt;a href="http://loyc-etc.blogspot.com/2010/05/new-features-net-framework-should-have.html"&gt;its own blog entry&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-6571226677945802827?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/6571226677945802827/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=6571226677945802827' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6571226677945802827'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6571226677945802827'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/05/why-c-is-better-than-c.html' title='Why C# is better than C++'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-27320266193284279</id><published>2010-02-26T13:10:00.002-07:00</published><updated>2010-02-26T13:13:01.731-07:00</updated><title type='text'>The Compact Patricia Trie</title><content type='html'>I am not quite sure why I did it, but after learning about Judy, I had this obsession with bringing something comparable to .NET. I basically failed, so then instead I made a crazy data structure called a CPTrie. It's like a normal trie, but really compact! And much slower, I imagine. So I spent the last 3 days working on &lt;a href="http://www.codeproject.com/KB/recipes/cptrie.aspx"&gt;a CodeProject article about it&lt;/a&gt;. Enjoy! It is included in Loyc.Utilities.dll.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-27320266193284279?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://www.codeproject.com/KB/recipes/cptrie.aspx' title='The Compact Patricia Trie'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/27320266193284279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=27320266193284279' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/27320266193284279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/27320266193284279'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2010/02/compact-patricia-trie.html' title='The Compact Patricia Trie'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-5324579415808935630</id><published>2009-02-21T09:49:00.007-07:00</published><updated>2009-02-21T10:48:09.196-07:00</updated><title type='text'>Failure</title><content type='html'>Unfortunately, I have lost enthusiasm about Loyc. I just can't bring myself to do the incredibly large amount of work necessary to make a new compiler infrastructure. Besides that, I'm running into severe indecision trying to design the AST. I wish I could completely separate the implementation of the AST from its public interface, so that I could change it later if desired, but that's not entirely possible in C#.&lt;br /&gt;&lt;br /&gt;I'm sure that if I had supportive friends and another programmer that shares my vision, I could do it, but I am so very alone in this endeavor. If you are reading this article, please leave a comment, otherwise I'll have to assume that not one damn person read it.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://boo.codehaus.org/"&gt;Boo&lt;/a&gt; recently added some very cool metaprogramming features in v0.9, the kind of thing that I would have liked to put in Loyc. Also it sounds like eventually boo will move to an extensible (PEG-based) syntax, which will theoretically give it a lot of the power that I wanted to give Loyc (albeit boo will still only be powerful enough to compile boo code, not C#--a key feature of Loyc is supposed to be multi-language support). However, the boo developers are &lt;span style="font-style:italic;"&gt;terrible&lt;/span&gt; at documenting their language. I wonder how Rodrigo managed to find other developers to work on boo given his reluctance to communicate. Maybe it was that &lt;a href="http://docs.codehaus.org/display/BOO/Boo+Manifesto"&gt;boo manifesto&lt;/a&gt;--it certainly won &lt;span style="font-style:italic;"&gt;me&lt;/span&gt; over.&lt;br /&gt;&lt;br /&gt;In the &lt;a href="http://groups.google.com/group/boolang"&gt;boo google group&lt;/a&gt; they recently &lt;a href="http://groups.google.com/group/boolang/browse_thread/thread/474190081fa207c1#"&gt;called for people to write examples&lt;/a&gt; to showcase boo's new features, but they were unwilling to tell people how to actually USE the new features!&lt;br /&gt;&lt;br /&gt;I asked:&lt;br /&gt;&lt;blockquote&gt;Where is the documentation for the "macro" macro? Where is the documentation for using the AST and those cool [| AST expressions |] with $interpolation [...and...] where is the documentation for the AST classes? &lt;br /&gt;&lt;/blockquote&gt;No one responded.&lt;br /&gt;&lt;br /&gt;I wrote:&lt;br /&gt;&lt;blockquote&gt;I would like to write a macro in which you could write something&lt;br /&gt;like...&lt;br /&gt;&lt;pre&gt;x = 12&lt;br /&gt;y = 7.0&lt;br /&gt;z = "11"&lt;br /&gt;total = 0.0&lt;br /&gt;witheach Var in x, y, z:&lt;br /&gt;    total += Convert.ToDouble(Var)&lt;/pre&gt;&lt;br /&gt;and the macro would expand this to&lt;br /&gt;&lt;br /&gt;x = 12&lt;br /&gt;y = 7.0&lt;br /&gt;z = "11"&lt;br /&gt;total = 0.0&lt;br /&gt;total += Convert.ToDouble(x)&lt;br /&gt;total += Convert.ToDouble(y)&lt;br /&gt;total += Convert.ToDouble(z)&lt;br /&gt;&lt;br /&gt;But I don't know how to get started.&lt;/blockquote&gt;No one responded.&lt;br /&gt;&lt;br /&gt;I asked:&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;can macros have memory? I think it would be cool to&lt;br /&gt;have a pair of macros, let's call them "define" and "expand". define&lt;br /&gt;would be used something like this:&lt;br /&gt;&lt;pre&gt;define PointClass(P, T):&lt;br /&gt;   class P:&lt;br /&gt;      public constructor(x as T, y as T):&lt;br /&gt;          X=x; Y=y&lt;br /&gt;      public X as T&lt;br /&gt;      public Y as T&lt;br /&gt;      static def op_Addition(a as P, b as P):&lt;br /&gt;                return P(a.X+b.X, a.Y+b.Y)&lt;br /&gt;      static def op_Subtraction(a as P, b as P):&lt;br /&gt;                return P(a.X-b.X, a.Y-b.Y)&lt;br /&gt;      static def op_Multiply(a as P, b as P):&lt;br /&gt;                return a.X*b.X + a.Y*b.Y&lt;/pre&gt;&lt;br /&gt;and expand would be used like this to define three different kinds of&lt;br /&gt;points:&lt;br /&gt;&lt;pre&gt;expand PointClass(PointF, float)&lt;br /&gt;expand PointClass(PointD, double)&lt;br /&gt;expand PointClass(PointI, int)&lt;/pre&gt;&lt;br /&gt;Is this even possible with the current macro architecture? &lt;br /&gt;&lt;/blockquote&gt;No one responded.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;A brief history of Loyc&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I actually wrote a complete unit-inference engine for boo around two years ago, including small changes to the parser, so that you could write, for example,&lt;br /&gt;&lt;pre&gt;_weight as double&lt;br /&gt;def GetAcceleration(force as double`N`) `m/s^2`:&lt;br /&gt;   return force/_weight&lt;/pre&gt;&lt;br /&gt;And the boo compiler would automatically determine that _weight is measured in kilograms. Or, you could specify instead that _weight is `kg` and the engine would automatically infer that GetAcceleration returns `m/s^2`. In fact it was not even necessary to specify any units on the GetAcceleration method; it was sufficient to include units in any call to the method:&lt;br /&gt;&lt;pre&gt;_weight as double`kg`&lt;br /&gt;def GetAcceleration(force as double):&lt;br /&gt;   return force/_weight&lt;br /&gt;def Foo():&lt;br /&gt;   _weight = 3&lt;br /&gt;   a = GetAcceleration(2`N`)&lt;br /&gt;   x = 3&lt;br /&gt;   a2 = GetAcceleration(x)&lt;/pre&gt;&lt;br /&gt;There are only two unit annotations in this code, but the engine has already inferred that the local variable x has units of `kg m/s^2`, i.e. newtons, while a and a2 have units of `m/s^2`.&lt;br /&gt;&lt;br /&gt;If you later wrote code that contradicted the units that had been inferred, the compiler would give you a warning. I was very happy with my work and looked forward to using units in my everyday boo programming. I don't always use many physical units like kilograms and metres in my code, mind you, but I would certainly use a lot of other units like bits, bytes, dwords, records, pixels and percentages.&lt;br /&gt;&lt;br /&gt;Unfortunately, as my engine required a parser change, it could not be used with standard boo unless boo's author accepted a parser patch. Unfortunately, when I announced my completed work on the boo group, there seemed to be absolutely no interest in it, and boo's author, Rodrigo B. de Oliveira, never even commented on it.&lt;br /&gt;&lt;br /&gt;That's when I earnestly began to work on Loyc. I decided that the set of features a language supports should not be under the control of a single person (boo), corporation (C#), or committee (C++). Instead, I felt, a compiler should exist that allowed anybody to add new features. Soon I came up with a name for this idea: Loyc, or Language of Your Choice, because the my compiler would support multiple languages and it the user could choose what the language would support or prohibit.&lt;br /&gt;&lt;br /&gt;But without even a single other person rallying behind my cause, I feel at this point it has been a failure. I would certainly consider working on boo instead, except that boo's developers don't seem interested in nurturing their community by helping people use boo. When boo finally has some half-decent documentation, I may start using it again. Hell, I'd write the documentation myself if I had any clue how to use boo's advanced features, but I don't, so I won't.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-5324579415808935630?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/5324579415808935630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=5324579415808935630' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5324579415808935630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5324579415808935630'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2009/02/failure.html' title='Failure'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-6686220478915362215</id><published>2008-11-21T09:40:00.000-07:00</published><updated>2008-11-21T09:42:12.603-07:00</updated><title type='text'>Using ICSharpCode.TextEditor</title><content type='html'>I wrote an article about &lt;a href="http://www.codeproject.com/KB/edit/TextEditorControl.aspx"&gt;using SharpDevelop's ICSharpCode.TextEditor&lt;/a&gt; on CodeProject.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-6686220478915362215?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://www.codeproject.com/KB/edit/TextEditorControl.aspx' title='Using ICSharpCode.TextEditor'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/6686220478915362215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=6686220478915362215' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6686220478915362215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6686220478915362215'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/11/using-icsharpcodetexteditor.html' title='Using ICSharpCode.TextEditor'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-138099351454177045</id><published>2008-10-03T10:35:00.005-06:00</published><updated>2008-12-31T12:27:44.952-07:00</updated><title type='text'>Visual Studio dialogs are modal—for OpenOffice</title><content type='html'>This is really irritating. I use Visual Studio 2008 and OpenOffice 2.2.0 Writer at the same time at work. When Visual Studio shows certain modal dialogs, such as a wizard for a new project, or dialogs of the SourceSafe plugin, OpenOffice freezes up completely. It won't even redraw itself, let alone respond to mouse clicks.&lt;br /&gt;&lt;br /&gt;Update: I thought Visual Studio was unaffected by this quirk until I started OpenOffice.org Writer at the same time as a SourceSafe "differences" dialog was already open. A message box appeared saying "Unable to complete operation" and then Visual Studio crashed (disappeared instantly). Hmm.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-138099351454177045?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/138099351454177045/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=138099351454177045' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/138099351454177045'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/138099351454177045'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/10/visual-studio-dialogs-are-modalfor.html' title='Visual Studio dialogs are modal—for OpenOffice'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-1871564897813597349</id><published>2008-09-26T13:46:00.007-06:00</published><updated>2008-09-30T13:47:58.825-06:00</updated><title type='text'>Bitstream Vera Sans Mono Bold</title><content type='html'>My favorite font for programming. Use a black background for vibrant colors.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_y8ybb5S16Uc/SOKCX3_m98I/AAAAAAAAAAw/XvWC54JVzaM/s1600-h/BitstreamVeraSansMono.png"&gt;&lt;img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_y8ybb5S16Uc/SOKCX3_m98I/AAAAAAAAAAw/XvWC54JVzaM/s400/BitstreamVeraSansMono.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5251903462146766786" /&gt;&lt;/a&gt;&lt;br /&gt;In this color scheme I use an off-white (not full intensity) rather than pure white, otherwise the white text seems brighter than everything else. Interestingly, this font is typically bundled with Linux, but IMO it looks significantly better in Windows.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-1871564897813597349?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/1871564897813597349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=1871564897813597349' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/1871564897813597349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/1871564897813597349'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/09/bitstream-vera-sans-mono-bold.html' title='Bitstream Vera Sans Mono Bold'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_y8ybb5S16Uc/SOKCX3_m98I/AAAAAAAAAAw/XvWC54JVzaM/s72-c/BitstreamVeraSansMono.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-2138026202380307050</id><published>2008-09-12T09:45:00.005-06:00</published><updated>2008-09-12T10:23:10.277-06:00</updated><title type='text'>Symbols in .NET</title><content type='html'>I'm a big fan of Ruby's "symbols". Symbols are sort of like strings or enums, but different. Their syntax is an identifier with a colon in front, e.g. :Foo. &lt;a href="http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol"&gt;See here&lt;/a&gt; for details.&lt;br /&gt;&lt;br /&gt;I love using symbols in place of enums, because if they are implemented properly, comparing two symbols is as fast as comparing two integers (enums). Enums have the problem of non-extensibility; library B can't define new values for an enum in library A. Meanwhile, anybody can define a new symbol at any time.&lt;br /&gt;&lt;br /&gt;Via Loyc I would like to add symbol support to C# and boo, but Loyc is a long way off as long as I have nobody to help me. In the meantime, &lt;a href="http://loyc.svn.sourceforge.net/viewvc/loyc/Src/Runtime/Symbol.cs?view=markup"&gt;see here&lt;/a&gt; for my current implementation of Symbols in C#.&lt;br /&gt;&lt;br /&gt;To simulate enums using Symbols in C#, I just define a static class full of Symbols. For example:&lt;br /&gt;&lt;pre&gt;public static class Tokens {&lt;br /&gt;  static public readonly Symbol WS = Symbol.Get("WS"); // whitespace&lt;br /&gt;  static public readonly Symbol NEWLINE = Symbol.Get("NEWLINE");&lt;br /&gt;  static public readonly Symbol ID = Symbol.Get("ID"); // identifier&lt;br /&gt;  static public readonly Symbol PUNC = Symbol.Get("PUNC");&lt;br /&gt;  static public readonly Symbol EOS = Symbol.Get("EOS");&lt;br /&gt;  static public readonly Symbol ML_COMMENT = Symbol.Get("ML_COMMENT");&lt;br /&gt;  static public readonly Symbol SL_COMMENT = Symbol.Get("SL_COMMENT");&lt;br /&gt;  ...&lt;br /&gt;}&lt;/pre&gt;Enjoy!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-2138026202380307050?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://loyc.svn.sourceforge.net/viewvc/loyc/Src/Runtime/Symbol.cs?view=markup' title='Symbols in .NET'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/2138026202380307050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=2138026202380307050' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2138026202380307050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2138026202380307050'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/09/symbols-in-net.html' title='Symbols in .NET'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-7532171754277514625</id><published>2008-09-12T01:13:00.002-06:00</published><updated>2008-09-12T01:18:01.543-06:00</updated><title type='text'>Simulating covariant return types in C#</title><content type='html'>For several years, Microsoft engineers have refused to add support for &lt;a href="http://en.wikipedia.org/wiki/Covariant_return_type"&gt;covariant return types&lt;/a&gt;, a trivially simple feature that should have been in the CLR from the beginning.&lt;br /&gt;&lt;br /&gt;Suppose you want to write a Clone() method that returns a copy of the current object. Naturally you want to write the following, but it is illegal:&lt;br /&gt;&lt;pre&gt;class MyStuff : ICloneable {&lt;br /&gt;    public MyStuff Clone() { ... }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Since you are implementing an interface, you can use this workaround that uses explicit interface implementation:&lt;br /&gt;&lt;pre&gt;class MyStuff : ICloneable {&lt;br /&gt;    public MyStuff Clone() { ... }&lt;br /&gt;    object ICloneable.Clone() { return Clone(); }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The above workaround is okay for implementing an interface, but what if you are writing a class hierarchy, and you want a Clone() method that is virtual but has the appropriate return type?&lt;br /&gt;&lt;pre&gt;class BaseNode : ICloneable&lt;br /&gt;{&lt;br /&gt;    object ICloneable.Clone() { return Clone(); }&lt;br /&gt;    public virtual BaseNode Clone() { ... }&lt;br /&gt;}&lt;br /&gt;class ComplexNode : BaseNode&lt;br /&gt;{&lt;br /&gt;    override BaseNode BaseNode.Clone() { return Clone(); } // Error!&lt;br /&gt;    public ComplexNode Clone() { ... }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Oops, the workaround that you use for interfaces is illegal for class inheritance. There is still a solution, though:&lt;br /&gt;&lt;pre&gt;class BaseNode : ICloneable&lt;br /&gt;{&lt;br /&gt;    object ICloneable.Clone() { return Clone(); }&lt;br /&gt;    public BaseNode Clone() { BaseNode c; Clone(out c); return c; }&lt;br /&gt;    protected virtual void Clone(out BaseNode clone) { ... }&lt;br /&gt;}&lt;br /&gt;class ComplexNode : BaseNode&lt;br /&gt;{&lt;br /&gt;    public new ComplexNode Clone() { ComplexNode c; Clone(out c); return c; }&lt;br /&gt;    protected override void Clone(out BaseNode clone) { clone = Clone(); }&lt;br /&gt;    protected virtual void Clone(out ComplexNode clone) { ... }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;That's right. You need six Clone() methods. The last method is virtual in case you want to make a class derived from ComplexNode, e.g. VeryComplexNode:&lt;br /&gt;&lt;pre&gt;class VeryComplexNode : ComplexNode&lt;br /&gt;{&lt;br /&gt;    public new VeryComplexNode Clone() { VeryComplexNode c; Clone(out c); return c; }&lt;br /&gt;    protected override void Clone(out BaseNode clone) { clone = Clone(); }&lt;br /&gt;    protected override void Clone(out ComplexNode clone) { clone = Clone(); }&lt;br /&gt;    protected virtual void Clone(out VeryComplexNode clone) { ... }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Without covariant return types, you have to to define an additional virtual function for each additional derived class.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-7532171754277514625?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909' title='Simulating covariant return types in C#'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/7532171754277514625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=7532171754277514625' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7532171754277514625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7532171754277514625'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/09/simulating-covariant-return-types-in-c.html' title='Simulating covariant return types in C#'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-7830963132974288920</id><published>2008-06-01T15:47:00.002-06:00</published><updated>2008-06-01T16:28:54.824-06:00</updated><title type='text'>How to get started with Subversion on Sourceforge</title><content type='html'>To get started with &lt;a href="http://tortoisesvn.net/"&gt;TortoiseSvn&lt;/a&gt; as the admin of a new project:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;On your SourceForge project page, choose "Subversion" from the "Admin" menu.&lt;/li&gt;&lt;li&gt;Check the box beside "The following box should be checked to enable Subversion:" and click Update. (Note: you may also want to disable CVS from the Admin | CVS page).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Prepare the folder that has your source code in it by removing binary files that are generated by your compiler (e.g. remove "bin", "Debug", "Release" folders), so you are only left with files you &lt;span style="font-weight: bold;"&gt;want&lt;/span&gt; to put in the repository.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Install TortoiseSvn if you haven't already.&lt;/li&gt;&lt;li&gt;Assuming your root folder is called "MyProject" and the "unix name" of your project on SourceForge is "myproject", right-click on the &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder (on your hard drive) and choose TortoiseSVN | Import...&lt;br /&gt;&lt;/li&gt;&lt;li&gt;As &lt;a href="http://alexandria.wiki.sourceforge.net/Subversion+-+Version+Control+for+Source+Code"&gt;documented here&lt;/a&gt;, use https://&lt;span style="font-style: italic;"&gt;myproject&lt;/span&gt;.svn.sourceforge.net/svnroot/&lt;span style="font-style: italic;"&gt;myproject&lt;/span&gt; as the URL of the repository. Then click OK and your files will be uploaded.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Unfortunately, the &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder is still not associated with the repository; you are required to "check out" the files, which means downloading them, in order to complete the association between the files on your computer and the repository. To accomplish this, first rename the &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder to &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt;2--or just delete it, as you probably don't need it anymore. Then make a new &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder, right-click on it and choose "SVN Checkout...". Use the same repository URL as before: https://&lt;span style="font-style: italic;"&gt;myproject&lt;/span&gt;.svn.sourceforge.net/svnroot/&lt;span style="font-style: italic;"&gt;myproject&lt;/span&gt;. Click OK and the files are downloaded.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Now when you recompile your project, the object files and stuff will come back (e.g. in "Debug", "Release" folders). You can ensure that TortoiseSVN won't upload a file/folder by right-clicking on the file/folder and choosing TortoiseSVN | Add to ignore list | &lt;span style="font-style: italic;"&gt;filename&lt;/span&gt;.&lt;br /&gt; &lt;/li&gt;&lt;li&gt;After making changes to your program, right click the &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder and choose "SVN Commit..." to upload the changes. TortoiseSVN will update existing files in the repository automatically, but you need to check the check box beside new ("unversioned") files. Then click OK.&lt;/li&gt;&lt;li&gt;When sharing a repository with other people, you must frequently right click the &lt;span style="font-style: italic;"&gt;MyProject&lt;/span&gt; folder and choose "SVN Update" to download changes made by others. &lt;/li&gt;&lt;li&gt;To learn more about Subversion and TortoiseSVN, read the &lt;span style="font-weight: bold;"&gt;truly excellent&lt;/span&gt; TortoiseSVN manual, by right-clicking on &lt;span style="font-weight: bold;"&gt;any&lt;/span&gt; file and choosing TortoiseSVN | Help.&lt;/li&gt;&lt;li&gt;When inviting other developers to your project, point them to &lt;a href="http://haacked.com/archive/2006/02/22/QuickstartGuidetoSubversiononSourceForge.aspx"&gt;this other handy tutorial&lt;/a&gt;.&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-7830963132974288920?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/7830963132974288920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=7830963132974288920' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7830963132974288920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7830963132974288920'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/06/how-to-get-started-with-subversion-on.html' title='How to get started with Subversion on Sourceforge'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-9076658951005878570</id><published>2008-05-18T17:22:00.007-06:00</published><updated>2008-05-18T17:48:29.852-06:00</updated><title type='text'>VList data structure in C# (.NET Framework 2.0)</title><content type='html'>I've made an implementation of Phil Bagwell's VList data structure in C#, with a fairly comprehensive test suite. It comes in two flavors: VList(of T), where you normally add/remove items at the beginning of the list, and RVList(of T), to which you normally add/remove items at the end. It implements the complete IList(of T) interface plus quite a few additional members including AddRange, InsertRange, RemoveRange, Push, and Pop. Converting a VList to a RVList and vice versa is a trivial O(1) operation that appears to reverse the order of the elements. VList and RVList are value types (structs) that contain a reference to the underlying linked list of arrays (VListBlock(of T)). Small lists (0 to 2 items) are optimized with a specialized block class (VListBlockOfTwo(of T)).&lt;br /&gt;&lt;br /&gt;License: Lesser GPL. Contact me at qwertie256, at, gmail.com if you would like the source code. Here's an example usage:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;void Example()&lt;br /&gt;{&lt;br /&gt; VList&amp;lt;int&amp;gt; oneTwo = new VList&amp;lt;int&amp;gt;(1, 2);&lt;br /&gt; VList&amp;lt;int&amp;gt; threeFour = new VList&amp;lt;int&amp;gt;(3, 4);&lt;br /&gt; VList&amp;lt;int&amp;gt; list = oneTwo;&lt;br /&gt; VList&amp;lt;int&amp;gt; list2 = threeFour;&lt;br /&gt;&lt;br /&gt; ExpectList(list, 1, 2);&lt;br /&gt; list.InsertRange(1, threeFour);&lt;br /&gt; ExpectList(list, 1, 3, 4, 2);&lt;br /&gt; list2.InsertRange(2, oneTwo);&lt;br /&gt; ExpectList(list2, 3, 4, 1, 2);&lt;br /&gt;&lt;br /&gt; // oneTwo and ThreeFour are unchanged:&lt;br /&gt; ExpectList(oneTwo, 1, 2);&lt;br /&gt; ExpectList(threeFour, 3, 4);&lt;br /&gt;}&lt;br /&gt;static void ExpectList&amp;lt;T&amp;gt;(IList&amp;lt;T&amp;gt; list, params T[] expected)&lt;br /&gt;{&lt;br /&gt; Assert.AreEqual(expected.Length, list.Count);&lt;br /&gt; for (int i = 0; i &lt; expected.Length; i++)&lt;br /&gt;  Assert.AreEqual(expected[i], list[i]);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I thought that I would use the RVList to implement Loyc's AST to help make it possible to take AST snapshots easily, but I now suspect it's not a good idea. I am still working on the problem.&lt;br /&gt;&lt;h2&gt;Performance characteristics&lt;/h2&gt;Similarly to a persistent linked list, &lt;ul&gt;&lt;li&gt;Adding an item to the front of a VList or the end of an RVList is always O(1) in time, and often O(1) in space (though, unlike a linked list, it may be much more)&lt;/li&gt;&lt;li&gt;Removing an item from the front of a VList or the end of an RVList is  O(1) in time, although space not necessarily reclaimed.&lt;/li&gt; &lt;li&gt;Adding or removing an item at the end of a VList or the front of an  RVList is O(N) and requires making a copy of the entire list.&lt;/li&gt; &lt;li&gt;Inserting or removing a list of M items at the end of a VList or the  front of an RVList is O(N + M).&lt;/li&gt; &lt;li&gt;Changing an item at an arbitrary position should be avoided, as it performs as poorly as inserting or removing an item at that position.&lt;/li&gt; &lt;/ul&gt; VLists, however, offer some operations that singly-linked lists cannot  provide efficiently: &lt;ul&gt; &lt;li&gt;Access by index averages O(1) in ideal conditions&lt;/li&gt; &lt;li&gt;Getting the list length is typically O(log N), but O(1) in my version&lt;/li&gt; &lt;li&gt;If a sublist points somewhere within a larger list, its index within the larger list can be obtained in between O(1) and O(log N) time. Consequently, reverse enumeration is possible without creating a  temporary stack or list.&lt;/li&gt; &lt;/ul&gt; Also, VLists can (in the best cases) store data almost as compactly as ordinary arrays.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-9076658951005878570?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='related' href='http://en.wikipedia.org/wiki/VList' title='VList data structure in C# (.NET Framework 2.0)'/><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/9076658951005878570/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=9076658951005878570' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/9076658951005878570'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/9076658951005878570'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2008/05/vlist-data-structure-in-c-net-framework.html' title='VList data structure in C# (.NET Framework 2.0)'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-6013581662563416106</id><published>2007-09-03T11:17:00.000-06:00</published><updated>2007-09-03T15:56:12.945-06:00</updated><title type='text'>Loyc design issues</title><content type='html'>It's been fun designing Loyc, but boy, I've got a lot left to think about.&lt;br /&gt;&lt;br /&gt;Right now I'm trying to figure out how to allow extensions to activate and deactivate statements based on arbitrary contextual criteria. One unanswered question is whether statements should have access to their parent node (ICodeNode) during parsing. The main problem with allowing it is that the parent nodes are, in general, not yet fully parsed when the child nodes are parsed, and it may be tricky to design convenient, reasonable, non-cumbersome semantics for the incomplete parent nodes. I'm leaning toward requiring only that the &lt;span style="font-style: italic;"&gt;type&lt;/span&gt; Symbol of parent nodes be made available. Probably some other kind of context than the parent node ought to be available, such as symbol tables. In some languages, notably C++, symbol tables are considered necessary for correct parsing, although there are usually ways around such problems; for example I think &lt;a href="http://www.computing.surrey.ac.uk/research/dsrg/fog/"&gt;FOG&lt;/a&gt; can parse C++ without them. Still, even if symbol tables aren't needed to parse, it often makes sense to &lt;span style="font-style: italic;"&gt;build&lt;/span&gt; symbol tables during parsing. But in Loyc I also want to separate concerns as much as possible in order to maximize code re-use. By separating out the code for building symbol tables,&lt;br /&gt;&lt;ol&gt;&lt;li&gt;it should be easier to add artificial (aka synthetic) nodes to symbol tables&lt;/li&gt;&lt;li&gt;people can parse code without building symbol tables, which is nice if, for whatever reason, the symbol tables are not needed.&lt;/li&gt;&lt;/ol&gt;But I digress. There's lots of unresolved issues and I'd just like to summarize the ones I can think of...&lt;br /&gt;&lt;ul&gt;&lt;li&gt;There may be a lot of statements allowed from a lot of different extensions, perhaps hundreds, and the set of available statements may vary with every new block that opens. I'm planning to give statements full control over parsing their contents, including nested statements, but there will be a conventional way that statements can give control back to the language style. So the questions are&lt;/li&gt;&lt;ul&gt;&lt;li&gt;How to efficiently modify the set of available statements (I decree the &lt;a href="http://en.wikipedia.org/wiki/Split_infinitive"&gt;split infinitive&lt;/a&gt; to be perfect English :P).&lt;/li&gt;&lt;li&gt;How to allow statements to specify when they are available. Arbitrary criteria should be possible but the most common case(s) should be easy for the user (i.e. extension writer) to use and should perform well. Or maybe the problem should be reconsidered as follows: how can block statements (that contain other statements) specify what categories of other statements they can contain?&lt;br /&gt;&lt;/li&gt;&lt;li&gt;How to provide the language style with enough control over how parsing operates that the original language spec can be supported under the Loyc extensible parsing model.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Similar concerns apply to operators. There may be hundreds of operators available in a program, but not all at once. Availability may be moderated by the parent statements and parent expressions.&lt;/li&gt;&lt;li&gt;Note to self: I need to introduce a new kind of OneOperatorPart that represents the edge of the expression. This would be a prerequisite to custom-syntax function calls such as Line(from x, y to x+10, y+10).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;What kind of context information should be available during statement and expression parsing? Certainly the type Symbol of parent and grandparent nodes... but some statements may only be available if a certain custom attribute was used on the statement or a parent statement, so I think the set of attributes for parent/grandparents should be available too. And maybe availability based on attributes should be a standard feature, a criterion upon which Loyc activates/deactivates the statements automatically. But as I've said, providing the parent ICodeNode seems like too much to ask. I suppose it could be provided optionally.&lt;/li&gt;&lt;li&gt;As I mentioned above, there are two ways to look at how statements are allowed to be nested inside other statements. You can either have the substatements specify what they can be located inside of; or, the parent statement can say what kind of substatements it can contain. Should Loyc support both approaches?&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Now consider this. Suppose somebody writes an "unless-else statement" extension:&lt;br /&gt;&lt;pre&gt;unless (x &lt; 0)&lt;br /&gt;  return new StringBuilder(x);&lt;br /&gt;else&lt;br /&gt;   return new StringBuilder(); &lt;/pre&gt;and somebody else writes an extension for "macro methods" which can be "instantiated" as normal methods:&lt;br /&gt;&lt;pre&gt;macro(T) T Abs(T x) {&lt;br /&gt;  unless (x &amp;lt; 0) return x;&lt;br /&gt;  else return -x;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;instantiate(int) Abs; // create method int Abs(int)&lt;br /&gt;instantiate(long) Abs; // create method long Abs(long)&lt;br /&gt;instantiate(float) Abs; // create method float Abs(float)&lt;br /&gt;instantiate(double) Abs; // create method double Abs(double)&lt;br /&gt;&lt;/pre&gt;You can see that the macro method statement should be able to parse all statements that belong inside a method, and the "unless" statement should be allowed in the same places an "if" statement is allowed. You can see that if the "macro method" had to specify explicitly the kind of statements it supports, or if the "unless" statement had to specify explicitly the allowable parent nodes, then there is no way the two extensions could work together if neither author knew about the other's extension.&lt;br /&gt;&lt;br /&gt;Therefore, I think statements should grouped by "category", where categories are classes of statements like "method body statements", "class body statements", "loop statements", "block statements", "conditional statements", etc. I suspect categories will be important for extensibility because they can allow statements to work together that are not aware of each other.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-6013581662563416106?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/6013581662563416106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=6013581662563416106' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6013581662563416106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/6013581662563416106'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/09/loyc-design-issues.html' title='Loyc design issues'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-4558024448537406999</id><published>2007-09-03T10:48:00.000-06:00</published><updated>2007-09-03T11:17:09.987-06:00</updated><title type='text'>Hello, no one!</title><content type='html'>At this point I have no readership and no one has the foggiest idea what Loyc is, except maybe my non-programmer best friend, Ivan. (In case some random guy reads this, Loyc is the Language of Your Choice, a multi-language compiler that will allow &lt;span style="font-style: italic;"&gt;anybody&lt;/span&gt; to add new features to existing programming languages.)&lt;br /&gt;&lt;br /&gt;Before I try to get anybody on board the project, I need to work out enough design issues to present a reasonable description of its design. I've got some design documents written but not posted on the web yet--I need to re-learn how to upload to sourceforge.net, and the usability of their services is truly awful, at least if one is not a Unix guy. For now I should probably make pages on my local crappy WEBrick server. Then I won't have to upload anything, just move stuff to another folder on my hard drive.&lt;br /&gt;&lt;br /&gt;For now you can read the incomplete doc about my &lt;a href="http://qwertie.net/loyc/onep.html"&gt;extensible expression parser called ONEP&lt;/a&gt;. If anyone clicks on that link I'll be shocked, shocked! I'm excited to announce (to my zero readers) that the C# code for BasicOneParser is complete and you can post a message if you want a copy.&lt;br /&gt;&lt;br /&gt;Anyway, I bought the domain name loyc.net a few months ago; in fact that's how I chose the name of the project: domains for most acronyms are already taken.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-4558024448537406999?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/4558024448537406999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=4558024448537406999' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/4558024448537406999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/4558024448537406999'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/09/hello-no-one.html' title='Hello, no one!'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-2575350135167515810</id><published>2007-08-11T15:16:00.000-06:00</published><updated>2007-08-11T15:24:09.701-06:00</updated><title type='text'>Microsoft sure knows how to foil search engines</title><content type='html'>I have been trying to learn COM and .NET development lately, but every time I try to search for COM and .NET related stuff on Google, it seems like I get any website that has .com or .net in the domain name. Plus, search engines are harly able to tell the difference between C, C++, C# and pages that are indexed by their first letter. For .NET I've got around this problem by searching for ".NET framework" or CLR, but for COM there seems to be no way to find information about it.&lt;br /&gt;&lt;br /&gt;What TLA will they come up with next? THE? FOO?&lt;br /&gt;&lt;br /&gt;P.S. My 80-gig secondary hard drive has died. It's only a matter of time before my 200-gig explodes...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-2575350135167515810?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/2575350135167515810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=2575350135167515810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2575350135167515810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/2575350135167515810'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/08/microsoft-sure-knows-how-to-foil-search.html' title='Microsoft sure knows how to foil search engines'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-8004687733079349744</id><published>2007-08-07T09:21:00.000-06:00</published><updated>2007-08-07T09:24:54.366-06:00</updated><title type='text'>Microsoft C++ doing what Loyc is doing?</title><content type='html'>I came across &lt;a href="http://msdn2.microsoft.com/en-us/library/w3yk4x9b(VS.80).aspx"&gt;this MSDN article&lt;/a&gt; today, which says &lt;br /&gt;&lt;blockquote&gt;...when the project is built, the compiler parses each C++ source file, producing an object file. However, when the compiler encounters an attribute, it is parsed and syntactically verified. The compiler then dynamically calls an attribute provider to insert code or make other modifications at compile time. The implementation of the provider differs depending on the type of attribute. For example, ATL-related attributes are implemented by Atlprov.dll.&lt;/blockquote&gt;&lt;br /&gt;So, boo and Loyc aren't the only compilers doing binary-compatible compiler extensions. I wonder who else is doing it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-8004687733079349744?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/8004687733079349744/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=8004687733079349744' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/8004687733079349744'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/8004687733079349744'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/08/microsoft-c-doing-what-loyc-is-doing.html' title='Microsoft C++ doing what Loyc is doing?'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-5355742355261477205</id><published>2007-07-18T12:13:00.000-06:00</published><updated>2007-07-18T14:51:55.505-06:00</updated><title type='text'>C++/CLI is disgusting</title><content type='html'>Microsoft has found a truly awful set of syntax and semantics for their new C++/CLI language, formerly known as "Managed Extensions for C++". They decided that the old syntax was ugly because it used keywords that began with double underscores (which is a standard way to add compiler extensions in C++). Unfortunately, their solution was much worse than the problem they were trying to solve.&lt;br /&gt;&lt;br /&gt;I had been using the first Managed C++ for a little while, but luckily I only made a single module in it (wrapper classes to allow C# to access some C++ classes). After a couple years I wanted to add a dialog box that accessed the C++ classes directly, but the forms designer only supported the "new" syntax; worse, Microsoft requires that the entire project only use one syntax or the other. So I learned the awfulness of the new design as I laboriously converted each line of the old code to the new syntax; the new syntax is so different that virtually every line of the module's header file had to be changed. And not just slightly. In many cases it was faster to retype the line than to try to adjust it. And they didn't just make new syntax, they invented new problematic semantics as well.&lt;br /&gt;&lt;br /&gt;The &lt;a href="http://msdn2.microsoft.com/en-us/library/ms235298%28VS.80%29.aspx"&gt;changes&lt;/a&gt; include&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The new "handles". A pointer to a managed class used to be called MyClass*, now it's MyClass^. Other than that they are still used like pointers (i.e. with the arrow notation).&lt;/li&gt;&lt;li&gt;"Tracking references". Instead of writing String^&amp; and Int32&amp;amp; like you would expect, you have to write String^% and Int32%.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;nullptr. Whereas you used to be able to initialize all pointers to NULL, including managed pointers, now you have to remember if it's a managed class and use "nullptr" if so.&lt;/li&gt;&lt;li&gt;Same with 'new'; now you have to write gcnew if the class is managed.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;New finalizer syntax. Confusingly, whereas C# and Managed C++ use "~ClassName" for finalizers, Microsoft decided it was too predictable and renamed it to "!ClassName". Worse, they now require your Dispose() function to be called ~ClassName(), which causes a silent semantics change in old code. Or it would, except that you'll know something's up because your Dispose() method yields this odd error: "'Dispose' : this method is reserved within a managed class".&lt;br /&gt;&lt;/li&gt;&lt;li&gt;You can no longer use a managed enum like you do a normal enum; you have to qualify the names with "EnumName::EnumValue". This makes it impossible to share an enum between C# and standard C++ code, so you have to create a second enum (with the same items) and convert between them all the time.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;In a managed class you &lt;span style="font-style: italic;"&gt;must&lt;/span&gt; say if you're overriding a base class function or not, or you'll get a compiler error--whereas in pure standard C++ you &lt;span style="font-style: italic;"&gt;can't&lt;/span&gt;. Argh! Even C# lets you off with a warning. And what a bizarre syntax they've picked too; instead of grouping the "override" keyword with "static", "virtual", etc., they make you put it at the end: virtual void foo() override {}. What's more, you have to specify &lt;span style="font-style: italic;"&gt;both&lt;/span&gt; virtual and override.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Similarly, "sealed" and "abstract" go after the class name.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;When making managed properties, you now have to group the setter with the getter in a single construct like in C#, but unlike in C#, you also have to repeat the data type three times (or twice if it's just a getter). How many times do you want to type Dictionary&amp;lt;string,SomeFreakyLongClassName&amp;gt;?&lt;/li&gt;&lt;li&gt;CLR enums are no longer implicitly convertable to arithmetic types.&lt;/li&gt;&lt;li&gt;They've switched to the standard 'typeid' syntax instead of __typeof(MyManagedType). Oh wait, no they haven't! The syntax is randomly different: MyManagedType::typeid versus typeid(UnmanagedType).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;What the hell were they thinking here?&lt;br /&gt;virtual Object^ InterfaceClone() = ICloneable::Clone;&lt;br /&gt;&lt;br /&gt;The old syntax for 'explicit interface implementation' made much more sense:&lt;br /&gt;Object* ICloneable::Clone();&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Tell me, how is it that when C# is supposedly modeled after C++, the C++ version of all these .NET features ends up looking so much &lt;s&gt;longer&lt;/s&gt; different than C#?&lt;br /&gt;&lt;br /&gt;Admittedly, there are a few things that don't suck, like&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the support for normal C++-style operator overloading in managed classes.&lt;/li&gt;&lt;li&gt;implicit boxing (although if NULL is defined as 0, watch out for boxed zeros when converting old code)&lt;/li&gt;&lt;li&gt;default indexers (much like in C#)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;trivial properties (but they're inflexible and so not usable in many scenarios)&lt;/li&gt;&lt;/ul&gt;And now some managed-style features work in unmanaged classes, such as properties. Personally I have no use for this. After all, using such features means you can't compile your unmanaged class in a non-.NET program, so their utility is limited. If I want to write a class that &lt;span style="font-weight: bold;"&gt;only&lt;/span&gt; works in .NET, I would almost always make it a "ref class" or "ref struct" so I can interoperate with other .NET languages.&lt;br /&gt;&lt;br /&gt;There are two main problems I see with their design.&lt;br /&gt;&lt;br /&gt;The first big problem is that they've forgotten the spirit of C++ and discarded longstanding rules of C++ such as implicit overriding. C++'s philosophy has long been that an object should be able to behave like a pointer, like a number, like a function. Smart pointers, iterators, fixed-point/matrix classes/bigints, functors. The ability of one thing to act like something else is the whole basis for the STL. But in Microsoft's new design, everything managed is completely segregated so you can no longer write code that &lt;span style="font-style: italic;"&gt;doesn't care&lt;/span&gt; whether something is managed or not. It's not just reference types either; value types and even simple enums are segregated to an extent that they weren't before. You always have to think: Do I have to Qualify:: that enum or not? should I use gcnew or new here? NULL or nullptr? * or ^? &amp; or %? I can only use one or the other in a given context, but the wretched compiler still makes me tell it what it wants to hear. Template code that before could have (theoretically) taken managed or unmanaged classes for arguments can now take only one or the other, because a separate syntax is needed for each.&lt;br /&gt;&lt;br /&gt;The second big problem is that there is no longer anything I can share between C# and standard C++. I have a library that needs to be compiled into both C# programs and MFC programs (which must be Windows CE compatible, so mixing .NET and MFC is not an option). With the old syntax it was possible to share a small number of value types and enums between plain C++ and managed C++ (with the help of some #define macros); now I have to make two versions and convert between them.&lt;br /&gt;&lt;br /&gt;If anything, Microsoft should have made the managed syntax more like standard C++, not less. It should have considered how to allow people to write classes that could be used directly from C# &lt;span style="font-style: italic;"&gt;or&lt;/span&gt; (in another program) directly from standard C++. This would have made a much better bridge between unmanaged land and managed land. As it is, Microsoft has imposed a kind of syntax apartheid.&lt;br /&gt;&lt;br /&gt;Bottom line: I &lt;span style="font-style: italic;"&gt;loathe&lt;/span&gt; the new syntax. It makes me long for the hellish landscape of double underscores again.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-5355742355261477205?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/5355742355261477205/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=5355742355261477205' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5355742355261477205'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/5355742355261477205'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/07/ccli-is-disgusting.html' title='C++/CLI is disgusting'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-8225411505006494787</id><published>2007-07-12T14:10:00.001-06:00</published><updated>2007-07-12T14:36:26.604-06:00</updated><title type='text'>Goodbye, ANTLR</title><content type='html'>Three days ago, after finding workarounds for the ANTLR3 (C#) bugs &lt;a href="http://qscribble.blogspot.com/2007/07/no-matter-what-i-do-i-cant-seem-to-make.html"&gt;detailed here&lt;/a&gt;, I immediately ran into even more bugs. For instance I had a rule that said&lt;br /&gt;&lt;br /&gt;SL_COMMENT: '#' (~NEWLINE_CHAR)*;&lt;br /&gt;&lt;br /&gt;Somehow the generated code for this rule included a check (during the matching stage, if I remember correctly) that said, in essense, "if the comment contains a slash character, generate a syntax error". What the hell? And there was another bug besides that which I've forgotten. My bug report on the first batch of bugs went mostly unacknowledged, so I didn't bother to try isolating this new problem.&lt;br /&gt;&lt;br /&gt;Instead, I'm planning to try another approach: I'll make my own ANTLR. I bought the ANTLR book May 26, and I've been unable to get the thing to work for me since then. I'm getting impatient. I know how a LL parser generator should behave, so I ought to be able to make one... right?&lt;br /&gt;&lt;br /&gt;Of course, I would like a parser generator done the Loyc way - as an extension to Loyc. But it'll be a little bit tricky to do this, because Loyc does not actually exist yet. It's still in the planning stages! There are no AST classes, no ONEP. So what will I do?&lt;br /&gt;&lt;br /&gt;Well, my initial goal will be a translator from boo to boo. I'll make some AST classes and give them the ability to print themselves out as source code. Then I'll create a lexer and tree parser by hand; as for the main parser, I'm not sure how to approach it. But after I've done those things, I'll write some routines for printing out AST nodes as text. So it will be able to read source code and spit it back out.&lt;br /&gt;&lt;br /&gt;At this point I've already written a lot of the lexer by hand. I've taken it as an opportunity to figure out how a parser generator should work, by attempting to write the lexer the way a machine would do it. I started by writing the lexer grammar in a hypothetical boo-style syntax; then I translated that grammar--mechanically, by hand--to C# source code.&lt;br /&gt;&lt;br /&gt;There is so much work I have to do before I start making the parser generator, though. I fear that by the time I'm done with the prerequisites, I will have forgotten the lessons I'm now learning about making a parser generator. We'll see.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-8225411505006494787?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/8225411505006494787/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=8225411505006494787' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/8225411505006494787'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/8225411505006494787'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/07/goodbye-antlr.html' title='Goodbye, ANTLR'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-4298256931773517425</id><published>2007-07-12T12:18:00.000-06:00</published><updated>2007-07-12T17:08:08.650-06:00</updated><title type='text'>What's wrong with Java</title><content type='html'>When I see the &lt;a href="http://72.5.124.55/j2se/1.5.0/docs/relnotes/features.html#lang"&gt;features added recently to Java&lt;/a&gt;, I'm sure glad I'm using .NET, C# and boo. Even though Java is a lot older than .NET, .NET seems to get the good features first. Care in point: Generics. For a former C++ developer, it seems stupid to give up type-safe collections; I don't know how many years it took before Java got generics (10?) but .NET got them in much less time (less than 4 years, I do believe) and Java was left playing catch-up. In fact, most of the "new" features in Java 5.0 seem to be things that C# had from the beginning:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;enhanced for loop (foreach in C#)&lt;/li&gt;&lt;li&gt;autoboxing/unboxing&lt;/li&gt;&lt;li&gt;enums&lt;/li&gt;&lt;li&gt;varargs (variable argument lists - "params" in C#)&lt;/li&gt;&lt;li&gt;annotations (much like .NET attributes)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Java generics aren't even supported by the JVM, so you get the same performance penalty from casting that you did before. I've always been unhappy with Java's performance (especially for GUI programs), whereas .NET just doesn't seem slow.&lt;br /&gt;&lt;br /&gt;Look, Sun, if nothing short of competition from Microsoft can prompt you to improve Java, you must not care very much much about it.&lt;br /&gt;&lt;br /&gt;Let's see, what else...&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Value types (structs). This is a big one for me because can offer a big performance boost in many situations. You don't want to allocate a new object if that object contains nothing more than an integer and some methods, do you? A new object sucks up at least 16-20 bytes of memory even if it just contains one integer or reference; creating it requires multiple method calls and all those bytes have to be initialized. Useful value types include&lt;/li&gt;&lt;ul&gt;&lt;li&gt;A "Point" type that has X and Y coordinates&lt;/li&gt;&lt;li&gt;A "FixedInt" type that contains a fixed-point number (the language must support operator overloading to make it easy to use, of course.)&lt;/li&gt;&lt;li&gt;A "BigInt" type that contains a small integer normally, but allocates a memory block for a large integer if necessary.&lt;/li&gt;&lt;li&gt;A "Handle" type that contains an opaque reference to something else&lt;br /&gt;&lt;/li&gt;&lt;li&gt;A "Symbol" type that contains a numeric identifier that represents a string (symbols are a built-in feature of Ruby and are typically used like enums, except they are more flexible)&lt;/li&gt;&lt;li&gt;A Pair&lt;a,b&gt; type that contains a pair of values A and B; often you get better performance by not allocating memory for this purpose.&lt;/a,b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Multi-language support. Well, the JVM can certainly support multiple languages, but only .NET is specifically designed for it. Admittedly, the design isn't that great, but at Microsoft specifically considers the needs of other languages.&lt;/li&gt;&lt;li&gt;Delegates. The Java equivalent is using interfaces with one function in them, but this is relatively inflexible and certainly more annoying to use. Java provides inner (&lt;a href="http://www.exciton.cs.rice.edu/JavaResources/Java/innerclasses.htm"&gt;even anonymous&lt;/a&gt;) classes to help people use the pattern, but delegates are way better.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Closures (functions inside other functions, where the inner function can access local variables of the outer function). Java doesn't have that, does it? You can access "final" variables from a function-inside-a-class-inside-a-function, but that's all.  By the way, .NET itself doesn't actually support closures, but C# fakes it well.&lt;/li&gt;&lt;li&gt;Iterators. Now this may be my favorite feature of C# 2.0; it would be hard to choose between iterators and generics. I love them not only because you can create enumerators easily (which is great) but also because you can approximate &lt;a href="http://qdl.sourceforge.net/coroutines.html"&gt;coroutines&lt;/a&gt; with them.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Swing. Ugh! It's ugly, it's slow, and the Windows "skin" isn't very convincing. There often seem to be glitches in Swing that you don't find in other programs, such as the failure to resize a window fluidly (i.e. the window doesn't redraw itself until you let go of your mouse button). Finally, and worst of all, developing Swing GUIs is a huge pain in the ass. I absolutely can't stand it. The .NET counterpart, Windows.Forms, doesn't seem all that well designed, but it looks good, it's relatively fast, and it's easy to write code for it. Plus, of course, a good Forms designer is a standard feature of any .NET IDE.&lt;/li&gt;&lt;/ul&gt;Right now I wish I could have the C# 3.0 "var" feature because I'm sick of typing&lt;br /&gt;&lt;br /&gt;SomeJerkGaveThisClassALongName foo = new SomeJerkGaveThisClassALongName();&lt;br /&gt;&lt;br /&gt;Obviously we should be able to write simply&lt;br /&gt;&lt;br /&gt;var foo = new SomeJerkGaveThisClassALongName();&lt;br /&gt;&lt;br /&gt;And there's a lot of other great stuff in &lt;a href="http://72.14.253.104/search?q=cache:sF_fuakzdkwJ:download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%25203.0%2520specification.doc+C%23+3.0&amp;hl=en&amp;amp;amp;amp;amp;amp;amp;ct=clnk&amp;cd=3&amp;amp;gl=ca&amp;amp;client=firefox-a"&gt;C# 3.0&lt;/a&gt; [&lt;a href="http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%203.0%20specification.doc"&gt;.doc&lt;/a&gt;]:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Lambda expressions (syntactic sugar for anonymous inner functions) with type inference&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Type inference for generic method calls&lt;/li&gt;&lt;li&gt;Extension methods (they are not well thought out, but I'd rather have them than not)&lt;/li&gt;&lt;li&gt;Object and collection initializers (to make code more brief)&lt;/li&gt;&lt;li&gt;Anonymous POD ("plain old data") classes, which work like tuples except that the fields have names.&lt;/li&gt;&lt;li&gt;And last but not least, the query thingie, LINQ.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt; Suddenly, C# is starting to seem a lot more like &lt;a href="http://boo.codehaus.org/"&gt;boo&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Having said all this, there are a couple of things from Java that I might like to have in C#:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The assert statement. Typing Debug.Assert() all the time is driving me nuts.&lt;/li&gt;&lt;li&gt;Inner classes that have an implicit link to the outer class&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;And let's see, if I could have some more features I think they would include&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Traits&lt;/li&gt;&lt;li&gt;The ability to supply a default implementation for a member of an interface&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Preconditions and postconditions on methods&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Static and run-time unit checking (units as in metres, litres, bytes, pixels, etc.)&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-4298256931773517425?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/4298256931773517425/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=4298256931773517425' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/4298256931773517425'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/4298256931773517425'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/07/whats-wrong-with-java.html' title='What&apos;s wrong with Java'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2458417257543687994.post-7218403261773614947</id><published>2007-07-12T12:16:00.000-06:00</published><updated>2007-07-12T14:38:53.569-06:00</updated><title type='text'>The Loyc Blog</title><content type='html'>This blog will be a place for me to report on the progress of &lt;a href="http://loyc.net"&gt;Loyc&lt;/a&gt; and to comment on the programming field in general. Welcome.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2458417257543687994-7218403261773614947?l=loyc-etc.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://loyc-etc.blogspot.com/feeds/7218403261773614947/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=2458417257543687994&amp;postID=7218403261773614947' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7218403261773614947'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2458417257543687994/posts/default/7218403261773614947'/><link rel='alternate' type='text/html' href='http://loyc-etc.blogspot.com/2007/07/loyc-blog.html' title='The Loyc Blog'/><author><name>Qwertie</name><uri>http://www.blogger.com/profile/04595705428290721343</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://www.geocities.com/Qwertie256/myface2.jpg'/></author><thr:total>0</thr:total></entry></feed>
