<?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'><id>tag:blogger.com,1999:blog-2447174102813539049</id><updated>2009-07-01T21:56:30.848-07:00</updated><title type='text'>Room 101</title><subtitle type='html'>A place to be (re)educated in Newspeak</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default?start-index=26&amp;max-results=25'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>33</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-1844927628139698674</id><published>2009-06-30T16:28:00.000-07:00</published><updated>2009-06-30T16:45:19.186-07:00</updated><title type='text'>A Ban on Imports</title><content type='html'>This is not, of course, an essay on restricting free trade.  Rather, this post is about the evils of the &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt; clause, which occurs in one form or another across a wide array of programming languages, from Ada, Oberon and the various Modulas, through Java and C#, and on to F# and Haskell.&lt;br /&gt;&lt;br /&gt;The import clause should be banned because it undermines modularity in a deep and insidious way. This is a point I’ve attempted to convey time and time again, with only limited success. I will now try to illustrate the problem via a hardware inspired example.&lt;br /&gt;&lt;br /&gt;Consider the not-so-humble MP3 player. An MP3 player is a hardware module. The market is full of them, as well as other hardware modules they can plug in to. For example, sound systems where on can dock an MP3 player and have it play on stereo speakers.&lt;br /&gt;&lt;br /&gt;Let’s try and describe the analog of such a sound system using programming language modularity constructs that rely on imports:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;module&lt;/span&gt; SoundSystem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;    import&lt;/span&gt; MP3Player;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;  ... wonderful functionality elided ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I want to describe how my sound system works, separately from the description of how an MP3 player works.  I would like to later plug in a particular MP3 player, say a Zune(tm) or an iPod(tm)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;tm&lt;/span&gt;&lt;span style="font-style: italic;"&gt;: Zune and iPod are trademarks of Microsoft and Apple respectively, two companies with armies of lawyers who might harass me if I do not state the obvious.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now the first problem is that neither Zune or iPod are named MP3Player.  If I want to connect my sound system to a Zune, I will have to edit the definition of &lt;span style="font-style: italic;"&gt;SoundSystem&lt;/span&gt; to name the specific module I want to import.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;If you’re very petty, you might say that Zune and iPod do not share a common interface and cannot be docked into the same sound system.  Imagine that we wish to use our sound system with an iPhone (tm)  and an iPod Touch (tm)  of some compatible generation.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;tm&lt;/span&gt;: iPhone and iPod Touch are trademarks of Apple.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Say I decide to go with a Zune.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;module&lt;/span&gt; SoundSystem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt; Zune;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;  ... wonderful functionality elided ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Later I change my mind for some reason, and want to hook up my system to an iPod. It’s easy: I just edit the definition of my system again, to import iPod:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;module&lt;/span&gt; SoundSystem&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;    &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt; iPod;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;  ... wonderful functionality elided ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;end&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The question you should be asking is: Why should I edit the definition of my system each time I change the configuration? In reality, it is unlikely that I am actually the designer of &lt;span style="font-style: italic;"&gt;SoundSystem&lt;/span&gt;. I probably don’t even have access to its definition. I just want to configure it to work with my MP3 player.&lt;br /&gt;&lt;br /&gt;The problem is that &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt; confounds &lt;span style="font-style: italic;"&gt;module definition&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;module configuration&lt;/span&gt;. Module definition describes the design of a module; module configuration describes how one hooks up different modules. The former has to do with module &lt;span style="font-style: italic; font-weight: bold;"&gt;internals&lt;/span&gt;; the latter should be done &lt;span style="font-weight: bold; font-style: italic;"&gt;externally&lt;/span&gt; to the modules involved, to allow them to be used in any context where they could function.&lt;br /&gt;&lt;br /&gt;We clearly want our sound system to abstract over the specific player being plugged in to it. Any player with a compatible interface will do.  A well known mechanism for abstracting things is parameterization.  We might be happier if we defined our sound system parametrically with respect to the MP3 player&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;module&lt;/span&gt; SoundSystem(anMP3Player){&lt;br /&gt;... great wonders using anMP3Player ...&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We could them configure our system to use an iPod:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;SoundSystem(iPod);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;or a Zune&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;SoundSystem(Zune);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;without having to modify (or even have access to) the source code for the definition of &lt;span style="font-style: italic;"&gt;SoundSystem&lt;/span&gt;. Hurray!&lt;br /&gt;&lt;br /&gt;The module definition looks a lot like a function, and the configuration code looks like a function application. This is very suggestive. Indeed, ML introduced a module system based on function-like things called &lt;span style="font-style: italic;"&gt;functors&lt;/span&gt; a quarter century ago.  But there’s a bit more to this.&lt;br /&gt;These hardware pieces tend to plug in to &lt;span style="font-style: italic; font-weight: bold;"&gt;each other&lt;/span&gt;.  For example, the definition of IPod is parametric too:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;IPod(dockingStation){... even greater wonders ...}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Our sound system does its thing by behaving like a docking station. It and the MP3 player are mutually recursive modules. Configuration therefore requires support for mutual recursion (which is not allowed in Standard ML):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;letrec&lt;/span&gt; {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;  dock = SoundSystem(mp3Player);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;  mp3Player = IPod(dock);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;} &lt;span style="font-weight: bold;"&gt;in&lt;/span&gt; dock;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;If this notation is unfamiliar, please brush up on your functional programming skills before you become unemployable. Basically, ignore the  first and last line, and treat the two lines involving = as equations.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So the module definitions are a lot like functions that yield modules. You could also think of module definitions as classes yielding instances. The instances are like physical hardware modules.&lt;br /&gt;&lt;br /&gt;Now we can add another sound system and use our old Zune&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;letrec&lt;/span&gt;&lt;span style="font-style: italic;"&gt; {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;dock2 = SoundSystem(oldMP3);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;oldMP3 = Zune(dock2);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;} &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;in&lt;/span&gt;&lt;span style="font-style: italic;"&gt; dock2;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is what is often called side by side deployment - multiple instances of the same design, configured differently.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Tangent:&lt;/span&gt;  Yes, Virginia, you can achieve that sort of thing in Java despite imports, using class loaders. Imports hardwire names into your code, and class loaders can counteract that by letting you define multiple namespaces. These can have multiple copies of your code, potentially hardwired to different things  (even though they all have the same name). If you think class loaders offer  a simple, clean way of doing things that is easy to learn, use, understand and debug, this post is not for you. Nor will any amount of OSGi magic on top fundamentally change things.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We might also choose to define things differently&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;module&lt;/span&gt;&lt;span style="font-style: italic;"&gt; SoundSystem(MP3Player) { &lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;    player = MP3Player(self);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt; ... &lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here, we are passing module definitions as parameters. We are also referring to &lt;span style="font-style: italic;"&gt;SoundSystem&lt;/span&gt;’s current instance from within itself - a lot like classes, no? We might configure things thusly&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;SoundSystem(iPod);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So it looks like mutual recursion and first class module definitions are very natural things to have. And yet traditional languages do not support this - even though many languages have constructs like classes and functions that are first class values and can be defined in a mutually recursive fashion. &lt;br /&gt;&lt;br /&gt;One problem with using these constructs to define modules is that they are usually able to access anything in the global namespace. This makes it very hard to avoid implicit dependencies.&lt;br /&gt;&lt;br /&gt;Interestingly, the global namespace is exactly what &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt; requires. Since we don’t need or want &lt;span style="font-weight: bold;"&gt;import&lt;/span&gt;, let’s do away with it and the global namespace.  We clearly will get a much more modular system without it; but wait - there seems to be one place where we really want the global namespace. That is when we write our configuration code, the code that wires our modules together. &lt;br /&gt;&lt;br /&gt;That’s fine - there are a number of solutions for that. It isn’t always clear that our configuration language is the same language as the programming language(s) that define our modules, for example. If you write a makefile, the global namespace is defined by your file system and accessible within the makefile. Not that I really want to recommend make and its ilk.&lt;br /&gt;&lt;br /&gt;I think we do want to code our configuration in a nice general purpose high level programming language. One solution is to have our IDE provide us with an object representing the known global namespace, and write our configuration code with respect to that namespace object. This is essentially what we do in Newspeak.&lt;br /&gt;&lt;br /&gt;In the next post, I’ll discuss more of the advantages of this approach, contrast how Newspeak handles things with other languages with powerful module systems, like Scheme (which for the past decade or so has had a system called Units that is quite close to what I’ve discussed so far) and ML, and show once more how one actually does configuration in Newspeak.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;To be continued.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-1844927628139698674?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/1844927628139698674/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=1844927628139698674' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1844927628139698674'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1844927628139698674'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/06/ban-on-imports.html' title='A Ban on Imports'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5605434425459409714</id><published>2009-05-25T16:10:00.000-07:00</published><updated>2009-05-25T18:10:35.004-07:00</updated><title type='text'>Original Sin</title><content type='html'>I’ve often said that Java’s original sin was not being a pure object oriented language - a language where everything is an object.&lt;br /&gt;&lt;br /&gt;As one example, consider type &lt;span style="font-weight: bold;"&gt;char&lt;/span&gt;.  When Java was introduced, the Unicode standard required 16 bits. This later changed, as 16 bits were inadequate to describe the world’s characters.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: Not really surprising if you think about it. For example, Apple’s internationalization support allocated 24 bits per character in the early 1990s. However, engineering shortcuts are endemic. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In the meantime, Java had committed to a 16 bit character type. Now, if characters were objects, their representation would be encapsulated, and nobody would very much affected how many bits are needed. A primitive type like &lt;span style="font-weight: bold;"&gt;char&lt;/span&gt;, however, advertises its representation to the world. Consequently, people dealing with unicode in Java have to deal with encoding code points themselves.&lt;br /&gt;&lt;br /&gt;I was having this conversation for the umpteenth time last week. My interlocutor asked whether it would be possible for Java to be as efficient as it was without primitive types. The answer is yes, and prompted this post.&lt;br /&gt;&lt;br /&gt;So how would we go about getting rid of primitive types without incurring a significant performance penalty?&lt;br /&gt;&lt;br /&gt;Java has a mandatory static type system; it is compiled into a statically typed assembly language (Java byte codes, aka JVML). It supports final classes. I do not favor any of these features, but we will take them as a given.  The only changes we will propose are those necessary to eradicate primitive types.&lt;br /&gt;&lt;br /&gt;Assume that the we have a final class &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt; representing 32 bit integers.  The compiler can translate occurrences of this type into type &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt;. Hence, we can generate the same code for scalars as Java does today with no penalty whatsoever.&lt;br /&gt;&lt;br /&gt;To make &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt; a suitable replacement for &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt;, we would like the syntactic convenience of using operators:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;font-size:100%;" &gt;Int twice(Int i) { return i + i;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is easy enough. We don’t want operator overloading of course. What we want is simply the ability to define methods whose names are operators.  The language can support the same set of binary operators it does today, with the same fixed precedence.  No need for special syntax and rules to define the precedence of operators etc. I leave that sort of thing to people who love complexity.&lt;br /&gt;&lt;br /&gt;It is crucial that &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;s behave as values. This requires that the &lt;span style="font-weight: bold;"&gt;==&lt;/span&gt; operator on &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;s behaves the same as equality. There should be no way to detect if we’ve allocated one copy of the number 3 or a million of them. Since we can define &lt;span style="font-weight: bold;"&gt;==&lt;/span&gt; as a method, we can define it in &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt; to work the way it does for most objects, and override it with a method in &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt; and other value classes to work differently. We’ll want to take care of &lt;span style="font-weight: bold;"&gt;identityHash&lt;/span&gt; as well of course.&lt;br /&gt;&lt;br /&gt;At this point, we can simply say that &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; stands for &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;. Except for locking. What would it mean to synchronize on an instance of &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;? To avoid this nastiness, we’ll just say that all instances of &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt; are locked when they are created. Hence no user code can ever synchronize on them.&lt;br /&gt;&lt;br /&gt;In this hypothetical dialect, literal integers such as 3 are considered instances of &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;. They, and all other integer results, can be stored in collections, passed to polymorphic code that requires type &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt; etc. The compiler sees to it that they are usually represented as the primitive integer type of the JVM. When they are used as objects, they get boxed into real instances of &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;. When an object gets cast to an &lt;span style="font-weight: bold;"&gt;Int&lt;/span&gt;, it gets unboxed.&lt;br /&gt;&lt;br /&gt;This is similar to what happens today, &lt;span style="font-style: italic;"&gt;except that it is completely transparent&lt;/span&gt;.  Because these objects have true value semantics for identity, you can never tell if a new instance was allocated by the boxing or not; indeed, you cannot tell if boxing or unboxing occur at all.&lt;br /&gt;&lt;br /&gt;So far I haven’t described anything really new. A detailed proposal along the lines above existed years ago.  It would allow adding new user defined value types like &lt;span style="font-weight: bold;"&gt;Complex&lt;/span&gt; as well.&lt;br /&gt;&lt;br /&gt;A more restricted, but somewhat similar proposal was considered as part of JSR201 - the idea being that boxing would create value objects, without actually replacing the existing primitive types. It was rejected by the expert group. As I recall, only myself and Corky Cartwright supported it. There were concerns as to what would become of the existing wrapper classes (&lt;span style="font-weight: bold;"&gt;Integer&lt;/span&gt; and friends); those are used by reflection etc., and are hopelessly broken because they have public constructors that allocate new instances for all the world to see.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: I don’t know who came up with the idea of an Integer class that could have multiple distinct instances of 3. I assume it was another engineering shortcut (see above) by someone really clever.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To be clear - &lt;span style="font-style: italic; font-weight: bold;"&gt;I am not suggesting that this discussion should be reopened. This is just a mental exercise&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;There is only one place where this proposal introduces a performance penalty: polymorphic code on type &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt; that tests for identity. Now that identity testing is method, it will be slower. Not by all that much, and only for type &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Why? Because we preclude overriding &lt;span style="font-weight: bold;"&gt;==&lt;/span&gt; in non-value classes. There are several ways to arrange this. Either by fiat, or by rearranging the class hierarchy with types &lt;span style="font-weight: bold;"&gt;Value&lt;/span&gt; (for value types) and &lt;span style="font-weight: bold;"&gt;Reference&lt;/span&gt; (for regular objects) as the only subtypes of &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt;, making &lt;span style="font-weight: bold;"&gt;==&lt;/span&gt; final in those two types. Or some other variation&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What has not been seriously discussed, to my knowledge, is what to do with arrays. As indicated above, scalar uses of primitive types don’t pose much of a problem.&lt;br /&gt;&lt;br /&gt;However, what of types like &lt;span style="font-weight: bold;"&gt;int[]&lt;/span&gt;? Again, we can allocate these as arrays of 32 bit integers just as we do today.  No memory penalty, no speed penalty when writing in &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt;s or reading them out.&lt;br /&gt;&lt;br /&gt;What makes things complicated is this: If &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; is a subtype of &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt; (as it is in the above) we’d expect &lt;span style="font-weight: bold;"&gt;int[]&lt;/span&gt; to be a subtype of &lt;span style="font-weight: bold;"&gt;Object[]&lt;/span&gt;, because in Java we expect covariant subtyping among arrays.&lt;br /&gt;&lt;br /&gt;Of course that isn’t type safe, and one could certainly argue that this our chance to correct that problem.  But I won’t. Instead, assume we want to preserve Java’s covariant array subtyping.&lt;br /&gt;&lt;br /&gt;The thing is, we do not want to box all the elements of an&lt;span style="font-weight: bold;"&gt; int[]&lt;/span&gt; when we assign it to an &lt;span style="font-weight: bold;"&gt;Object[]&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The zeroth order solution is to leave the existing subtype relations among arrays of the predefined types  unchanged. It’s ugly, but we are no worse off than we are today - and have a bunch of advantages because we are free of primitive types. But we can do better.&lt;br /&gt;&lt;br /&gt;Suppose &lt;span style="font-weight: bold;"&gt;Object[]&lt;/span&gt; has methods &lt;span style="font-weight: bold;"&gt;getObjectElement&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;setObjectElement&lt;/span&gt;. These (respectively) retrieve and insert elements into the array.&lt;br /&gt;&lt;br /&gt;We can equip &lt;span style="font-weight: bold;"&gt;int[]&lt;/span&gt; with versions of these methods that box/unbox elements that are being retrieved or inserted. We ensure that &lt;span style="font-weight: bold;"&gt;a[i]&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;a[i] = e&lt;/span&gt; are compiled differenty based upon the static type of &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt;. If &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; is &lt;span style="font-weight: bold;"&gt;Object[]&lt;/span&gt;, we use the &lt;span style="font-weight: bold;"&gt;getObjectElement&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;setObjectElement&lt;/span&gt;.  If &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; is of type&lt;span style="font-weight: bold;"&gt; int[]&lt;/span&gt;, we use methods that access the integer representation directly, say, &lt;span style="font-weight: bold;"&gt;getIntElement&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;setIntElement&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;At this point, we can even introduce meaningful relations among array types like &lt;span style="font-weight: bold;"&gt;int[]&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;short[] &lt;/span&gt;using the same technique.&lt;br /&gt;&lt;br /&gt;In essence, &lt;span style="font-weight: bold;"&gt;Object[]&lt;/span&gt; provides an interface that different array types may implement differently. We just provide a sugar for accessing this interface magically based on type.  On a modern implementation like Hotspot, the overhead for this would be minimal.&lt;br /&gt;&lt;br /&gt;Of course, if you make arrays invariant, the whole issue goes away. That just makes things too easy. Besides, I’ve come to the conclusion that Java made the right call on array covariance in the first place.&lt;br /&gt;&lt;br /&gt;All in all - Java could have been purely object oriented with no significant performance hit. But it wasn’t, isn’t and likely won’t.   &lt;span style="font-style: italic;"&gt;Sic Transit Gloria Mundi.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5605434425459409714?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5605434425459409714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5605434425459409714' title='50 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5605434425459409714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5605434425459409714'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/05/original-sin.html' title='Original Sin'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>50</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5734360635972064061</id><published>2009-04-30T19:09:00.000-07:00</published><updated>2009-04-30T19:54:06.519-07:00</updated><title type='text'>The Need for More Lack of Understanding</title><content type='html'>People are always claiming that if only there was more understanding in the world, it would be a better place.  This post will argue that less is more: we need less understanding - specifically  more not understanding.&lt;br /&gt;&lt;br /&gt;A couple of weeks ago I gave a &lt;a href="http://msdn.microsoft.com/en-us/oslo/dd727737.aspx"&gt;talk&lt;/a&gt; at &lt;a href="http://www.sellsbrothers.com/conference/"&gt;DSL Dev Con.&lt;/a&gt; One of the encouraging things that was evident there was the increased understanding that &lt;span style="font-style: italic; font-weight: bold;"&gt;not&lt;/span&gt; understanding is important.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: While I'm advertising this talk, I might as well advertise &lt;/span&gt;&lt;a style="font-style: italic;" href="http://channel9.msdn.com/shows/Going+Deep/Gilad-Bracha-Inside-Newspeak/"&gt;my interview on Microsoft's channel 9&lt;/a&gt; which&lt;span style="font-style: italic;"&gt; explains the motivation for Newspeak and its relation to cloud computing.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Several programming languages support a mechanism by which a class or object can declare a general-purpose handler for method invocations it does not explicitly support.&lt;br /&gt;&lt;br /&gt;Smalltalk was, AFIK, the first language to introduce this idea. You do it by declaring a method called &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; . The method takes a single argument, that represents a reification of the call. The argument tells us the name of the method that was invoked, and the actual arguments passed.  If a method &lt;span style="font-style: italic;"&gt;m is&lt;/span&gt; invoked on an object that does not have a member method &lt;span style="font-style: italic;"&gt;m (&lt;/span&gt;that is, &lt;span style="font-style: italic;"&gt;m&lt;/span&gt; is not declared by the class of the object or any of its superclasses), then the object’s  &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; method is invoked. The default implementation of &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt;, declared in class &lt;span style="font-weight: bold;"&gt;Object&lt;/span&gt;, is to throw an exception. By overriding  &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; one can control the system’s behavior when such calls are made. Similar mechanisms exist in several other dynamic languages (e.g., &lt;span style="font-weight: bold;"&gt;missingMethod&lt;/span&gt; in Ruby and Groovy, &lt;span style="font-weight: bold;"&gt;_noSuchMethod_&lt;/span&gt; in some dialects of Javascript).&lt;br /&gt;&lt;br /&gt;Aficionados of these languages know that this is an extremely useful mechanism. However, users of mainstream object-oriented languages typically lack an appreciation of the power this mechanism can provide. I hope this post can  be a small step in rectifying that situation.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;DoesNotUnderstand:&lt;/span&gt; helps implement orthogonal persistence, lazy loading,  futures, and remote proxies, to name a few.  Recently, there’s been a surge of interest in domain-specific languages, and &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; can help there as well.&lt;br /&gt;&lt;br /&gt;We’ll use an example from my talk at DSL Dev Con.  Consider how to interact with an OS shell like bash or csh from within a general purpose programming language.  We’ll use Newspeak as our general purpose language (what were you expecting?), because it works best (in my unbiased opinion).&lt;br /&gt;&lt;br /&gt;Suppose you want a listing of the files in the current directory. You could view ls as a method on a shell object, and write: &lt;span style="font-weight: bold;"&gt;shell ls&lt;/span&gt;.  Of course, we won’t do something like the following Java code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;class Shell {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    public Collection&lt;string&gt; ls() {...}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;... an infinity of other stuff&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are any number of commands that a shell can understand, depending on the current path and the executables in the directories on that path. We cannot plausibly enumerate them all as a fixed set of methods in &lt;span style="font-weight: bold;"&gt;Shell&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Instead, in we can define a class &lt;span style="font-weight: bold;"&gt;NewShell&lt;/span&gt; with a &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; method to look up the name of the message in the shell’s path and execute it.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;shell ls&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If we write this code in the context of a subclass of &lt;span style="font-weight: bold;"&gt;NewShell&lt;/span&gt;, we can take advantage of Newspeak’s implicit receiver sends and just write&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Nice, but not quite good enough. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls aFilename&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;doesn’t work at all.  We don’t want to invoke ls immediately here - we need to gather its arguments in some way.  One way to do this is to have &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; return a  function object, that can be fed its arguments.  This is in fact what we do in our implementation. We call this object a &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt;. To get a &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt; to actually run the command, you call one of is value methods, with the desired arguments:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls value: aFileName&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is less convenient for the simple case, where we need to write&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls value&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;to get ls to do something - but it is much more general.&lt;br /&gt;&lt;br /&gt;What about modifiers, as in &lt;span style="font-weight: bold;"&gt;ls -l&lt;/span&gt; ? We can make simple cases work slightly better by defining -  as a method on &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt; :&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls -’l’&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is what the current implementation does.&lt;br /&gt;&lt;br /&gt;The most general approach is to treat them as arguments&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls value: ‘-l’&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls value: ‘-l’ value: aFileName&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;An alternative might be to leave ls as it was originally, but allow&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls: aFileName&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;as well. In this version, &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; checks to see if the message takes an argument (i.e., it ends with a colon). If so it strips the colon off the message name, creates &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt; for the result, and calls its &lt;span style="font-weight: bold;"&gt;value:&lt;/span&gt; method with the argument. This handles modifiers pretty well&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls: ‘-l’&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If there are multiple arguments, we can pass a tuple as the argument, and doesNotUnderstand: will unpack it as needed.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ls: {‘-l’. aFileName}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now how about pipes?&lt;br /&gt;&lt;br /&gt;We could introduce &lt;span style="font-weight: bold;"&gt;pipeValue&lt;/span&gt; methods, that produced an object that responded to  the pipe operator. Or we could say that everything produced a &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt; (and these understood “&lt;span style="font-weight: bold;"&gt;|&lt;/span&gt;”) and a special action is needed to get a result (sending it an &lt;span style="font-weight: bold;"&gt;evaluate&lt;/span&gt; or &lt;span style="font-weight: bold;"&gt;end&lt;/span&gt; message). This action is the analog of the newline that tells the shell to go ahead and evaluate. This could be dispensed with in a lazy setting.&lt;br /&gt;&lt;br /&gt;Combining our second proposal above with this, we could say that value was used to derive a result. Then we can view the shell as a combinator library for &lt;span style="font-weight: bold;"&gt;CommandSessions&lt;/span&gt;. This does conflate two issues - the use of &lt;span style="font-weight: bold;"&gt;CommandSession&lt;/span&gt; to delay evaluation until a result is needed (the shell parses the input as a unit ensuring laziness)   and the use of real combinators on byte streams.&lt;br /&gt;&lt;br /&gt;We use &lt;span style="font-weight: bold;"&gt;NewShell&lt;/span&gt; in our IDE - for example, to manipulate subversion commands in the source control browser. It would be nice to refine it further, perhaps along the lines suggested above, but even in its current simplistic incarnation, it is quite useful.&lt;br /&gt;&lt;br /&gt;As I noted at the beginning of this post, there a host of other cool uses for &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt;. I may return to those in another post.&lt;br /&gt;&lt;br /&gt;Of course,  if you are a fan of mandatory static typing, you aren’t allowed to use &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt; in your language. n the general case, it simply cannot be statically typed - which is an argument against mandatory typing, not against &lt;span style="font-weight: bold;"&gt;doesNotUnderstand:&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Just as switch statements, catch clauses and regular expressions all need defaults/catch-alls/wildcards, so does method dispatch.  There are situations where you cannot avoid uncertainty.  Reality is dynamic.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5734360635972064061?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5734360635972064061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5734360635972064061' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5734360635972064061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5734360635972064061'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/04/need-for-more-lack-of-understanding.html' title='The Need for More Lack of Understanding'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-6796281891265227069</id><published>2009-04-18T17:17:00.000-07:00</published><updated>2009-04-18T17:56:49.586-07:00</updated><title type='text'>The Language Designer’s Dilemma</title><content type='html'>Last week I was at &lt;a href="http://langnetsymposium.com/"&gt;lang.net 09&lt;/a&gt; and &lt;a href="http://sellsbrothers.com/conference/"&gt;DSL Dev Con&lt;/a&gt;. It was great fun, as always. The &lt;a href="http://langnetsymposium.com/2009/talks/23-ErikMeijer-LiveLabsReactiveFramework.html"&gt;best talk was by Erik Meijer&lt;/a&gt;. Use the link to see it - but without live video of Erik in action, you can't really understand what it was like. Such is performance art.&lt;br /&gt;&lt;br /&gt;I also gave a &lt;a href="http://langnetsymposium.com/2009/talks/08-GiladBracha-Hopscotch.html"&gt;talk&lt;/a&gt;, and there were many others, but that is not the point of this post. Rather, this post was prompted by one specific, and excellent, talk - Lars Bak’s &lt;a href="http://langnetsymposium.com/2009/talks/18-LarsBak-JavaScript.html"&gt;presentation&lt;/a&gt; on V8.  Lars clearly enjoyed his visit to the lion’s den; more importantly, perhaps Microsoft will finally shake off their apparent paralysis and produce a competitive Javascript engine.&lt;br /&gt;&lt;br /&gt;That, in turn, will make it possible to distribute serious applications targeting the web browser, with the knowledge that all major browsers have performant Javascript engines that can carry the load.&lt;br /&gt;&lt;br /&gt;It’s all part of the evolution of the web browser into an OS. This was what Netscape foresaw back in 1995.  And it is a perfect example of &lt;a href="http://www.google.com/books?id=lqKho8KWXmAC&amp;amp;dq=Innovator%27s+Dilemma&amp;amp;printsec=frontcover&amp;amp;source=bn"&gt;innovator’s dilemma&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Innovator’s dilemma applies very directly to programming languages, and Todd Proebsting already &lt;a href="http://www.google.com/url?sa=U&amp;amp;start=1&amp;amp;q=http://ll2.ai.mit.edu/talks/proebsting.ppt&amp;amp;ei=yXHqSc_KKaXoswOQ1_TfAQ&amp;amp;sig2=SKypcCim41psrQv4fBXymA&amp;amp;usg=AFQjCNFRF-2e-HeTHnEJ15nXu3yrhkFaGg"&gt;discussed this&lt;/a&gt; back in 2002.&lt;br /&gt;&lt;br /&gt;To recap, the basic idea is that established players in a market support sustaining innovation but not disruptive innovation. Examples of sustaining innovation would be the difference between Windows 2000 and Windows XP, or between Java 1.0 thru Java 6.&lt;br /&gt;Sustaining innovations are gradual improvements and refinements - some improvement in performance, or some small new feature etc.&lt;br /&gt;&lt;br /&gt;Disruptive innovation tends to appear “off the radar” of the established players. It is often inferior to the established product in many ways. It tends to start in some small niche where it happens to work better than the established technology. The majors ignore it, as it clearly can’t handle the “real” problems they are focused on. Over time, the new technology grows more competent and eats its way upward, consuming the previous market leader’s lunch.&lt;br /&gt;&lt;br /&gt;We’ve seen this happen many times before. Remember Sun workstations? PCs came in and took over that market. Sun retreated to the server business, and PC’s chase it up towards the high end of that market, until there’s nowhere left to run.&lt;br /&gt;&lt;br /&gt;In the programming language space, take Ruby as an example. Ruby is slow, until quite recently lacked IDE support etc. It’s easy for the Javanese to dismiss it.  Over time, such technology can evolve to be much faster, have better tooling etc. And so it may grow into Java’s main market.&lt;br /&gt;&lt;br /&gt;Don’t believe me? Java in 1995 was just a language for writing applets. It’s performance was poorer than Smalltalk’s, and nowhere near that of C++.  There were no IDEs. Who's laughing now?&lt;br /&gt;&lt;br /&gt;Javascript is an even clearer case in point.  It was just a scripting language for web browsers.  Incredibly slow implementations, restricted to interacting with the just the browser.  No debuggers, no tools, no software engineering support to speak of.&lt;br /&gt;&lt;br /&gt;Then V8 came along and showed people that it can be much faster. Lars’ has doubled performance since the release in September, and expects to double it again within a year, and again 2-3 years after that.&lt;br /&gt;&lt;br /&gt;Javascript security and modularity are evolving as well. By the time that’s done, I suspect it will far outstrip clunky packaging mechanisms like OSGi.  This doesn’t mean you have to write everything in Javascript - I don’t believe in a monolingual world.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: Yes, I did spend ten years at Sun. A lot of it was spent arguing that Java was not the final step in the evolution of programming languages. I guess I’m just not very persuasive.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The web browser itself is one of the most disruptive technologies in computing. It is poised to become the OS. Javascript performance is an essential ingredient but there are others. SVG provides a graphics model.  HTML 5 brings with it persistent storage - a file system, as it were - only better. You may recall that Vista was supposed to have a database like file system. Vista didn’t deliver, but web browsers will.&lt;br /&gt;&lt;br /&gt;This trend seems to make the traditional OS irrelevant. Who cares what’s managing the disk drive? It’s just some commoditized component, like a power supply. We can already see how netbooks are prospering. This isn’t good news for Windows.&lt;br /&gt;&lt;br /&gt;Of course, you might ask why Microsoft would go along with this? Is IE’s Javascript so inadequate on purpose? Maybe it is part of the master plan? Well, I don’t believe in conspiracy theories.&lt;br /&gt;&lt;br /&gt;It does look as if Microsoft is facing a lose-lose proposition. Making IE competitive supports the movement toward a web-browser-as-OS world. Conversely, if they let IE languish, as they have, they can expect its market share to continue to drop.&lt;br /&gt;&lt;br /&gt;However, you cannot stop the trend toward the Web OS by making your tools inferior. You can only compete by innovating, not by standing still.&lt;br /&gt;&lt;br /&gt;I wouldn’t count Redmond out just yet. They have huge assets - a terrific research lab, armies of smart people in product land as well, market dominance, and vast amounts of money. They also have huge liabilities of course - and those add up to innovator’s dilemma.&lt;br /&gt;&lt;br /&gt;In any case, for language implementors, it’s clear that one needs to be able to compile to the internet platform and Javascript is its assembly language. Web programming will evolve into general purpose programming, using the persistent storage provided by the browser to function off line as well as online.&lt;br /&gt;&lt;br /&gt;As many readers of this blog will recognize, this sets the stage for the brave new world of objects as software services. I hope to bring Newspeak to the point where it is an ideal language for this coming world. It’s a bit difficult with the very limited resources at our disposal, but we are making progress.&lt;br /&gt;&lt;br /&gt;The entire progression from conventional computing to the world of the Web OS is taking longer than one expects, but is happening. The time will come when we hit the tipping point, and things will change very rapidly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-6796281891265227069?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/6796281891265227069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=6796281891265227069' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6796281891265227069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6796281891265227069'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/04/last-week-i-was-at-lang.html' title='The Language Designer’s Dilemma'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-7076023764071614736</id><published>2009-03-29T12:44:00.000-07:00</published><updated>2009-03-30T07:54:38.517-07:00</updated><title type='text'>Subsuming Packages and other Stories</title><content type='html'>Barbara Liskov recently won the Turing award. It’s great to see the importance of programming language work recognized once again. Even if you aren’t into the academic literature, you have probably heard her name, for example, when people mention the Liskov substitution principle (LSP). The LSP  states that if a property P is provable for an object of type S, and T is a subtype of S, then P is provable for an object of type T.&lt;br /&gt;&lt;br /&gt;This is behavioral subtyping - instances of subtypes are semantically substitutable where supertypes are expected. This is a very strong requirement, but not something a practical programming language can enforce.&lt;br /&gt;&lt;br /&gt;Closely related (and enforceable)  is the subsumption rule of type systems for OO languages. This rule states that if an expression e has type T, and T is a subtype of S, then e has type S as well.  This post is about the importance of subsumption in programming language design.&lt;br /&gt;&lt;br /&gt;The subsumption rule is a cornerstone of formal type systems for OO. More importantly, it captures a deep intuition that programmers have about subtyping. After all, what could be more natural: if e: T and T &lt;: S, e: S. I hope readers agree with me that this property is intuitive, perhaps even so obvious that you wonder why anyone would bother belaboring the point.   ***   So it is most unfortunate that mainstream programming languages violate subsumption left and right. Below, we’ll look at some examples, and draw some conclusions about language design.  &lt;span style="font-weight: bold;"&gt;Case 1: Hiding fields.&lt;/span&gt; In Java (and C++) you can define a field in a subclass with type X, even though the superclass has a field of the same name, with unrelated type Y. The subclass’ field &lt;span style="font-style: italic;"&gt;hides&lt;/span&gt; the superclass field.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; S { &lt;span style="font-weight: bold;"&gt;final&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; f = 42;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; T &lt;span style="font-weight: bold;"&gt;extends&lt;/span&gt; S { &lt;span style="font-weight: bold;"&gt;final&lt;/span&gt; String f = “!”;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;new&lt;/span&gt; T().f;  // “!”&lt;br /&gt;((S)&lt;span style="font-weight: bold;"&gt; new&lt;/span&gt; T()).f; // 42&lt;br /&gt;&lt;br /&gt;In case it isn’t obvious, any object of type S has the property that it’s &lt;span style="font-style: italic;"&gt;f&lt;/span&gt; field has type &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt;. An instance of T should also have the same property, but doesn’t. To get at the field defined by S, we have to coerce the T value into an S.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: It would be much better to ensure that fields are always private, or better yet, avoid field references altogether, as in Self or Newspeak.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A typical programmer in the Java/C++ tradition may ask what’s the harm in the rules as they are.&lt;br /&gt;&lt;br /&gt;The harm is precisely in having rules that contradict strong and valid intuition. The contradiction will bite you in the end, because you will end up thinking some program property holds based on your intuition, while the rules of the language contradict that intuition. Years of exposure to these misfeatures may have inured the mind to their toxicity, but they are toxic all the same.&lt;br /&gt;&lt;br /&gt;However, there is much more harm, as we’ll see as this post progresses.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Case 2: Hiding static methods.&lt;/span&gt;  The same sort of situation occurs with static methods. Static methods aren’t really methods at all, as no run time object is involved in determining the meaning of their invocations. No overriding can occur between static methods. The meaning of a static method is statically bound: static method is a misnomer for subroutine (as in FORTRAN subroutine circa 1955). And so Java allows static methods to hide each other much like fields.  You can easily recreate the above example with static methods.&lt;br /&gt;&lt;br /&gt;I’ve written elsewhere about the problems of all things &lt;span style="font-weight: bold;"&gt;static&lt;/span&gt;; add this to the list. Maybe you’re beginning to get the sense that violating subsumption correlates with iffy language constructs.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Case 3: Package privacy.&lt;/span&gt; This brings us back to Barbara Liskov. Her work on CLU in the mid 70s brought David Parnas’ notion of information hiding into programming languages, with support for abstract datatypes (ADTs). Packages are one of the mechanisms that Java uses to support ADTs. Packages have had all kinds of problems; lack of subsumption is at the root of many of them.&lt;br /&gt;&lt;br /&gt;For example, recall that in Java, protected access implies package access. Package access is always relative the the current package. So when a protected member is inherited by a subclass in another package, it eliminates the access from the superclass package (while granting access to its own package):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;package&lt;/span&gt; P1;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; A {&lt;span style="font-weight: bold;"&gt;protected&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; foo(){&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; 91;}}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; C {&lt;span style="font-weight: bold;"&gt;static&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; i = &lt;span style="font-weight: bold;"&gt;(new&lt;/span&gt; P2.B()).foo();}&lt;br /&gt;&lt;br /&gt;**&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;package&lt;/span&gt; P2;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; B &lt;span style="font-weight: bold;"&gt;extends&lt;/span&gt; P1.A {}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This code is legal. If we then change B such that&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class&lt;/span&gt; B &lt;span style="font-weight: bold;"&gt;extends&lt;/span&gt; P1.A {&lt;span style="font-weight: bold;"&gt;protected&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;int&lt;/span&gt; foo() {&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; 42;}}&lt;br /&gt;&lt;br /&gt;The call in C is no longer allowed!  We have not added or removed members in the subclass, or changed their accessibility (or have we? Did we have a choice?) and yet we’ve broken client code.&lt;br /&gt;&lt;br /&gt;I don’t want to go into the details of an even more bizarre &lt;a href="http://bracha.org/x-pkg.rtf"&gt;example&lt;/a&gt; (due, as far as I can recall, to David Chase) - but &lt;a href="http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/"&gt;others have&lt;/a&gt;. Suffice to say that there was a huge bug tail around such examples; it took years to resolve them. Compiler engineers did not correctly diagnose the problems, because their intuition led them to try and preserve subsumption, to no avail. I have several patents to my name that deal with the mechanisms of implementing the desired behavior.&lt;br /&gt;&lt;br /&gt;And why should you care?  Because these issues led to subtle incompatibilities between compilers and VMs, across vendors and releases; these led to subtle program bugs, to jokes about &lt;span style="font-style: italic;"&gt;write once debug everywhere&lt;/span&gt; etc.&lt;br /&gt;&lt;br /&gt;Packages are one way in which Java supports ADTs.  It turns out that you cannot have ADTs, inheritance and subsumption in one language. You must make a choice - and I argue that subsumption must always be preserved.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Case 4: Class-based Encapsulation.&lt;/span&gt;&lt;br /&gt;Another mechanism that supports ADTs in all the mainstream OO languages is class based encapsulation. This the idea that privacy is per class type, not per object. This makes it possible to write code like&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; C {&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;private int&lt;/span&gt; f(){&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; 42;}&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;public int&lt;/span&gt; foo(C c){&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; c.f();} // I can get at c’s f(), because we’re both C’s&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;It also makes it possible to violate subsumption:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class &lt;/span&gt;A {&lt;br /&gt;   {&lt;br /&gt;   ((A)&lt;span style="font-weight: bold;"&gt; new&lt;/span&gt; B()).f(); // 42&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;new&lt;/span&gt; B().f(); // “!”&lt;br /&gt;   }&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;private int&lt;/span&gt; f(){&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; 42;} // package private&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;public class &lt;/span&gt;B &lt;span style="font-weight: bold;"&gt;extends&lt;/span&gt; A {&lt;br /&gt;   &lt;span style="font-weight: bold;"&gt;public&lt;/span&gt; String f(){&lt;span style="font-weight: bold;"&gt;return&lt;/span&gt; “!”;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;An alternative to class-based encapsulation is &lt;span style="font-style: italic;"&gt;object-based encapsulation&lt;/span&gt;. Privacy is per object.  You can enforce it via context free syntax. For example, you can arrange that only method invocations on &lt;span style="font-weight: bold;"&gt;self&lt;/span&gt; (aka &lt;span style="font-weight: bold;"&gt;this&lt;/span&gt;) attempt to lookup private methods. You don’t need a typechecker (aka verifier) to prevent malicious parties accessing private methods or fields. Consequently, you don’t have to rely on a complex byte code verifier to prevent such access as one does in Java and .Net.&lt;br /&gt;&lt;br /&gt;This fits perfectly with the object-capability security model. It is essentially what we do in Newspeak; but the ideas are universal.&lt;br /&gt;&lt;br /&gt;Oh, and object-based encapsulation works with both subsumption and inheritance.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Case 5: Privileged access within “nests” of classes.&lt;/span&gt; Giving enclosing classes privileged access to their nested classes, and giving sibling classes privileged access to each other, is another violation of subsumption (but giving a nested class free access to its surrounding scope, including private elements of its enclosing class, is fine).&lt;br /&gt;&lt;br /&gt;Another way to look at all this is that object-based encapsulation falls out naturally from the use of interfaces. Consider what would happen if the type C above was, by language definition, the public interface of C (as it is in Strongtalk, for example). Subsumption would fall out automatically.&lt;br /&gt;&lt;br /&gt;Subsumption is a very good litmus test for language constructs; if a construct violates subsumption, it will cause grief. Ditch it.&lt;br /&gt;&lt;br /&gt;Strict adherence to subsumption reduces cognitive dissonance for programmers, compiler writers, VM implementors and language designers. It helps avoid bugs in user code, compilers and VMs, enhancing reliability and portability. It makes it easier to build secure systems.&lt;br /&gt;&lt;br /&gt;So if you design a type system for an OO language, preserve subsumption at all costs. Do not try and combine ADTs with inheritance (because then you lose subsumption).&lt;br /&gt;&lt;br /&gt;Now if we can’t have ADTs and inheritance, we have to chose between them. I believe the benefits of inheritance far outweigh the costs. Those who feel differently are unlikely to do better than &lt;a href="http://lucacardelli.name/"&gt;Luca Cardelli&lt;/a&gt;‘s sublimely beautiful &lt;a href="http://lucacardelli.name/Papers/TypefulProg.pdf"&gt;Quest&lt;/a&gt;. On the other hand, if one choses inheritance, one should go with object-based encapsulation.&lt;br /&gt;&lt;br /&gt;Put another way, if you take the adage &lt;span style="font-style: italic;"&gt;program to an interface, not an implementation&lt;/span&gt; seriously, it will prevent a host of problems. This is what we are doing with Newspeak.&lt;br /&gt;&lt;br /&gt;At a meta-level, the lesson is: pay attention to programming language theory; occasionally, you can learn from it.&lt;br /&gt;&lt;br /&gt;Update: Yardena pointed out &lt;a href="http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/"&gt;this discussion of Java packages&lt;/a&gt;. It goes through the problems in detail. I've added a link to it in the main post as well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-7076023764071614736?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/7076023764071614736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=7076023764071614736' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7076023764071614736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7076023764071614736'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/03/subsuming-packages-and-other-stories.html' title='Subsuming Packages and other Stories'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-142319848391306271</id><published>2009-02-27T23:30:00.000-08:00</published><updated>2009-02-27T23:41:23.496-08:00</updated><title type='text'>Newspeak Prototype Escapes into the Wild</title><content type='html'>The Newspeak prototype is now available at &lt;a href="http://newspeaklanguage.org/downloads/"&gt;http://newspeaklanguage.org/downloads/&lt;/a&gt; . We had planned to release it in early January, but decided to complete a few more things - like better source control support, a fully functional Hopscotch based debugger and a new GUI for unit testing (shown below).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_k3ghkt1e0I8/SajovPJraDI/AAAAAAAAAEA/uQcp6ELtWKE/s1600-h/testResults2.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 395px;" src="http://2.bp.blogspot.com/_k3ghkt1e0I8/SajovPJraDI/AAAAAAAAAEA/uQcp6ELtWKE/s400/testResults2.png" alt="" id="BLOGGER_PHOTO_ID_5307748059074750514" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Newspeak is a long way from being finished - we are less than half way to realizing the vision of a robust, high performance, secure, network aware, high productivity platform. We can only get there if people care enough to use it and contribute to it.  I hope you'll check it out.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-142319848391306271?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/142319848391306271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=142319848391306271' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/142319848391306271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/142319848391306271'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/02/newspeak-prototype-escapes-into-wild.html' title='Newspeak Prototype Escapes into the Wild'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_k3ghkt1e0I8/SajovPJraDI/AAAAAAAAAEA/uQcp6ELtWKE/s72-c/testResults2.png' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-1728887574143465074</id><published>2009-01-13T10:43:00.000-08:00</published><updated>2009-01-13T10:46:43.448-08:00</updated><title type='text'>Apologia</title><content type='html'>Here’s a quick update on the status of the Newspeak prototype release. If anyone is keeping score - I had said we’d put something out in the first week of January 2009.  So we’re behind schedule. I apologize, but what are you going to do - cut my funding?&lt;br /&gt;&lt;br /&gt;Nevertheless, we will be putting a prototype out soon. I’m holding off so that a few small things can be added.&lt;br /&gt;&lt;br /&gt;In the meantime, I wanted to take the opportunity to say a bit about the prototype. Obviously, due to our reduced circumstances, it is a lot less ambitious than I had once hoped.&lt;br /&gt;&lt;br /&gt;We do have a native GUI binding on Windows; I had hoped to have native GUI bindings for Mac and Linux, but that will depend on future open source contributions. You can of course run the system on those platforms, but you’ll have to live with the Morphic binding for now.&lt;br /&gt;&lt;br /&gt;The language itself remains incomplete as well - but the key features are there. I expect to rev the syntax and add the missing features (like object literals) over time. One of the reasons these features have been delayed is that I want to add them in the context of a completely new compiler.  Such a compiler will also make it easier to do ports - e.g., Newspeak on V8.&lt;br /&gt;&lt;br /&gt;One of the biggest deficits is the lack of libraries written in Newspeak. We continue to rely heavily on the existing Squeak libraries. We have made some progress on this front, largely through the efforts of some volunteers who got early access to the system. I want to take this opportunity to thank Yardena Meymann, David Pennell and Stephen Pair for their work on the library port. They are all busy professionals who were willing to take the time to do something concrete to help move Newspeak forward. I hope that we’ll see a lot more of that after the release!&lt;br /&gt;&lt;br /&gt;What we’ve been doing is porting some of the core libraries from Strongtalk to Newspeak. These libraries have several important advantages: they are small, yet complete enough to run a real system; they are blue book compatible (give or take), so we have a good chance of replacing our uses of Squeak code with them, without excessive disruption; they are quite cleanly written; they have type annotations; and, it so happens, they have a liberal license.&lt;br /&gt;&lt;br /&gt;On the other hand, these libraries were not purpose built for Newspeak; no thought has been given to security, the designs do not leverage features like mixins as much as one might etc..  Still, they provide us the with the most realistic path to getting a small stand alone system in the foreseeable future.&lt;br /&gt;&lt;br /&gt;This library code will not be anywhere near complete and integrated when we put out the prototype. But it whatever we have will be available, and we’ll move on from there.&lt;br /&gt;There are any number of other things lacking, and no doubt many will be eager to point them out. However, that scarcely matters. What matters is the &lt;span style="font-style: italic;"&gt;potential&lt;/span&gt; this system has. It’s being put out, so those with the imagination, sophistication and, above all, &lt;span style="font-weight: bold;"&gt;taste&lt;/span&gt; to appreciate it can start using it and contributing to it - eventually realizing that potential.&lt;br /&gt;&lt;br /&gt;So for that small elite that has shown interest and appreciation for Newspeak so far - thanks, and hang in there. It will be available, Real Soon Now :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-1728887574143465074?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/1728887574143465074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=1728887574143465074' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1728887574143465074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1728887574143465074'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2009/01/apologia.html' title='Apologia'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-9220526657191250140</id><published>2008-12-09T19:07:00.000-08:00</published><updated>2008-12-09T19:26:50.755-08:00</updated><title type='text'>Living without Global Namespaces</title><content type='html'>Newspeak differs from most programming languages in that it doesn’t provide a global namespace. And it differs from most imperative programming languages, because it has no static state.&lt;br /&gt;&lt;br /&gt;I’ve spoken and written a fair amount about &lt;a href="http://gbracha.blogspot.com/2008_02_01_archive.html"&gt;why the absence of static state is a good thing&lt;/a&gt; . What I haven’t discussed much is how you actually organize programs in this way. There have been a lot of questions along these lines. This post is an attempt to answer some of them.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Caveat&lt;/span&gt;: Some of the details here differ in the current prototype.  Some of the features are still incomplete. What's described here is how things are supposed to work. We're not far from that.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First, let’s tackle the question of static state. It should be obvious: anything that you expected to put in a static variable goes in an instance variable of a module. What about singleton classes? How do I ensure that there’s only one instance? The easiest way is to initialize a read only slot of a module with an object literal. What happens if there are multiple instances of the module declaration? Well, each module has its own “singleton”. That’s exactly what happens with singleton classes in Java when they are defined by multiple class loaders.&lt;br /&gt;&lt;br /&gt;What if your class defines some service process and you need to be really sure there’s only one in the entire system? First, in many cases you may find that the system in question is your subsystem, defined by your modules, and the answer above applies.&lt;br /&gt;&lt;br /&gt;Now if you really mean “the entire system”, then you need to control that via some state in the platform object - through its links to the world’s state (e.g., the file system) or by having some registry in the platform object. Of course, not all code may see the true platform object, so it isn’t really global either; but it won’t matter.&lt;br /&gt;&lt;br /&gt;Having no static state doesn’t preclude having a global namespace, as long as that namespace doesn’t contain any stateful objects. The original plan for Newspeak was to have a global namespace of pure values, structured as an inversion of the internet domain namespace. This would have been much like the convention for naming Java packages (except that the scopes of namespaces would nest properly, as you’d expect). It was the only idea from Java that I saw a use for in Newspeak. It’s a good idea, but it turns out to be unnecessary.&lt;br /&gt;&lt;br /&gt;So, given no global namespace, what can I write at the top level? Remember, I can’t refer to any names, even things like &lt;span style="font-style: italic;"&gt;Object&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;String&lt;/span&gt; that presumably exist in every implementation. This seems awkward. Not to worry - we won’t be writing SKI combinators or even plain old lambdas.&lt;br /&gt;&lt;br /&gt;We might be able to write some literal expressions like 1 + 2, but that isn’t all that interesting, and isn’t even necessary. What we need to write are things that produce new kinds of objects, like classes.&lt;br /&gt;&lt;br /&gt;Happily, we can write a a top level class declaration, with one caveat: A top level class declaration cannot declare a superclass explicitly since there is no way to name it, because there is no enclosing namespace.  In that case, by special dispensation, the superclass will be the class &lt;span style="font-style: italic;"&gt;Object&lt;/span&gt; provided by the underlying platform. Similar rules apply to object literals (which can be thought of as “anonymous classes done right”).&lt;br /&gt;&lt;br /&gt;Ok, so now we can write a class, which can have other classes nested inside it, so it can be an entire library; and since there is no surrounding namespace, it is necessarily independent of any specifics of the environment - it is a module declaration. An example of such a module declaration would be the Newspeak AST&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; NewspeakAST usingLib: platform { ....&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;... lots of nested AST classes ....&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A similar class would be &lt;span style="font-style: italic;"&gt;CombinatorialParsing&lt;/span&gt; library I’ve written about before.&lt;br /&gt;&lt;br /&gt;There’s just one little problem. How do I use such a class? I gave it a name, but no one can refer to it, since there isn’t any surrounding namespace for the name to be bound!&lt;br /&gt;&lt;br /&gt;Suppose I want to create a parser that builds an AST, using the two classes mentioned above. I need a grammar, which should be defined by a subclass of the parser library, and the parser class itself would in turn be a subclass of the grammar. Call these classes &lt;span style="font-style: italic;"&gt;Grammar&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;Parser&lt;/span&gt;.&lt;br /&gt;Since I can’t name the superclass of &lt;span style="font-style: italic;"&gt;Grammar&lt;/span&gt;, I’ll just define it as a mixin, and worry about how to pair it with the superclass later.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;class&lt;/span&gt; Grammar  =  { ....}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Likewise with &lt;span style="font-style: italic;"&gt;Parser&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;class&lt;/span&gt;&lt;span style="font-style: italic;"&gt; Parser usingLib: platform astLib: ast = { ...}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That way I can define all the actual code required. The problem remaining is how to link all these pieces together.&lt;br /&gt;&lt;br /&gt;If I actually had a namespace where I could refer to the pieces, I could write linking code like:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;“confused”&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;main: platform {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    MyGrammar = Grammar |&gt; CombinatorialParsing usingLib: platform.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    MyParser = Parser usingLib: platform astLib: NewspeakAST  |&gt; MyGrammar.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;    return:: MyParser  parse: ‘a string in my language, perhaps?’&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So how would I go about creating such a namespace? This is ultimately a question of tooling. Suppose my IDE lets me load class objects dynamically - say by reading in serialized class objects saved in files on disk. When it loads such a class object, it can reflect on it to find out its name, and store the class object in a slot of the same name in some new object it creates.&lt;br /&gt;&lt;br /&gt;If I choose to load the classes, &lt;span style="font-style: italic;"&gt;Grammar&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;Parser&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;CombinatorialParsing&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;NewspeakAST&lt;/span&gt;, I can create an object that is precisely the namespace I needed. I can then modify its class by adding the &lt;span style="font-style: italic;"&gt;main:&lt;/span&gt; method listed above. This object is now an application, whose behavior is defined by its &lt;span style="font-style: italic;"&gt;main:&lt;/span&gt; method. I can serialize this application object to disk.&lt;br /&gt;&lt;br /&gt;Running my program then amounts to deserializing the object, and invoking its &lt;span style="font-style: italic;"&gt;main:&lt;/span&gt; method with an object representing the current platform.&lt;br /&gt;&lt;br /&gt;I’ve glossed over some crucial details here. We don’t really want to serialize the entire object, as it points to objects in our IDE, like &lt;span style="font-style: italic;"&gt;Object&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;Class&lt;/span&gt; and a few others. These are standard, and we can cut off the object graph  with symbolic links at these standard points, and have the deserializer hook up their equivalents on the destination.&lt;br /&gt;&lt;br /&gt;Is using the IDE this way cheating? After all, it ultimately resorts to using the namespace of the underlying file system (or the network, or a global IDE namespace, depending where the IDE fetches class objects from).  I think not. The truth is that this is what any language in the world does at some level. Whether we rely on a compiler that uses a &lt;span style="font-style: italic; font-weight: bold;"&gt;CLASSPATH&lt;/span&gt; environment variable to define a set of local directories, or on the IDE, or on makefiles in a given directory to link separately compiled files, it is ultimately the same: some tool uses the operating system to find pieces of program.&lt;br /&gt;&lt;br /&gt;We don’t have to use the IDE; we could use a preprocessor that understood directives that referred to classes in the file system instead. It could even use something as inane as &lt;span style="font-weight: bold; font-style: italic;"&gt;CLASSPATH&lt;/span&gt;. Of course, I’m not really recommending that.&lt;br /&gt;&lt;br /&gt;My key point is that the language needs nothing more than objects to serve as its namespaces.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-9220526657191250140?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/9220526657191250140/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=9220526657191250140' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/9220526657191250140'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/9220526657191250140'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/12/living-without-global-namespaces.html' title='Living without Global Namespaces'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-6152967728783830038</id><published>2008-12-05T15:14:00.000-08:00</published><updated>2008-12-05T15:58:31.652-08:00</updated><title type='text'>Unidentified Foreign Objects  (UFOs)</title><content type='html'>I recently found out that Newspeak’s basic foreign function interface (FFI), called  &lt;span style="font-weight: bold;"&gt;Aliens&lt;/span&gt;, is being &lt;a href="http://wiki.squeak.org/squeak/6100"&gt;made available in Squeak&lt;/a&gt; (though that will require new VMs with the required primitives).  Thanks to John McIntosh for doing this.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I should also thank Eliot Miranda for most of the original work on aliens, and  Vassili Bykov, Peter Ahe and Bill Maddox for the rest. Also thanks to Lars Bak, whose work on the Strongtalk FFI inspired the VM level view of aliens; and to Dave Ungar,  who was the first to understand that objects were all you needed on the language side of an FFI. Lastly, this post benefited immensely from conversations with Vassili.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So I figured I’d write a little bit about Aliens from a high level perspective. As usual, the ideas apply to programming languages in general.&lt;br /&gt;&lt;br /&gt;In Smalltalk,  there isn’t a standard FFI. Various dialects provide different solutions, with varying degrees of functionality, performance and ease of use. To be honest, they are usually a poor fit with the surrounding language and fairly awkward to use. This inhibits Smalltalk’s interoperability with the rest of the world. I’d argue that the absence of a good, standard FFI has cost the Smalltalk community dearly.&lt;br /&gt;&lt;br /&gt;In Java, by contrast, native methods and JNI provide a standardized FFI. This mechanism is far from perfect, but at least there is a more or less standard solution.&lt;br /&gt;&lt;br /&gt;What these and other systems have in common is support for a special construct (such as the native modifier for methods, or declarations like &lt;span style="font-weight: bold;"&gt;extern C&lt;/span&gt;, or the truly ugly ad hoc FFI syntax extensions used in various Smalltalks) for foreign functions.&lt;br /&gt;&lt;br /&gt;Newspeak’s FFI was strongly influenced by the Strongtalk FFI; but unlike Strongtalk, Newspeak doesn’t have a special syntax for foreign calls. As &lt;span style="font-weight: bold;"&gt;Self&lt;/span&gt; showed many years ago, one doesn’t really need a special syntax for the FFI.  The foreign functions, APIs, DLLs etc. can all be represented as objects. They just happen to be &lt;span style="font-style: italic;"&gt;foreign&lt;/span&gt; objects.&lt;br /&gt;&lt;br /&gt;The idea of a foreign object, which we call an &lt;span style="font-weight: bold; font-style: italic;"&gt;alien&lt;/span&gt;, is at the foundation of the Newspeak FFI.&lt;br /&gt;&lt;br /&gt;For starters, any decent language should be able to represent functions as values; and in an object-oriented language, these values are objects, accessed via a standard interface. Foreign functions are just a different implementation of that interface.&lt;br /&gt;&lt;br /&gt;Another natural way to model a foreign function is as a method defined on a foreign object.  For example, one can view an entire DLL as an object with a set of methods corresponding to the functions defined by the DLL. Better yet, we could represent an entire API as an object, independently of what DLLs actually defined it.&lt;br /&gt;&lt;br /&gt;Aliens can be defined for different foreign languages; for example, while &lt;span style="font-weight: bold;"&gt;Alien&lt;/span&gt; is used to interface with C, we also have a class called &lt;span style="font-weight: bold;"&gt;ObjectiveCAlien&lt;/span&gt; that can be used to interface with ObjectiveC, which is the native language on MacOS X.  C Aliens and ObjectiveC Aliens do not interfere with each other, and when/if we need to add Java Aliens or CLR Aliens we can do that as well.&lt;br /&gt;&lt;br /&gt;The alien approach is also a good fit with security: one need not be concerned that code may bypass high level language safety guarantees by calling out to C; untrusted code can be prevented from doing that, simply by not providing any Alien library objects to it.&lt;br /&gt;&lt;br /&gt;Newspeak’s C Alien implementation is fast, but also dangerous. An alien is basically a blob of memory. The user of an Alien is responsible for interpreting and accessing that data correctly.  There is no checking being done for you.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: It's  worth noting that the basic Alien layer may evolve further; for example, we aren't thrilled with the practice of subclassing Alien. It's not clear if the Alien class really needs to change, or just the pattern of using it.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;On top of this foundation, safer and/or more convenient abstractions can be built.  We have built objects that support not just methods corresponding to the functions of an API, but also methods that provide factories for the various datatypes used in the function’s signatures, including those defined by macros. These objects wrap the basic alien API, and help with error prone book keeping - converting between Newspeak types (e.g., Strings) and foreign types, freeing aliens after use etc.&lt;br /&gt;&lt;br /&gt;At the moment, both the declarations of low level aliens and higher level APIs are constructed manually, which is tedious and  error prone. We’ve been planning on a higher level tool called CSlick, which would allow you to specify a set of .h files and the requisite DLLs, and obtain an object that supports the desired functions automatically.&lt;br /&gt;&lt;br /&gt;As a first approximation, you could think of CSlick as a function:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;CSlick: List&lt;headerfiles&gt; -&gt; List&lt;dll&gt; -&gt; ForeignAPI&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The signature above is deliberately curried, because you may actually want to be able to specify just the header files, and later bind different DLLs to provide the actual functionality, just as a .h file can be associated with different .c files.&lt;br /&gt;&lt;br /&gt;When this will happen is anyone’s guess right now; but &lt;a href="http://blog.3plus4.org/"&gt;Vassili&lt;/a&gt; has done this before (in the context of Lisp) and I’m sure he can do it again.&lt;br /&gt;&lt;br /&gt;The resulting foreign API should incorporate the low level alien API, and, as much as possible, a higher level API as well.&lt;br /&gt;&lt;br /&gt;The CSlick implementation will need to know how to parse C header files, and how to reflectively manufacture the low level code that actually invokes the C functions. Fortunately we have a strong parsing infrastructure, so that isn’t as daunting as it sounds.&lt;br /&gt;&lt;br /&gt;When I’ve told people about CSlick, they often mention SWIG. However, I believe CSlick can be made substantially easier to use than SWIG. SWIG has to cope with multiple languages, each with a pre-existing story on how to do foreign calls. In contrast, we can integrate CSlick more tightly with the language. Ultimately, that should translate to a simpler model for the user.&lt;br /&gt;&lt;br /&gt;The key take away is that objects are all you really need to interact with foreign programming languages.  They are better than built in language constructs in terms of ease of use, security, and multiple language support. As usual, less is more.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-6152967728783830038?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/6152967728783830038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=6152967728783830038' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6152967728783830038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6152967728783830038'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/12/unidentified-foreign-objects-ufos.html' title='Unidentified Foreign Objects  (UFOs)'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5192679980025218518</id><published>2008-11-24T17:11:00.000-08:00</published><updated>2008-11-24T17:51:45.800-08:00</updated><title type='text'>We have Good news, and we have Bad news</title><content type='html'>First, the good news. We will be releasing a Newspeak prototype in the first week of January 2009. This prototype is anything but finished, but it will have to do, because of the bad news (insert dramatic pause here).&lt;br /&gt;&lt;br /&gt;What's the bad news? Well, funding for the Newspeak team is being discontinued in early January, another victim of the times.&lt;br /&gt;&lt;br /&gt;We are currently seeking a new home for Newspeak, but it is by no means certain that such a thing can be found in the current economic climate.  Perhaps I should take the Newspeak private jet to Washington and demand a bail-out - but alas, the jet is indisposed (when I call it all I  get is &lt;span style="font-style: italic;"&gt;message not understood&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;We expect to keep pushing the Newspeak platform forward in any event; that said, there's a big difference between having several developers fully dedicated to a project, and the limited efforts people can make in their spare time. So the goal is to find support for the project soon - otherwise we'll all go get other jobs and progress on Newspeak will slow down accordingly.&lt;br /&gt;&lt;br /&gt;Of course, once we put out the prototype, others can help fill in the blanks. I hope that will happen, irrespective of whether Newspeak gets further funding. I also plan to write up our work on Newspeak for academic publication.&lt;br /&gt;&lt;br /&gt;Hopefully, this is only a temporary setback, and in it lies new opportunity: &lt;span style="font-style: italic;"&gt;per aspera ad astra&lt;/span&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5192679980025218518?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5192679980025218518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5192679980025218518' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5192679980025218518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5192679980025218518'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/11/we-have-good-news-and-we-have-bad-news.html' title='We have Good news, and we have Bad news'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-685389284173953354</id><published>2008-11-01T20:29:00.000-07:00</published><updated>2008-11-01T21:01:29.055-07:00</updated><title type='text'>Dynamic IDEs for Dynamic Languages!</title><content type='html'>Dynamic languages are all the rage these days, and rightly so. Historically, most of the currently popular dynamic languages have had negligible IDE support (since they are scripting languages).  More recently, we see a flurry of activity around IDE support for these languages. However, the mainstream IDEs are designed for, and implemented in, static languages. Consequently, even when dealing with a dynamic language, the dynamism stops when it comes to the IDE itself.&lt;br /&gt;&lt;br /&gt;What do I mean by this? In an IDE written in a dynamic language (such as Smalltalk or Self or Lisp), the IDE code itself can be modified on the fly. This is a double edged sword, as I’ll explain shortly. I’ll start with the good edge.&lt;br /&gt;&lt;br /&gt;Suppose you want to modify the IDE for some reason. It lacks a feature you need, or something is buggy. If it’s a proprietary system, you can file a bug with the vendor, and hope that they pay attention, so that in the next release, in a year or two, you’ll get the fix.&lt;br /&gt;&lt;br /&gt;If the system is open source, you can just go ahead and implement the fix/feature. Now if that system is written in a mainstream, conventional language, you just need to recompile the compilation unit with the fix, and rebuild your system.  Of course, in order to develop the fix, you had to load a project with the source for your IDE and probably go through several edit-compile-debug cycles. When it all worked, you replace your existing installation with your new build. I have the strange feeling that all this setting up the IDE as a project within itself, rebuilding and reinstalling could take quite a lot of time and effort.&lt;br /&gt;&lt;br /&gt;In an IDE like Smalltalk (or Newspeak, or Self), the IDE source is always available from within itself.  In such a system, if you change its source code, the change takes effect immediately. For example, the Newspeak class browser  supports a pop up menu for methods, that lets you find references to the method and to the messages sent within it.  In the screen shot below, the speech bubble on the right denotes this menu&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_k3ghkt1e0I8/SQ0gTpFLsrI/AAAAAAAAADY/TSVnK1_vv-U/s1600-h/inspectPresenter0.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 55px;" src="http://1.bp.blogspot.com/_k3ghkt1e0I8/SQ0gTpFLsrI/AAAAAAAAADY/TSVnK1_vv-U/s400/inspectPresenter0.png" alt="" id="BLOGGER_PHOTO_ID_5263899061283173042" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;img src="file:///Users/gilad/Library/Caches/TemporaryItems/moz-screenshot.jpg" alt="" /&gt;&lt;br /&gt;&lt;br /&gt;However, the initial version did not support the same functionality for slots.&lt;br /&gt;How do we fix this? Well, for starters, we need to find where the relevant source code is. How do you do this? The old fashioned way is to go on a trek through the source code trying to figure it out. What subdirectory should we start with? Which file should we open first? What should we grep for?&lt;br /&gt;&lt;br /&gt;Fortunately, Newspeak makes it easy, as most IDE components have an &lt;span style="font-style: italic;"&gt;Inspect Presenter&lt;/span&gt; menu item, that will open an object inspector on the actual presenter in question; a presenter on a presenter.&lt;br /&gt;&lt;br /&gt;For example, if we want to find out how the references menu for methods works, we can look at a method  like the one above. On the far right is a drop down menu:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_k3ghkt1e0I8/SQ0gotzDWkI/AAAAAAAAADg/OVd9k8JIruI/s1600-h/inspectPresenter1.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 194px; height: 100px;" src="http://1.bp.blogspot.com/_k3ghkt1e0I8/SQ0gotzDWkI/AAAAAAAAADg/OVd9k8JIruI/s400/inspectPresenter1.png" alt="" id="BLOGGER_PHOTO_ID_5263899423326558786" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This opens an object presenter, like this:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_k3ghkt1e0I8/SQ0gyhOkY1I/AAAAAAAAADo/g7QepAEsAb8/s1600-h/inspectPresenter2.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 328px;" src="http://2.bp.blogspot.com/_k3ghkt1e0I8/SQ0gyhOkY1I/AAAAAAAAADo/g7QepAEsAb8/s400/inspectPresenter2.png" alt="" id="BLOGGER_PHOTO_ID_5263899591751000914" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Toward the top, is a line marked class; it contains a hyperlink to the class of the object being inspected - the class &lt;span style="font-style: italic;"&gt;ExpandableMethodPresenter&lt;/span&gt;. Now we know where the code that presents methods resides! If we click on the link, the Hopscotch browser will show us the class.&lt;br /&gt;&lt;br /&gt;Having found the code that manages the presentation of methods, and the implementation of the references menu, we next want to find the code that presents slots, so we can modify it. We do the same thing again - invoke the &lt;span style="font-style: italic;"&gt;Inspect Presenter&lt;/span&gt;  menu item, but this time on a slot.&lt;br /&gt;&lt;br /&gt;Once we’ve found the code and made the change, we can test it right away. Just hit refresh in the browser, and you’ll see the new reference bubble next to slots.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_k3ghkt1e0I8/SQ0jG3N8-wI/AAAAAAAAAD4/lMdnRrfN-Vg/s1600-h/header.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 154px;" src="http://3.bp.blogspot.com/_k3ghkt1e0I8/SQ0jG3N8-wI/AAAAAAAAAD4/lMdnRrfN-Vg/s400/header.PNG" alt="" id="BLOGGER_PHOTO_ID_5263902140274637570" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;All of this is part of what Dan Ingalls, and Smalltalkers in general, mean when they say the system is &lt;span style="font-weight: bold; font-style: italic;"&gt;alive&lt;/span&gt;. It’s the key property you seek in every system - its ability to respond to changes and mutate while running, which gives you the kind of interactivity you get in the real world with real objects.  &lt;span style="font-weight: bold; font-style: italic;"&gt;Live code&lt;/span&gt; in the IDE is the opposite of &lt;span style="font-style: italic; font-weight: bold;"&gt;dead code&lt;/span&gt; - stuff written in files, which needs to be explicitly loaded into a runtime to do anything.&lt;br /&gt;&lt;br /&gt;This all is very cool, but it also has a downside. Suppose you have a bug in your code, causing it to crash. You may be able to fix it in the debugger - but maybe the root of the problem is in another class. You need to browse it - but now, every time you try and browse a class, it crashes. You’ve shot yourself in the foot.&lt;br /&gt;&lt;br /&gt;This is where you almost wish you were looking at dead code, by browsing files in an editor (or a conventional IDE). Fortunately, we know how to deal with this problem as well.&lt;br /&gt;&lt;br /&gt;In this case, we want to manipulate the modified IDE from another, unmodified one, &lt;span style="font-style: italic; font-weight: bold;"&gt;as a dynamic program&lt;/span&gt;. That way we still enjoy  immediate feedback for our changes, while jkeeping our feet safe.  Once it works, we want to be able to easily suck in the changes into the unmodifed version - without restarting, rebuilding, or going back to the big bang.&lt;br /&gt;&lt;br /&gt;We haven’t implemented this yet - but much of it’s been done before, in the mid-90s, by Allen Wirfs-Brock in the FireWall project at ParcPlace.  You do it using mirror based reflection.&lt;br /&gt;&lt;br /&gt;One of the many things mirrors facilitate, is managing reflection in a distributed setting. Java’s JDI is an example of a mirror API was designed to do just that - though it has serious limitations because it has to work on a variety of JVMs. If you design the mirror API correctly, and build your IDE on it, the IDE can work almost identically on programs within the same process, in another process, or across the internet.&lt;br /&gt;&lt;br /&gt;This ability to manage reflective changes to the IDE via a separate process is a luxury, which probably explains why it generally isn’t implemented, The reality is that shooting yourself in the foot is infrequent, and you can always recover (if only by saving your changes to a file before testing them). The benefits of liveness far outweigh the risks.&lt;br /&gt;&lt;br /&gt;Nevertheless, in time, I expect to address this in the Newspeak IDE. In the meantime, it’s alive, even if support for death is lacking. Of course, the broader lesson is that IDEs &lt;span style="font-weight: bold; font-style: italic;"&gt;especially&lt;/span&gt;, should be implemented using dynamic languages.&lt;br /&gt;&lt;br /&gt;The above echoes my previous post from June, but I hope that the concrete examples are helpful. The liveness makes it easy to implement features like &lt;span style="font-style: italic;"&gt;Inspect Presenter&lt;/span&gt;, which let you identify where to change the IDE, but  even more critically, liveness allows you actually make the change and get instant feedback.&lt;br /&gt;&lt;br /&gt;In closing, I want to emphasize that this is not an unprincipled approach. All of the above is possible because the IDE can reflect on itself.  Self reference is at the heart of computing. It’s all about recursion. I find it strange that many theoretically oriented computer scientists, who can wax poetic about the Y combinator, eschew languages and systems that apply the same principles of self-application to themselves.  I’m sure this is will change though; the good ideas win in the end.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-685389284173953354?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/685389284173953354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=685389284173953354' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/685389284173953354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/685389284173953354'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/11/dynamic-ides-for-dynamic-languages.html' title='Dynamic IDEs for Dynamic Languages!'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_k3ghkt1e0I8/SQ0gTpFLsrI/AAAAAAAAADY/TSVnK1_vv-U/s72-c/inspectPresenter0.png' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-6860101018441749150</id><published>2008-09-20T14:25:00.000-07:00</published><updated>2008-09-20T15:10:14.543-07:00</updated><title type='text'>Skinning Newspeak?</title><content type='html'>Whenever it comes to discussing language syntax, &lt;a href="http://en.wikipedia.org/wiki/Color_of_the_bikeshed"&gt;Parkinson’s law of triviality&lt;/a&gt; comes to mind.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Incidentally, the book is back in print! If you haven’t read it, check out &lt;a href="http://books.google.com/books?id=a3ktAAAAMAAJ&amp;amp;q=Parkinson%27s+Law&amp;amp;dq=Parkinson%27s+Law&amp;amp;ei=OG_VSMXXC5zOswPx1YmOBA&amp;amp;client=safari&amp;amp;pgis=1"&gt;this priceless classic&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Newspeak’s syntax currently resembles Smalltalk and Self. For the most part, this a fine thing, but I recognize that it can be a barrier to adoption. So, as we get closer to releasing Newspeak we may have to consider, with heavy heart,  changes that could ease the learning curve.&lt;br /&gt;&lt;br /&gt;One approach is the idea of syntactic skins. You keep the same abstract language underneath, but adjust its concrete syntax. In theory, you can have a skin that looks Smalltalkish, and one that looks Javanese, and another sort of like Visual Basic etc.&lt;br /&gt;&lt;br /&gt;The whole idea of skins is closely related to Charles Simonyi’s notion of intentional programming. Cutting through the vapor, one of the few concrete things one can extract is the idea (not new or original with Simonyi) of an IDE that can present programs in a rich variety of skins, some of which are graphical.  That, and support for defining DSLs for the domain experts to program in. This is all a fine thing, as long as you understand that’s all it is. This is still a pretty tall order.&lt;br /&gt;In any case, Magnus Christerson is doing a superb job of making that vision a reality.&lt;br /&gt;&lt;br /&gt;It is of course crucial that any program can be automatically displayed in any skin in the IDE. And designing skins requires thought, and is prone to abuse, which makes me hesitate.&lt;br /&gt;&lt;br /&gt;Naming conventions that may make sense in one syntax may not really work in another, for example.  Take maps (dictionaries in Smalltalk). In Smalltalk, the number of arguments to a method is encoded in the method name. So class &lt;span style="font-weight: bold;"&gt;Dictionary&lt;/span&gt; has a method called &lt;span style="font-weight: bold;"&gt;at:put:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;aMap at: 3 put: 9.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In a javanese language, you’d tend to use a different name, say, &lt;span style="font-style: italic; font-weight: bold;"&gt;put&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;aMap.put(3, 9);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;However, with skins, you need to either use the very same name&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;aMap.at:put:(3, 9);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;which looks weird and may even conflict with other parts of the syntax, or have some automated transformation&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;aMap.atPut(3, 9);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;All of which looks odd and may have issues (after all &lt;span style="font-weight: bold;"&gt;at:Put:&lt;/span&gt; would be a distinct, legal Newspeak method name which would also map to &lt;span style="font-weight: bold;"&gt;atPut&lt;/span&gt;). And what happens if you start writing your code in the javanese syntax? How do I map &lt;span style="font-weight: bold;"&gt;put&lt;/span&gt; into a 2 argument Newspeak method name? &lt;span style="font-weight: bold;"&gt;p:ut:&lt;/span&gt;? &lt;span style="font-weight: bold;"&gt;pu:t:&lt;/span&gt;? Maybe in this case, it takes a single tuple as its argument: &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;aMap put: {3. 9}.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There may be a creative way out; &lt;a href="http://blogs.msdn.com/madst/"&gt;Mads Torgersen&lt;/a&gt; once suggested a syntax like&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;aMap.at(3) put(9)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or maybe we map all names into &lt;span style="font-weight: bold;"&gt;ratHole&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The standard procedure call syntax also has more substantial difficulties. Without a typechecker, it’s hard to get the number of arguments right. The Smalltalk keyword syntax, while unfamiliar to many, has a huge advantage in a dynamically typed setting - no arity errors (if you’ve written any javascript, you probably know what I mean).&lt;br /&gt;&lt;br /&gt;In addition, the Smalltalk keyword notation is really nice for defining embedded DSLs, as &lt;a href="http://blog.3plus4.org/"&gt;Vassili&lt;/a&gt; notes in &lt;a href="http://blog.3plus4.org/2008/09/13/a-taste-of-implicit-receivers/"&gt;a recent post&lt;/a&gt;. This is a point that I want to expand upon at some future time.&lt;br /&gt;&lt;br /&gt;So I’m pretty sure that regardless of whether we use skins or not, we’ll retain the keyword message send syntax across all skins. It’s just a good idea for this sort of language. &lt;br /&gt;&lt;br /&gt;There are syntactic elements that are easy to tweak so that they are more familiar to a wider audience. In some cases, there are standard syntactic conventions that work well, and we can just adopt them.  For example, using curly braces as delimiters for class and method bodies (and also closures), or using &lt;span style="font-weight: bold;"&gt;return:&lt;/span&gt; instead of &lt;span style="font-weight: bold;"&gt;^&lt;/span&gt;. If these were the only issues, one might not really consider skins, since the differences are minor. The &lt;a href="http://bracha.org/newspeak-spec.pdf"&gt;current draft spec&lt;/a&gt; mentions some of these.&lt;br /&gt;&lt;br /&gt;Skins may be most valuable for issues in between the two extremes cited above. One of the most obvious is operator precedence. People have been taught a set of precedences in elementary school, and most have never recovered. Programmers have also learned C or something similar, they have even more expectations in this regard.&lt;br /&gt;&lt;br /&gt;Newspeak, like Smalltalk, gives all binary operators equal precedence, evaluating them in order from left to right.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;5 + 4 * 3&lt;/span&gt; evaluates to &lt;span style="font-style: italic; font-weight: bold;"&gt;27&lt;/span&gt; in Smalltalk, not to &lt;span style="font-style: italic; font-weight: bold;"&gt;17&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now I have never, ever had a bug due to this, but many people get all worked up over this issue. Why not just give in, and follow standard precedence rules? Well, there is the question of whose rules - C, Java, Perl?  What about operators those languages don’t have (ok, so Perl probably has all operators in the known universe and some to spare)?&lt;br /&gt;&lt;br /&gt;Another issue is that Newspeak is designed to let you embed domain specific languages as libraries. Then the standard choices don’t always make sense. Allow people to set precedence explicitly you say? This is problematic. Newspeak aims to stay simple. This is a matter of taste and judgement. If you like an endless supply of bells and whistles, look elsewhere.&lt;br /&gt;&lt;br /&gt;Skins might give us an out.  Some skins would dictate the precedence of popular operators (leaving the rest left-to-right, as in Scala for example). This means your DSL may look odd in another skin, but maybe that’s tolerable.&lt;br /&gt;&lt;br /&gt;Once you have skins, you can also address issues that otherwise aren’t worth dealing with - like dots. If you really feel the need to write &lt;span style="font-weight: bold;"&gt;aList.get&lt;/span&gt; instead of &lt;span style="font-weight: bold;"&gt;aList get&lt;/span&gt;, a suitable skin could be made available.&lt;br /&gt;&lt;br /&gt;It looks like language skins can be used to bridge over minor syntactic differences, but not much more. On the other hand, if you don’t have skins, you have a better chance of establishing a shared lingua franca amongst a programming community.&lt;br /&gt;&lt;br /&gt;Overall, my sense is that such skins are more trouble than they’re worth.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-6860101018441749150?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/6860101018441749150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=6860101018441749150' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6860101018441749150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6860101018441749150'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/09/skinning-newspeak.html' title='Skinning Newspeak?'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-7885861475997677863</id><published>2008-08-31T12:15:00.000-07:00</published><updated>2008-08-31T12:41:00.302-07:00</updated><title type='text'>Foreign functions, VM primitives and Mirrors</title><content type='html'>An issue that crops up in systems based on virtual machine is: what are the primitives provided by the VM and how are they represented?&lt;br /&gt;&lt;br /&gt;One answer would be that those are simply the instructions constituting the virtual machine language (often referred to as byte codes). However, one typically finds that there are some operations that do not fit this mold. An example would be the &lt;span style="font-weight: bold; font-style: italic;"&gt;defineClass()&lt;/span&gt; method, whose job is to take a class definition in JVML (Java Virtual Machine Language) and install it into the running JVM.  Another would be the &lt;span style="font-weight: bold; font-style: italic;"&gt;getClass()&lt;/span&gt; method that every Java object supports.&lt;br /&gt;&lt;br /&gt;These operations cannot be expressed directly by the high level programming languages running on the VM, and no machine instruction is provided for them either. Instead, the VM provides a procedural interface. So while the Java platform exposes &lt;span style="font-style: italic; font-weight: bold;"&gt;getClass()&lt;/span&gt;, &lt;span style="font-weight: bold; font-style: italic;"&gt;defineClass()&lt;/span&gt; and the like, behind the scenes these Java methods invoke a VM primitive to do their job.&lt;br /&gt;&lt;br /&gt;Why aren’t primitives supported by their own, dedicated virtual machine language instructions? One reason is there are typically too many of them, and giving each an instruction might disrupt the instruction set architecture (because you might need too many bits for opcodes, for example). It’s also useful to have an open ended set of primitives, rather than hardwiring them in the instruction set.&lt;br /&gt;&lt;br /&gt;You won’t find much discussion of VM primitives in the Java world. Java provides no distinct mechanism for calling VM primitives. Instead, primitives are treated as native methods (aka foreign functions) and called using that mechanism. Indeed, in Java there is no distinction between a foreign function and a VM primitive: a VM primitive is foreign function implemented by the VM.&lt;br /&gt;&lt;br /&gt;On its face, this seems reasonable. The JVM is typically implemented in a foreign language (usually C or C++) and it can expose any desired primitives as C functions that can then be accessed as native methods. It is very tempting to use one common mechanism for both purposes.&lt;br /&gt;&lt;br /&gt;One of the goals of this post is to explain why this is wrong, and why foreign functions and VM primitives differ and should be treated differently.&lt;br /&gt;&lt;br /&gt;Curiously, while Smalltalk defines no standardized FFI (Foreign Function Interface), the original specification defines a standard set of VM primitives. Part of the reason is historical: Smalltalk was in a sense the native language on the systems where it originated. Hence there was no need for an FFI (just as no one ever talks about an FFI in C), and hence primitives could not be defined in terms of an FFI and had to be thought of distinctly.&lt;br /&gt;&lt;br /&gt;However, the distinction is useful regardless. Calling a foreign function requires marshaling of data crossing the interface. This raises issues of different data formats, calling conventions, garbage collection etc. Calling a VM primitive is much simpler: the VM knows all there is to know about the management of data passed between it and the higher level language.&lt;br /&gt;&lt;br /&gt;The set of primitives is moreover small and under the control of the VM implementor. The set of foreign functions is unbounded and needs to be extended routinely by application programmers. So the two have different usability requirements.&lt;br /&gt;&lt;br /&gt;Finally, the primitives may not be written in a foreign language at all, but in the same language in a separate layer.&lt;br /&gt;&lt;br /&gt;So, I’d argue that in general one needs both an FFI and a notion of VM primitives (as in, to take a random example, Strongtalk). Moreover, I would base an FFI on VM primitives rather than the other way around. That is, a foreign call is implemented by a particular primitive (&lt;span style="font-style: italic; font-weight: bold;"&gt;call-foreign-function&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;Consider that native methods in Java are implemented with VM support; the JVM’s method representation marks native methods specially, and the method invocation instructions handle native calls accordingly. &lt;br /&gt;&lt;br /&gt;The Smalltalk blue book’s handling of primitives is similar; primitive methods are marked specially and handled as needed by the method invocation (send) instructions.&lt;br /&gt;&lt;br /&gt;It might be good to have one instruction, &lt;span style="font-style: italic; font-weight: bold;"&gt;invokeprimitive&lt;/span&gt;, dedicated to calling primitives. Each primitive would have an identifying code, and one assumes that the set of primitives would never exceed some predetermined size (8 bits?). That would keep the control of the VM entirely within the instruction set.&lt;br /&gt;&lt;br /&gt;It is good to have a standardized set of VM primitives, as Smalltalk-80 did. It makes the interface between the VM and built in libraries cleaner, so these libraries can be portable. We discussed doing this for the JVM about nine or ten years ago, but it never went anywhere.&lt;br /&gt;&lt;br /&gt;If primitives aren’t just FFI calls, how does one invoke them at the language level? Smalltalk has a special syntax for them, but I believe this is a mistake. In Newspeak, we view a primitive call as a message send to the VM. So it is natural to reify the VM via a VM mirror that supports messages corresponding to all the primitives.&lt;br /&gt;&lt;br /&gt;A nice thing abut using a mirror in this way, is that access to primitives is now controlled by a capability (the VM mirror), so the standard object-capability architecture handles access to primitives just like anything else.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;To get this to really work reliably, the low level mirror system must prohibit installation of primitive methods by compilers etc.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Another desirable propery of this scheme is that you can emulate the primitives in a regular object for purposes of testing, profiling or whatever. It's all a natural outgrowth of using objects and message passing throughout.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-7885861475997677863?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/7885861475997677863/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=7885861475997677863' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7885861475997677863'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7885861475997677863'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/08/foreign-functions-vm-primitives-and.html' title='Foreign functions, VM primitives and Mirrors'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-7477168280176165957</id><published>2008-07-27T11:30:00.000-07:00</published><updated>2008-07-27T11:36:42.556-07:00</updated><title type='text'>Invisible Video</title><content type='html'>A quick update; several people have told me that the Smalltalk Solutions video of the Hopscotch demo is unhelpful, since you can't see the screen. I should have watched it before linking to it; I apologize. Since I was in the room during the presentation, I didn't think to watch it again. My bad.  I've taken the link down. We'll produce a viewable demo for download and post it in th enear future.  Again, apologies to  to anyone who spent time downloading/watching the unwatchable.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-7477168280176165957?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/7477168280176165957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=7477168280176165957' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7477168280176165957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7477168280176165957'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/07/invisiible-video.html' title='Invisible Video'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5523663196277658696</id><published>2008-07-26T12:39:00.000-07:00</published><updated>2008-07-27T11:21:44.143-07:00</updated><title type='text'>Debugging Visual Metaphors</title><content type='html'>My previous post commented on the unsatisfactory state of mainstream IDEs. Continuing with this theme, I want to focus on one of my long term pet peeves - debugger UIs.&lt;br /&gt;&lt;br /&gt;Donald Norman, in his book &lt;a href="http://www.amazon.com/Design-Everyday-Things-Donald-Norman/dp/0385267746"&gt;&lt;span style="font-style: italic;"&gt;The Design of Everyday Things&lt;/span&gt;&lt;/a&gt;, makes the point that truly good designs are easy to use, because they make it intuitive how they should be used - in his words, they &lt;span style="font-style: italic;"&gt;afford&lt;/span&gt; a usage pattern.&lt;br /&gt;&lt;br /&gt;How do you describe the state of a running program on a whiteboard? You draw the stack. So it seems to me that a stack is the natural visual metaphor for a debugger. Here is a screen shot of our new Hopscotch-based debugger, which will soon replace the Squeak debugger in the Newspeak IDE.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://bracha.org/Hopscotch%20debugger.png" /&gt;&lt;br /&gt;&lt;br /&gt;You can see that the debugger looks like a stack trace. Every entry in the stack trace can be expanded into a presenter that shows that stack frame - including the source code for the method in question, and a view of the state of the frame - the receiver, the variables in the activation, the top of the evaluation stack for expressions (i.e., the last value computed).&lt;br /&gt;&lt;br /&gt;Of course, this isn’t my idea. It’s another one of those great ideas from Self, as evidenced in this screenshot:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://bracha.org/Self%20debugger.png" /&gt;&lt;br /&gt;&lt;br /&gt;we already stole the idea from Self once before, in Strongtalk, as shown here.&lt;br /&gt;&lt;br /&gt;&lt;img style="width: 709px; height: 572px;" src="http://bracha.org/StrongtalkDebugger.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;One of the nice properties of the stack oriented view is that you can view multiple stack frames at once, so you can see and reason about a series of calls, much as you would at your whiteboard.&lt;br /&gt;&lt;br /&gt;In contrast, most IDEs (even Smalltalk IDEs) show a multi-pane view, with one pane showing the source for the current method (in its file, naturally), one pane showing the state of the current frame, and one showing the stack trace.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://bracha.org/EclipseDebugger.jpg" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can’t easily see the state of the caller of the current method, or its code, while simultaneously looking at the current method (or another activation in the call chain). And if you modify the current method’s code (assuming you can do that at all), you’re likely locked into a mode, and can’t see the other frames unless you save or cancel your changes.&lt;br /&gt;&lt;br /&gt;Hopscotch GUIs are inherently non-modal - so you can modify any one of the methods you’re viewing, and then link to another page to view, say, the complete class declaration, all without opening another window and without having to save or lose your work.&lt;br /&gt;&lt;br /&gt;The fact that one rarely needs more than one window is one of the things I really like about Hopscotch. There’s no need for a docking bar, or tabs for that matter. Tabs are popular these days, but they don’t scale: they occupy valuable screen real estate, and beyond half a dozen or so become disorienting and unmanageable.&lt;br /&gt;Hopscotch does better than the mainstream, and better than previous efforts like Strongtalk or Self, partly because of its navigation model, and partly because of the inherent &lt;span style="font-weight: bold; font-style: italic;"&gt;compositionality&lt;/span&gt; of tools built with it.  The fact that I can move from the debugger to the class browser in the same window did not require special planning - it’s inherent in the way Hopscotch based tools behave - they can be composed by means of a set of combinators. Compositionality is one of the most crucial, and most often overlooked, properties in software design; it’s what sets Hopscotch apart.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;You can find out more about Hopscotch in this &lt;a href="http://bracha.org/hopscotch-wasdett.pdf"&gt;paper&lt;/a&gt; and on &lt;a href="http://blog.3plus4.org/"&gt;Vassili ‘s blog&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The design of debugger UIs is one example of something that needs to change in modern IDEs. There are others of course. Many are related to the basic problems of modality, navigation and proliferation of panes/windows noted above. Overall, your typical IDE UI is too much like the &lt;a href="http://uscockpits.com/Jet%20Bombers/B-52G%20Stratofortress%20center.JPG"&gt;control panel of a B-52 bomber&lt;/a&gt; or an &lt;a href="http://www.space1.com/Spacecraft_Data/Handbook_Illustrations/Apollo/Apollo_Control_Panel/apollo_control_panel.html"&gt;Apollo space capsule&lt;/a&gt;: a mind boggling array of switches, gauges, controls and wizards that interact with each other in myriad and confusing ways.  This is neither necessary nor desirable. Like explicit memory management and primitive types, in time we will progress beyond these.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5523663196277658696?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5523663196277658696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5523663196277658696' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5523663196277658696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5523663196277658696'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/07/debugging-visual-metaphors.html' title='Debugging Visual Metaphors'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-8970800758112672874</id><published>2008-06-07T16:30:00.000-07:00</published><updated>2008-06-07T17:09:57.744-07:00</updated><title type='text'>Incremental Development Environments</title><content type='html'>Back in 1997, when I started working at Sun, I did not expect to do much programming language design. After all, Java was completely specified in the original JLS, or so it was thought.&lt;br /&gt;&lt;br /&gt;What I actually expected to do was to work on a Java IDE. Given my Smalltalk background, I was of course very much aware of how valuable a good IDE is for programmers. The Java IDE I had in mind never materialized. Management at the time thought this was an issue to be left to other vendors. If this seems a little strange today - well, it seemed strange back then too. In any case, the Java world has since learned that IDEs are very important and strategic.&lt;br /&gt;&lt;br /&gt;That said, todays Java IDEs are still very different from what I envisioned based on my Smalltalk background.&lt;br /&gt;&lt;br /&gt;Java IDEs have yet to fully embrace the idea of incremental development.  Look in any such system, and you’ll find a button or menu labeled &lt;span style="font-weight: bold; font-style: italic;"&gt;Build&lt;/span&gt;. The underlying idea is that a program is assembled from scratch every time you change it.&lt;br /&gt;&lt;br /&gt;Of course, that is how the real world works, right? You find a broken window on the Empire State building, so you tear it down and rebuild it with the fixed window. If you're clever, you might be able to do it pretty fast. Ultimately, it doesn't scale.&lt;br /&gt;&lt;br /&gt;The build button comes along with an obsession with files. In C and its ilk, the notion of files is part of the language, because of #include.  Fortunately, Java did away with that legacy. Java programmers can think in terms of packages, independent of their representation on disk.  Why spend time worrying about class paths and directory structures?  What do these have to do with the structure of your program and the problem you’re trying to solve?&lt;br /&gt;&lt;br /&gt;The only point where files are useful in this context is as a medium for sharing source code or distributing binary code.  Files are genuinely useful for those purposes, and Smalltalk IDEs have generally gone overboard in the opposite direction; but I digress.&lt;br /&gt;&lt;br /&gt;A consequence of the file/build oriented view is that the smallest unit of incremental change is the file - something that is often too big (if you ever notice the time it takes to compile a change, that’s too long) and moreover, not even a concept in the language.&lt;br /&gt;&lt;br /&gt;More fundamentally, what’s being changed is a static, external representation of the program code; there is no support for changing the live process derived from the code so that it matches up with the code that describes it. It’s like having a set of blueprints for a building (the code in the file) which doesn’t match the building (the process generated from the code).&lt;br /&gt;&lt;br /&gt;For example, once you add an instance variable to a class, what happens to all the existing instances on the heap? In Smalltalk, they all have the new instance variable. In Java - well, nothing happens.&lt;br /&gt;&lt;br /&gt;In general, any change you make to the code should be reflected immediately in the entire environment. This implies a very powerful form of fix-and-continue debugging (I note in amazement that after all these years, Eclipse still doesn’t support even the most basic form of fix-and-continue).&lt;br /&gt;&lt;br /&gt;All this is of course a very tall order for a language with a mandatory static type system.&lt;br /&gt;I’m not aware of a JVM implementation that can begin to deal with class schema changes (that is, changing the shapes of objects because their class has lost or acquired fields). It’s not impossible, but it is hard.&lt;br /&gt;&lt;br /&gt;Consider that removing a field requires invalidating any code that refers to it. In a language where fields are private, the amount of code to invalidate is nicely bounded to the class (good design decisions tend to simplify life). Public fields, apart from their nasty software engineering properties, add complexity to the underlying system.&lt;br /&gt;&lt;br /&gt;This isn’t just a headache for the IDE. The VM has to provide support for changing the classes of existing instances. However, in the absence of VM support there all kinds of tricks one can play. If you compile all code yourself, you can ensure that no one accesses fields directly - everything goes through accessors. You can even rewrite imported binaries. With enough effort, I believe you can make it all work on an existing JVM with good JVMDI support.&lt;br /&gt;&lt;br /&gt;Changing code in a method is supported by JVMDI (well, the JVMDI spec allows for supporting schema changes as well - it’s just that it isn’t required and no one ever implemented it). However, what happens if you change the signature of a method?  Any number of existing callers may be invalid due to type errors.  The IDE needs to tell you about this pretty much instantaneously, invalidating all these callers. Most of this worked in Trellis/Owl back in the late 80s. The presence of the byte code verifier means that this applies to binary code as well.&lt;br /&gt;&lt;br /&gt;Achieving true incremental development is very hard. Still, given the amount of people working on Java, you’d think it would have happened after all these years. It hasn’t, and I don’t expect it to.&lt;br /&gt;&lt;br /&gt;Someone will rightly make the point that mandatory typing can be very helpful in an IDE - its easier to find callers of methods, implementors, references to fields or classes, as well as refactoring (though, oddly, all these features originated in IDEs for dynamic languages - Smalltalk or Lisp; speculating why that is would make for another controversial post). This post isn’t really about static/dynamic typing - it’s about incrementality in the IDE.&lt;br /&gt;&lt;br /&gt;Of course, mainstream IDEs annoy me for other reasons: the bloat, the slow startup, and most of all the B-52 bomber/Apollo space capsule style UI. That probably deserves another post.&lt;br /&gt;&lt;br /&gt;In the meantime, I can go back to &lt;a href="http://blog.3plus4.org/"&gt;Vassili’s&lt;/a&gt; fabulous &lt;a href="http://bracha.org/hopscotch-wasdett.pdf"&gt;Hopscotch&lt;/a&gt; browsers and leave the mainstream to cope with all the docking bars, tabs and panes too small to print their name. You, dear reader, if you’re using a mainstream IDE, may not realize what you’re missing. To an extent, these things have to be experienced to be appreciated. Still I encourage you to demand better - demand true incremental development support, in whatever language you use. Because in the end, there are only two kinds of development - incremental, and excremental.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-8970800758112672874?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/8970800758112672874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=8970800758112672874' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/8970800758112672874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/8970800758112672874'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/06/incremental-development-environments.html' title='Incremental Development Environments'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-6406299503701798118</id><published>2008-05-06T09:56:00.000-07:00</published><updated>2008-05-06T10:00:27.239-07:00</updated><title type='text'>The Future of Newspeak</title><content type='html'>Several people have asked me when Newspeak will be released. Well, I still don’t know, but at least now I know it &lt;span style="font-weight: bold;"&gt;will&lt;/span&gt; be released. Cadence has generously agreed to make Newspeak available as open source software under the Apache 2.0 license.&lt;br /&gt;&lt;br /&gt;We will be publishing a draft spec for Newspeak soon; I say draft because I expect Newspeak to continue to evolve substantially for the next year at least, and because the initial spec will necessarily be incomplete.&lt;br /&gt;&lt;br /&gt;It will be a while before we are ready to make a proper public release of Newspeak. In the meantime, I’ve gathered some information on my &lt;a href="http://bracha.org/Site/Newspeak.html"&gt;personal web site&lt;/a&gt;. We plan to set up an official Newspeak web site in the near future.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-6406299503701798118?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/6406299503701798118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=6406299503701798118' title='20 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6406299503701798118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/6406299503701798118'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/05/future-of-newspeak.html' title='The Future of Newspeak'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>20</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-8743722749815113628</id><published>2008-04-26T07:43:00.000-07:00</published><updated>2008-04-26T08:38:07.453-07:00</updated><title type='text'>java'scrypt</title><content type='html'>Everyone is talking about cloud computing these days; I should add some vapor to the mist.&lt;br /&gt;&lt;br /&gt;I began thinking seriously about the topic a bit after April fools day 2004, when gmail was released. I started using gmail shortly therefater.  After about 30 minutes, I was convinced that web clients had a long way to go.&lt;br /&gt;&lt;br /&gt;It is true that gmail showed the world that you could do far more with javascript in a browser than I or most other people had realized.&lt;br /&gt;&lt;br /&gt;Javascript and Ajax have come a long way since then. For an impressive demonstration of what’s possible, see the &lt;a href="http://research.sun.com/projects/lively/"&gt;Lively Kernel&lt;/a&gt;, a realization of John Maloney’s  Morphic GUI ideas of Self and Squeak, in Javascript. It only works in Safari 3.0 and some advanced Firefox builds, but that situation will improve in time.&lt;br /&gt;&lt;br /&gt;Javascript’s performance remains an issue; this will also improve dramatically in the foreseeable future. However, with all the talk about computing in the cloud, I think some caveats are in order.&lt;br /&gt;&lt;br /&gt;Many real applications will need to be used offline, and so will need to store state on the client.  They are NOT going to be pure server side web applications. Google has finally acknowledged as much with the introduction of Google Gears. Microsoft’s LiveMesh naturally emphasizes this even more.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I say naturally, because Microsoft has an interest in the personal computer, while Google has an interest in taking us to an updated version of X terminals and 1960’s time sharing.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Of course, there are other technologies in this space, like Adobe Air.  Macromedia (now part of Adobe) saw this coming earlier than most, going back to at least 2003 with its &lt;/span&gt;&lt;a style="font-style: italic;" href="http://www.adobe.com/products/central/"&gt;Central project&lt;/a&gt;&lt;span style="font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;With all these pieces in place, we may finally have the underlying elements necessary to build good applications delivered through the web browser.&lt;br /&gt;&lt;br /&gt;Why has this taken so long? In theory, Java was supposed to deliver all this over ten years ago. Java started as a client technology, and applets were supposed to do exactly what advanced Ajax applications do today, only better.&lt;br /&gt;&lt;br /&gt;Of course, the problem was that Java clients behaved very poorly; rather than fix the client technology, Sun captured the lucrative enterprise business. To understand why Sun behaved that way, why it had to behave that way, read  &lt;a href="http://books.google.com/books?id=SIexi_qgq2gC&amp;amp;dq=innovator%27s+dillema&amp;amp;pg=PP1&amp;amp;ots=AhrUcHA7Em&amp;amp;sig=9cwl6_QMtk4bSvX7ldw4i_UvC48&amp;amp;hl=en&amp;amp;prev=http://www.google.com/search?client=safari&amp;amp;rls=en-us&amp;amp;q=Innovator%27s+Dillema&amp;amp;ie=UTF-8&amp;amp;oe=UTF-8&amp;amp;sa=X&amp;amp;oi=print&amp;amp;ct=title&amp;amp;cad=one-book-with-thumbnail"&gt;The Innovator’s Dilemma&lt;/a&gt;.&lt;br /&gt;Maybe I’ll expand on that theme in another post.&lt;br /&gt;&lt;br /&gt;The result of Java’s failure on the client was that the entire vision of rich network-enabled client applications was put on hold for a decade.   It is now once again coming to the fore,  but there are other languages in that space now, Javascript chief among them.&lt;br /&gt;&lt;br /&gt;I’m not advocating writing clients in Javascript directly. Javascript is the assembly language of the internet platform (and the browser is the OS). It’s flexible enough to support almost anything on top of it, and poorly structured enough that humans shouldn’t be writing sizable applications in it.&lt;br /&gt;&lt;br /&gt;I am not in favor of the attempts to make &lt;a href="http://www.ecmascript.org/docs.php"&gt;Javascript 2&lt;/a&gt; be another Java either. Javascript’s importance stems from the existing version’s position in the browser, and from its flexibility.   It should be kept simple. Javascript's value increases the more uniformly it is implemented across browsers. A new, complex language will take a long time to reach that point.&lt;br /&gt;&lt;br /&gt;People should program in a variety of languages that suit them, and have these compiled to Javascript and HTML (GWT is an example of that, though not one I really like).  One shouldn't have to deal with the hodgepodge of Javascript and HTML (broadly defined to include XML, CSS etc.)  that constitute web programming today.&lt;br /&gt;&lt;br /&gt;I don’t expect native clients to go away any time soon though. They will provide a better experience than the browser in many ways for quite a long time. The advantage of writing in a clean, high level language and platform is that it can be compiled to run either on the “WebOS” or on a native OS.&lt;br /&gt;&lt;br /&gt;What one wants to see in such a platform is the ability to take advantage of the network in a deep way - for software delivery and update and for data synchronization. Which brings me back to my theme of objects as software services (see the &lt;a href="http://video.google.com/videoplay?docid=-162051834912297779"&gt;video&lt;/a&gt;), which I’ve been preaching publicly since 2005 ( see my &lt;a href="http://www.bracha.org/oopsla05-dls-talk.pdf"&gt;OOPSLA 2005 presentation&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;One should be able to write an application once, and deploy it both to the web browser and natively to any major platform. In all those scenarios, one should be able to update the software and data on the fly,  run it on- and offl-ine and and monitor it over the network continuously. And all this needs to be done in a way that preserves user’s security. This last bit is still a big challenge.&lt;br /&gt;&lt;br /&gt;The technology to do this is becoming available, even though everything takes much longer than one expects.  The vision has its root in research in the 1980s, continued in the 1990s with Java (and others, now largely forgotten, like &lt;a href="http://en.wikipedia.org/wiki/General_Magic"&gt;General Magic&lt;/a&gt;'s  &lt;a href="http://cgibin.erols.com/ziring/cgi-bin/cep/cep.pl?_key=Telescript"&gt;Telescript&lt;/a&gt;) and will be probably be realized in the 2010s, per Alan Kay’s 30 year rule.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-8743722749815113628?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/8743722749815113628/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=8743722749815113628' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/8743722749815113628'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/8743722749815113628'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/04/everyone-is-talking-about-cloud.html' title='java&apos;scrypt'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5601241685320247528</id><published>2008-03-23T10:48:00.000-07:00</published><updated>2008-03-23T11:31:38.314-07:00</updated><title type='text'>Monkey Patching</title><content type='html'>Earlier this month I spoke at the &lt;a href="http://www.sfi.org.pl/festival"&gt;International Computer Science Festival in Krakow&lt;/a&gt;. Krakow is a beautiful city with several universities, and it is becoming a high tech center, with branches of companies like IBM and Google. The CS festival draws well over a thousand participants; the whole thing is organized by students. While much of the program was in Polish, there were quite a few talks in English.&lt;br /&gt;&lt;br /&gt;Among these was &lt;a href="http://chadfowler.com/"&gt;Chad Fowler&lt;/a&gt;’s talk on Ruby. Chad is a very good speaker, who did an excellent job of conveying the sense of working in a dynamic language like Ruby. Almost everything he said would apply to Smalltalk as well.&lt;br /&gt;&lt;br /&gt;One of the points that came up was the custom, prevalent in Ruby and in Smalltalk, of extending existing classes with new methods in the service of some other application or library. Such methods are often referred to as extension methods in Smalltalk, and the practice is supported by tools such as the Monticello source control system.&lt;br /&gt;&lt;br /&gt;As an example, I’ll use my parser combinator library, which I’ve described in previous posts. To define a production for an &lt;span style="font-weight: bold;"&gt;if&lt;/span&gt; statement, you might write:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;if:: tokenFromSymbol: #if.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;then:: tokenFromSymbol: #then.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;ifStat:: if, expression, then, statement.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;assuming that productions for &lt;span style="font-weight: bold; font-style: italic;"&gt;expression&lt;/span&gt; and &lt;span style="font-style: italic; font-weight: bold;"&gt;statement&lt;/span&gt; already exist. The purpose of the rules if and then is to produce a tokenizing parser that accepts the symbols &lt;span style="font-style: italic; font-weight: bold;"&gt;#if&lt;/span&gt; and &lt;span style="font-style: italic; font-weight: bold;"&gt;#then&lt;/span&gt; respectively.  It might be nicer to just write:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;ifStat:: #if, expression, #then, statement.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;and have the system figure out that, in this context, you want to denote a parser for a symbol by the symbol directly, much as you would when writing the BNF.&lt;br /&gt;&lt;br /&gt;One way of achieving this would be to actually go and add the necessary methods to the class &lt;span style="font-style: italic; font-weight: bold;"&gt;Symbol&lt;/span&gt;, so that symbols could behave like parsers.  I know otherwise intelligent people who are prepared to argue for this approach. As I said, Smalltalkers would call these additions extension methods, but I find the more informal term &lt;span style="font-weight: bold;"&gt;monkey patching&lt;/span&gt; conveys a better intuition.&lt;br /&gt;&lt;br /&gt;Typically, one wants to deliver a set of such methods as a unit, to be installed when a certain class or library gets loaded.  So these changes are often provided as a patch that is applied dynamically. Not a problem in Smalltalk or Ruby or Python (though I gathered from the Pythoners in Krakow that they, to their credit, frown on the practice).&lt;br /&gt;&lt;br /&gt;Apparently, there is a need to explain why monkey patching is a really bad idea. For starters, the methods in one monkey’s patch might conflict with those in some other monkey’s patch. In our example, the sequencing operator for parsers conflicts with that for symbols.&lt;br /&gt;&lt;br /&gt;A mere flesh wound, says our programming primate: I usually don’t get conflicts, so I’ll pretend they won’t happen. The thing is, as thing scale up, rare occurrences get more frequent, and the costs can be very high.&lt;br /&gt;&lt;br /&gt;Another problem is API bloat. You can see this in &lt;a href="http://www.squeak.org/"&gt;Squeak&lt;/a&gt; images, where a lot of monkeying about has taken place over the years. Classes like &lt;span style="font-weight: bold; font-style: italic;"&gt;Object&lt;/span&gt; and &lt;span style="font-weight: bold; font-style: italic;"&gt;String&lt;/span&gt; are polluted with dozens of methods contributed by enterprising individuals who felt that their favorite convenience method is something the whole world needs to benefit from.&lt;br /&gt;&lt;br /&gt;Even in your own code, one needs to exercise restraint lest your API becomes obese with convenience methods. Big APIs eat up memory for both people and machinery, reducing responsiveness as well as learnability.&lt;br /&gt;&lt;br /&gt;Then there is the small matter of security. If you are free to patch the definition of a class like &lt;span style="font-style: italic; font-weight: bold;"&gt;String&lt;/span&gt; (typically on the fly when their code gets loaded), what’s to stop malicious macaques from replacing critical methods with really damaging stuff?&lt;br /&gt;&lt;br /&gt;The counter argument is that in many cases (though not in this example), the patch is designed to avoid the use of &lt;span style="font-weight: bold; font-style: italic;"&gt;typecase/switch/instance-of&lt;/span&gt; constructs, which bring  their own set of evils to the table.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lamp.epfl.ch/%7Eemir/written/MatchingObjectsWithPatterns-TR.pdf"&gt;Extractors&lt;/a&gt; are a new approach to pattern matching developed by &lt;a href="http://lamp.epfl.ch/%7Eodersky/"&gt;Martin Odersky&lt;/a&gt; for &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt;. They overcome the usual difficulty with pattern matching, which is that it violates encapsulation by exposing the implementation type of data, just like &lt;span style="font-style: italic; font-weight: bold;"&gt;instance-of&lt;/span&gt;.  It may be part of the answer here as well.&lt;br /&gt;&lt;br /&gt;However, many monkey patches are motivated by a desire for syntactic sugar, as the example shows.  Extractors won’t help here.&lt;br /&gt;&lt;br /&gt;A variety of language constructs have been devised to deal with this and related situations. &lt;a href="http://www.iam.unibe.ch/%7Escg/Archive/Papers/Berg03aClassboxes.pdf"&gt;Class boxes&lt;/a&gt; and selector namespaces in Smalltalk dialects, &lt;a href="http://www.swa.hpi.uni-potsdam.de/cop/"&gt;context oriented programming&lt;/a&gt; in Lisp and Smalltalk, &lt;a href="http://en.wikipedia.org/wiki/Extension_method"&gt;static extension methods in C#&lt;/a&gt; and even &lt;a href="http://www.haskell.org/tutorial/classes.html"&gt;Haskell type classes&lt;/a&gt; are related. These mechanisms don’t all provide the same functionality of course. I confess that I find none of them attractive. Each comes at a price that is too high for what it provides.&lt;br /&gt;&lt;br /&gt;For example, C# extension methods rely on mandatory typing. Furthermore, they would not address the example above, because we need the literal symbols we use in the grammar to behave like parsers when passed into the parser combinator library code, not just in the lexical scope of the grammar.&lt;br /&gt;&lt;br /&gt;Haskell type classes are much better. They would work for this problem (and many others), but also rely crucially on mandatory typing.&lt;br /&gt;&lt;br /&gt;Class boxes are dynamic, but again only effect the immediate lexical scope. The same is true of simple formulations of selector namespaces. Richer versions let you import the desired selectors elsewhere, but I find this gets rather baroque. I'm not sure how COP meshes with security; so far it seems too complex for me to consider.&lt;br /&gt;&lt;br /&gt;I’ve contemplated a change to the Newspeak semantics that would accommodate the above example, but it hasn’t been implemented, and I have mixed feelings about it. If a literal like  &lt;span style="font-weight: bold; font-style: italic;"&gt;#if &lt;/span&gt;is interpreted as an invocation of a factory method on &lt;span style="font-weight: bold; font-style: italic;"&gt;Symbol&lt;/span&gt;, then we can override &lt;span style="font-weight: bold; font-style: italic;"&gt;Symbol&lt;/span&gt; so that it supports the parser combinators. This only effects symbols created in a given scope, but isn’t just syntactic sugar like the C# extension methods suggested above.&lt;br /&gt;&lt;br /&gt;Of course, this can be horribly abused; one shudders to think what a band of baboons might make of the freedom to redefine the language’s literals. On the other hand, used judiciously, it is great for supporting domain specific languages.&lt;br /&gt;&lt;br /&gt;So far, I have no firm conclusions about how to best address the problems monkey patching is trying to solve. I don’t deny that it is expedient and tempting. Much of the appeal of dynamic languages is of course the freedom to do such things. The contrast with a language like Java is instructive.  Adding a method to &lt;span style="font-weight: bold; font-style: italic;"&gt;String&lt;/span&gt; is pretty much impossible. One has to sacrifice one’s first-born to the gods of the &lt;a href="http://jcp.org/en/home/index"&gt;JCP&lt;/a&gt; and wait seven years for them to decide whether to add the method or not. I’m not endorsing that model either: I know it only too well.&lt;br /&gt;&lt;br /&gt;Regardless, given my flattering portrayals of primate practices, you may deduce that my main comment on monkey patching is “just say no”.  The problems it induces far outweigh its benefits. If you feel tempted, think hard about design alternatives. One can do better.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5601241685320247528?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5601241685320247528/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5601241685320247528' title='20 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5601241685320247528'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5601241685320247528'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/03/monkey-patching.html' title='Monkey Patching'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>20</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-7020647932848543962</id><published>2008-02-17T17:22:00.000-08:00</published><updated>2008-02-17T17:28:06.445-08:00</updated><title type='text'>Cutting out Static</title><content type='html'>Most imperative languages have some notion of static variable. This is unfortunate, since static variables have many disadvantages. I have argued against static state for quite a few years (at least since the dawn of the millennium), and in Newspeak, I’m finally able to eradicate it entirely.  Why is static state so bad, you ask?&lt;br /&gt;&lt;br /&gt;Static variables are bad for security. See the &lt;a href="http://www.erights.org/"&gt;E literature&lt;/a&gt; for extensive discussion on this topic. The key idea is that static state represents an ambient capability to do things to your system, that may be taken advantage of by evildoers.&lt;br /&gt;&lt;br /&gt;Static variables are bad for distribution. Static state needs to either be replicated and sync’ed across all nodes of a distributed system, or kept on a central node accessible by all others, or some compromise between the former and the latter. This is all difficult/expensive/unreliable.&lt;br /&gt;&lt;br /&gt;Static variables are bad for re-entrancy. Code that accesses such state is not re-entrant. It is all too easy to produce such code. Case in point: javac. Originally conceived as a batch compiler, javac had to undergo extensive reconstructive surgery to make it suitable for use in IDEs. A major problem was that one could not create multiple instances of the compiler to be used by different parts of an IDE, because javac had significant static state. In contrast, the code in a Newspeak module definition is always re-entrant, which makes it easy to deploy multiple versions of a module definition side-by-side, for example.&lt;br /&gt;&lt;br /&gt;Static variables are bad for memory management. This state has to be handed specially by implementations, complicating garbage collection. The woeful tale of class unloading in Java revolves around this problem. Early JVMs lost application’s static state when trying to unload classes. Even though the rules for class unloading were already implicit in the specification, I had to add a section to the JLS to state them explicitly, so overzealous implementors wouldn’t throw away static application state that was not entirely obvious.&lt;br /&gt;&lt;br /&gt;Static variables are bad for for startup time. They encourage excess initialization up front. Not to mention the complexities that static initialization engenders: it can deadlock, applications can see uninitialized state, and unless you have a really smart runtime, you find it hard to compile efficiently (because you need to test if things are initialized on every use).&lt;br /&gt;&lt;br /&gt;Static variables are bad for for concurrency. Of course, any shared state is bad for concurrency, but static state is one more subtle time bomb that can catch you by surprise.&lt;br /&gt;&lt;br /&gt;Given all these downsides, surely there must be some significant upside, something to trade off against the host of evils mentioned above? Well, not really.  It’s just a mistake, hallowed by long tradition. Which is why Newspeak has dispensed with it.&lt;br /&gt;&lt;br /&gt;It may seem like you need static state, somewhere to start things off, but you don’t. You start off by creating an object, and you keep your state in that object and in objects it references. In Newspeak, those objects are modules.&lt;br /&gt;&lt;br /&gt;Newspeak isn’t the only language to eliminate static state. E has also done so, out of concern for security.  And so has Scala, though its close cohabitation with Java means Scala’s purity is easily violated. The bottom line, though, should be clear. Static state will disappear from modern programming languages, and should be eliminated from modern programming practice.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-7020647932848543962?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/7020647932848543962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=7020647932848543962' title='47 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7020647932848543962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7020647932848543962'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2008/02/cutting-out-static.html' title='Cutting out Static'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>47</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-4363727735499118288</id><published>2007-12-31T17:09:00.000-08:00</published><updated>2007-12-31T17:25:23.207-08:00</updated><title type='text'>More on Modules</title><content type='html'>My posts seem to raise more questions than they answer. This is as it should be, in accordance with the Computational Theologist Full Employment Act. In this post, I’ll try and answer some of the questions that arose from my last one.&lt;br /&gt;&lt;br /&gt;How does one actually hook modules together and get something going? As I mentioned before, module definitions are top level classes - classes that are defined in a namespace, rather than in another class.&lt;br /&gt;&lt;br /&gt;Defining a top level class makes its name available in the surrounding namespace. More precisely, it causes the compiler to define a getter method with the name of the class on the namespace; the method will return the class object.&lt;br /&gt; &lt;br /&gt;Since a module definition is just a class, one needs to instantiate it, by calling a constructor - which is a class side method. Continuing with the example from the previous post:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;MovieLister finderClass: ColonDelimitedMovieFinder &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now this isn’t quite realistic, since &lt;span style="font-style:italic;"&gt;ColonDelimitedMovieFinder&lt;/span&gt; probably needs access to things like collections and files to do its job. So it’s probable that it takes at least one parameter itself. The typical situation is that a module definition takes a parameter representing the necessary parts of the platform libraries. It might look something like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;ColonDelimitedMovieFinder usingLib: platform = (&lt;br /&gt;| &lt;br /&gt;OrderedCollection = platform Collections OrderedCollection.&lt;br /&gt;FileStream = platform Streams FileStream.&lt;br /&gt;|&lt;br /&gt;)...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So we’d really create the application this way:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;MovieLister finderClass: (ColonDelimitedMovieFinder usingLib: Platform new)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;where &lt;span style="font-style:italic;"&gt;Platform&lt;/span&gt; is a predefined module that provides access to the built-in libraries. &lt;br /&gt;&lt;br /&gt;Bob Lee points out that if I change &lt;span style="font-style:italic;"&gt;MovieLister&lt;/span&gt; so that it takes another parameter, I have to update all the creation sites for &lt;span style="font-style:italic;"&gt;MovieLister&lt;/span&gt;, whereas using a good DIF I only declare what needs to be injected and where. &lt;br /&gt;&lt;br /&gt;In many cases, I could address this issue by declaring a secondary constructor that feeds the second argument to the primary one.  &lt;br /&gt;&lt;br /&gt;Say we changed &lt;span style="font-style:italic;"&gt;MovieLister&lt;/span&gt; because it too needed access to some platform library:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;class MovieLister usingLib: platform finderClass: MovieFinder = ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We might be able to define a  secondary constructor &lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;class MovieLister usingLib: platform finderClass: MovieFinder = (&lt;br /&gt;...&lt;br /&gt;): ( &lt;br /&gt;  finderClass: MovieFinder = (&lt;br /&gt;     ^usingLib: Platform new finderClass: MovieFinder&lt;br /&gt; )&lt;br /&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are however two situations where this won’t work in Newspeak. &lt;br /&gt;&lt;br /&gt;One is inheritance, because subclasses must call the primary constructor. I showed how to deal with that in one of my August 2007 posts -  don’t change the primary constructor - change the superclass.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;class MovieLister finderClass: MovieFinder =  NewMovieLister usingLib: Platform new finderClass: MovieFinder&lt;br /&gt;(...)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The other problematic case is for module definitions. In most cases,  the solutions above won’t help; they won’t be able to provide a good default for the additional parameter, because they won’t have access to the surrounding scope. For this last situation I have  no good answer yet. I will say that the public API of a top level module definition should be pretty stable, and the number of calls relatively few.&lt;br /&gt;&lt;br /&gt;So overall, I think Bob makes an important point - DIFs give you a declarative way of specifying how objects are to be created. On the other had, it gets a bit complicated when different arguments are needed in different places, or if we don’t want to compute so many things up front at object creation time.  Guice has mechanisms to help with that, but I find them a bit rich for my blood.  In those cases, I really prefer to specify things naturally in my code.  &lt;br /&gt;&lt;br /&gt;Another advantage of abstracting freely over classes is that you can inherit from classes that are provided as parameters. &lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;class MyCollections usingLib: platform = (&lt;br /&gt;| ArrayList = platform ArrayList. |&lt;br /&gt;)( &lt;br /&gt;ExtendedArray List = ArrayList (...)&lt;br /&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now, depending what library you actually provide to &lt;span style="font-style:italic;"&gt;MyCollections&lt;/span&gt; as an argument, you can obtain distinct subclasses (in fact, there’s an easier way to do this,  but this post is once again getting too long). Correct me if I’m wrong, but I don’t think a DIF helps here.&lt;br /&gt;&lt;br /&gt;You can also do class hierarchy inheritance: modify an entire library embedded within a module by subclassing it and changing only what’s needed. This is somewhat less modular (inheritance always causes problems) but the tradeoff is well worth it in my opinion.&lt;br /&gt;&lt;br /&gt;I spoke about class hierarch inheritance at JAOO, and will likely speak about it again in one or more of my upcoming talks on Newspeak, at Google in Kirkland on January 8th, at &lt;a href="http://fool08.kuis.kyoto-u.ac.jp/"&gt;FOOL&lt;/a&gt; on January 13th, or at &lt;a href="http://www.langnetsymposium.com/"&gt;Lang.Net 2008&lt;/a&gt; in Redmond in late January.&lt;br /&gt; &lt;br /&gt;I’m trying to make each of these talks somewhat different, but they will necessarily have some overlap. I hope that some of these talks will make it onto the net and make these ideas more accessible.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-4363727735499118288?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/4363727735499118288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=4363727735499118288' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/4363727735499118288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/4363727735499118288'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2007/12/more-on-modules.html' title='More on Modules'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-1781570237423980139</id><published>2007-12-16T17:25:00.000-08:00</published><updated>2007-12-16T17:49:55.093-08:00</updated><title type='text'>Lethal Injection</title><content type='html'>Some months ago, I wrote a &lt;a href="http://gbracha.blogspot.com/2007_08_01_archive.html"&gt;couple of posts&lt;/a&gt; about object construction and initialization. I made the claim that so-called dependency-injection frameworks (DIFs) are completely unnecessary in a language like Newspeak, and promised to expand on that point in a later post.  Four months should definitely qualify as “later”, so here is the promised explanation.&lt;br /&gt;&lt;br /&gt;I won’t explain DIFs in detail here - read &lt;a href="http://martinfowler.com/articles/injection.html"&gt;Martin Fowler’s excellent overview&lt;/a&gt; if you need an introduction. The salient information about DIFs is that they are used to write code that does not have undue references to concrete classes embedded in it. These references are usually calls to constructors or static methods. These concrete references create undue intermodule dependencies.&lt;br /&gt;&lt;br /&gt;The root of the problem DIFs address is that mainstream languages provide inadequate mechanisms to abstract over classes.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Terminology rant: DIFs should more properly be called &lt;span style="font-weight: bold;"&gt;dependee&lt;/span&gt;-injection frameworks. A dependency is a relationships between a dependent (better called &lt;span style="font-weight: bold;"&gt;depender&lt;/span&gt;) and a &lt;span style="font-weight: bold;"&gt;dependee&lt;/span&gt;. The dependencies are what we do not want in our code; we certainly don’t want to inject more of them. Instead, DIFs inject instances of the dependees, so the dependers don’t have to create them.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;DIFs require you write your code in a specific way, where you avoid creating instances of dependees. Instead, you make sure that there is a way to provide the dependee instance (in DIF terminology, to inject it) from outside the object.  You then tell the framework where and what to inject. The reason injection traffics in instances rather than the classes themselves is because there’s no good way to abstract over the classes.&lt;br /&gt;&lt;br /&gt;Having recapped DIFs, lets move on to Newspeak. Newspeak modules are defined in namespaces. Namespaces are simply objects that are required to be deeply immutable; they are stateless.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Tangent: This ensures that there is no global or static state in Newspeak, which gives us many nice properties.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Namespaces are organized like Java packages, as an inversion of the internet domain namespace. Unlike Java packages, sub-namespaces can see their enclosing namespace.&lt;br /&gt;&lt;br /&gt;A module is a top-level class, that is, a class defined directly within a namespace. Newspeak classes can nest arbitrarily, so a module can contain an entire class library or framework, which can in turn be subdivided into subsystems to any depth. Nested classes can freely access the lexical scope of their enclosing class.&lt;br /&gt;&lt;br /&gt;Modules, like all classes, are reified as objects that support constructor methods. Recall that in Newspeak, a constructor invocation is indistinguishable from an ordinary method invocation. Objects are constructed by sending messages to (invoking virtual methods on) another object. That object may or may not be a class; it makes no difference.  Hence all the usual abstraction mechanisms in the language apply to classes - in particular, parameterization.&lt;br /&gt;&lt;br /&gt;Here is a trivial top level class, modeled after &lt;a href="http://martinfowler.com/articles/injection.html#ANaiveExample"&gt;the motivating example for DIFs given in Fowler’s article&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;blockquote&gt;public class MovieLister = (&lt;br /&gt;|&lt;br /&gt; private movieDB = ColonDelimitedMovieFinder from:’movies.txt’.&lt;br /&gt;|)&lt;br /&gt;(&lt;br /&gt;   public moviesDirectedBy: directorName = (&lt;br /&gt;       ^movieDB findAll select:[:m |&lt;br /&gt;                           m director = directorName&lt;br /&gt;                        ].&lt;br /&gt;)&lt;/blockquote&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The idea is that MovieLister supports one method, moviesDirectedBy:, which takes a string that contains the name of a director and returns a collection of movies directed by said director. The point of Fowler’s example is that there is an undesirable dependency on a class, ColonDelimitedMovieFinder, embedded in MovieLister. If we want to use a different database, we need to change the code.&lt;br /&gt;&lt;br /&gt;However, this code won’t actually work in Newspeak. The reason is that the enclosing namespace is not visible inside a Newspeak module. Any external dependencies must be made explicit by passing them to the module as parameters to a constructor method. These parameters could be other modules, namespaces, or classes and instances of any kind.&lt;br /&gt;&lt;br /&gt;In this specific case, ColonDelimitedMovieFinder cannot be referenced from MovieLister. If we try and create a MovieLister by writing: MovieLister new, creation will fail with a message not understood error on ColonDelimitedMovieFinder. We’d have to declare a constructor for MovieLister with the movie finder as a parameter:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;public class MovieLister finderClass: MovieFinder = (&lt;br /&gt;|&lt;br /&gt; private movieDB = MovieFinder from:’movies.txt’.&lt;br /&gt;|)&lt;br /&gt;(&lt;br /&gt;   public moviesDirectedBy: directorName = (&lt;br /&gt;       ^movieDB findAll select:[:m |&lt;br /&gt;                           m director = directorName&lt;br /&gt;                        ].&lt;br /&gt;)&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;At this point, we can immediately see that we can replace ColonDelimitedMovieFinder with any class that supports the same interface, which was the object of the entire exercise. Newspeak won’t let you create a module with concrete external dependencies, because that wouldn’t really be a module, would it?&lt;br /&gt;&lt;br /&gt;In Newspeak code in a module doesn’t have any concrete external dependencies, and no dependees need to be injected. What’s more we can subclass or mix-in any class coming in as a parameter - something a DIF won’t handle.&lt;br /&gt;&lt;br /&gt;What about a subsystem within a module? What if I don’t want it using the same name binding as the enclosing module?  I can explicitly parameterize my subsystem,  though that requires pre-planning. &lt;br /&gt;&lt;br /&gt;I can also override any class binding in a subclass. Newspeak is message-based, so all names are late-bound. Hence any reference to the name of a class can be overridden in a subclass. Classes can be overridden by methods or slots or other classes in any combination. So even if you do not explicitly parameterize your code to allow for another class to be used to construct an object, you can still override the binding of the class name as necessary.&lt;br /&gt;&lt;br /&gt;In summary, Newspeak is designed to support, even induce, loose coupling. That’s the point of message based programming languages. DIFs are an expedient technique to reduce code coupling in the sad world of mainstream development, but in a language like Newspeak, they are pointless.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-1781570237423980139?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/1781570237423980139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=1781570237423980139' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1781570237423980139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/1781570237423980139'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2007/12/some-months-ago-i-wrote-couple-of-posts.html' title='Lethal Injection'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-5103070001746073480</id><published>2007-09-25T11:50:00.001-07:00</published><updated>2007-09-25T14:23:47.056-07:00</updated><title type='text'>Executable Grammars</title><content type='html'>Way back in January, &lt;a href="http://gbracha.blogspot.com/2007/01/parser-combinators.html"&gt;I described a parser combinator library&lt;/a&gt; I’d built in Smalltalk. Since then, we’ve moved from Smalltalk to Newspeak,  and refined the library in interesting ways.&lt;br /&gt;&lt;br /&gt;The original  parser combinator library has  been a great success, but grammars built with it were still polluted by two solution-space artifacts. One is the need to use self sends, as in&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;id := self letter, [(self letter | [self digit]) star]&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;In Newspeak, self sends can be implicit, and so this problem goes away. We could write&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;id:: letter, [(letter | [digit]) star]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The other problem is the use of blocks. We’d much rather have written&lt;br /&gt;&lt;br /&gt;id:&lt;span style="font-style: italic;"&gt;: letter, (letter | digit) star&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The solution is to have the framework pre-initialize all slots representing productions with a forward reference parser that acts as a stand-in for the real parser that will be computed later.&lt;br /&gt;&lt;br /&gt;When the tree of parsers representing the grammar has been computed, it will contain stand-ins exactly in those places where forward references occurred due to mutual recursion. When such a parser gets invoked, it looks up the real parser (now stored in the corresponding slot) and forwards all parsing requests to it.&lt;br /&gt;&lt;br /&gt;Our parsers are now entirely unpolluted by solution-space boilerplate, so I feel justified in calling them executable grammars. They really are executable specifications, that can be shared among all the tools that need access to a language’s grammar.&lt;br /&gt;&lt;br /&gt;Below is a small but complete grammar in Newspeak:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;class ExampleGrammar1 = ExecutableGrammar (&lt;br /&gt;|&lt;br /&gt; digit = self charBetween: $0 and: $9.&lt;br /&gt; letter = (self charBetween: $a and: $z) | (self charBetween: $A and: $Z).&lt;br /&gt; id = letter, (letter | digit) star.&lt;br /&gt; identifier = tokenFor: id.&lt;br /&gt; hat = tokenFromChar: $^.&lt;br /&gt; expression = identifier.&lt;br /&gt; returnStatement = hat, expression.&lt;br /&gt;|&lt;br /&gt;) ()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you want to understand all the details, check out &lt;a href="http://bracha.org/executableGrammars.pdf"&gt;this paper&lt;/a&gt;; if you can, you might also look at my &lt;a href="http://jaoo.dk/"&gt;JAOO &lt;/a&gt;2007 &lt;a href="http://bracha.org/newspeak-parsers.pdf"&gt;talk&lt;/a&gt;, which also speculates on how we can make things look even nicer, e.g.:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;class ExampleGrammar3 = ExecutableGrammar (&lt;br /&gt;|&lt;br /&gt;digit = $0 - $9.&lt;br /&gt;letter = ($a - $z) | ($A - $Z).&lt;br /&gt;id = letter, (letter | digit) star.&lt;br /&gt;identifier = tokenFor: id.&lt;br /&gt;expression = identifier.&lt;br /&gt;returnStatement = $^, expression.&lt;br /&gt;|&lt;br /&gt;)() &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-5103070001746073480?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/5103070001746073480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=5103070001746073480' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5103070001746073480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/5103070001746073480'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2007/09/executable-grammars.html' title='Executable Grammars'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-2103408822566191738</id><published>2007-08-15T13:30:00.000-07:00</published><updated>2007-08-18T12:01:14.544-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='constructors'/><title type='text'>Object Initialization and Construction Revisited</title><content type='html'>In my last post, which discussed object initialization and construction,  I had promised to come back to the topic and clarify it with concrete examples. I've finally found time to do that; hopefully I will dispel some of the misunderstandings that the last post engendered, no doubt replacing them with fresh, deeper misunderstandings.&lt;br /&gt;&lt;br /&gt;Below is a standard example - a class representing points in the plane. What’s non-standard is that it is written in &lt;span style="font-weight: bold;"&gt;Newspeak&lt;/span&gt;, an experimental language in the style of Smalltalk, which I and some of my colleagues are working on right now.  In cases where the syntax is non-obvious, I’ll use comments (Pascal style, like so: &lt;span style="font-style: italic;"&gt;(* this is a comment *)&lt;/span&gt;) to show how a similar construct might be written in a more conventional (and less effective) notation.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;class Point2D x: i y: j = ( &lt;br /&gt;(* Javanese version might look like this : &lt;br /&gt;   class Point2D setXY(i, j) { ...} *)&lt;br /&gt;(*A class representing points in 2-space” *)&lt;br /&gt;|&lt;br /&gt;  public x ::= i.&lt;br /&gt;  public y ::= j.&lt;br /&gt;|&lt;br /&gt;) (    (* instance side *)&lt;br /&gt;&lt;br /&gt;  public printString = (&lt;br /&gt;    ˆ ’ x = ’, x printString, ’ y = ’, y printString&lt;br /&gt;  ) &lt;br /&gt;)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;this declaration introduces the class &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt;.  The class name is immediately followed by a message pattern (method signature for readers of Javanese) &lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;x: i y: j&lt;/span&gt;&lt;/span&gt;.  This pattern describes the primary constructor message for the class. The pattern introduces two formal parameters, &lt;span style="font-style: italic;"&gt;i&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;j&lt;/span&gt;, which are in scope in the class body. The result of sending this message to the class is a fresh instance, e.g.:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;Point2D x: 42 y: 91 &lt;br /&gt;(* In Javanese, you might write Point2D.setXY(42, 91);  &lt;br /&gt;   But don’t even think of interpreting setXY as a static method!&lt;br /&gt;*)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;yields a new instance of &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt; with &lt;span style="font-style: italic;"&gt;x = 42&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;y = 91&lt;/span&gt;. The message causes a new instance to be allocated and executes the slot initializers for that instance, in this case&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;x ::= i.&lt;br /&gt;y ::= j.&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The slots are accessed only through automatically generated getters (&lt;span style="font-style: italic;"&gt;x&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;y&lt;/span&gt;) and setters (&lt;span style="font-style: italic;"&gt;x:&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;y:&lt;/span&gt;).&lt;br /&gt;&lt;br /&gt;How is all this different from mainstream constructors?&lt;br /&gt;Because an instance is created by sending a message to an object, and not by some special construct like a constructor invocation, we can replace the receiver of that message with any object that responds to that message. It can be another class (say, an implementation based on polar coordinates), or it can be a factory object that isn’t a class at all. &lt;br /&gt;&lt;br /&gt;Here is a method that takes the class/factory as a parameter&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;makePoint: pointFactory = (&lt;br /&gt;(* In Javanese: &lt;br /&gt;   makePoint(pointFactory) {&lt;br /&gt;     return pointFactory.setXY(100, 200)&lt;br /&gt;   } &lt;br /&gt;*)&lt;br /&gt;  ^pointFactory x: 100 y: 200&lt;br /&gt;)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We can invoke this so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;makePoint: Point2D&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;but also so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;makePoint: Polar2D&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;where &lt;span style="font-style: italic;"&gt;Polar2D&lt;/span&gt; might be written as follows:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;class Polar2D rho: r theta: t = (&lt;br /&gt;(* A class representing points in 2-space”*)&lt;br /&gt;|&lt;br /&gt;  public rho ::= r.&lt;br /&gt;  public theta ::= t.&lt;br /&gt;|&lt;br /&gt;) (   (* instance side *)&lt;br /&gt;  public x = ( ^rho * theta cos) (* emulate x/y interface *)&lt;br /&gt;  public y = (^rho * theta sin)&lt;br /&gt;  ...&lt;br /&gt;  public printString  = (&lt;br /&gt;    ˆ ’ x = ’, x printString, ’ y = ’, y printString&lt;br /&gt;  )&lt;br /&gt;) : (  (* class side begins here*)&lt;br /&gt;  public x: i y: j = (&lt;br /&gt;    | r t |&lt;br /&gt;    t := i arcCos.&lt;br /&gt;    r := j/ t sin.&lt;br /&gt;    ˆrho: r theta: t&lt;br /&gt;  )&lt;br /&gt;)&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here, &lt;span style="font-style: italic;"&gt;Polar2D&lt;/span&gt; has a secondary constructor, a class method &lt;span style="font-style: italic;"&gt;x:y:&lt;/span&gt;, which will be invoked by &lt;span style="font-style: italic;"&gt;makePoint:&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;You cannot do this with constructors or with static factories; you simply cannot abstract over them.&lt;br /&gt;&lt;br /&gt;You could use reflection in Java, passing the &lt;span style="font-style: italic;"&gt;Class&lt;/span&gt; object as a parameter and then searching for a constructor or static method matching the desired signature. Even then, you would have to commit to using a class. Here we can use any object that responds to the message &lt;span style="font-style: italic;"&gt;x:y:&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Using Java core reflection in this case is awkward and verbose, and historically hasn’t been available on configurations like JavaME. And it doesn’t work well with proxy objects either (see the &lt;a href="http://www.bracha.org/mirrors.pdf"&gt;OOPSLA 2004 paper&lt;/a&gt; we wrote for details).  What’s more, you may not have the right security permissions to do it. The situation is not much better with the VM from the makers of Zune (tm) either.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;Zune is a trademark of Microsoft Corporation.  Microsoft is also a trademark of Microsoft Corporation. But GNU’s not Unix&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Alternatively, you could also define the necessary factory interface, implement it with factory classes, create factory objects and only pass those around.  You’d have to do this for every class of course, whether you declared it or not. This is tedious, error prone, and very hard to enforce. The language should be doing this for you.&lt;br /&gt;&lt;br /&gt;So far, we’ve shown how to manufacture instances of a class. What about subclassing? This is usually where things get sticky.&lt;br /&gt;&lt;br /&gt;Here’s a class of 3D points&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;class Point3D x: i y: j z: k = Point2D x: i y: j (&lt;br /&gt;(* A class representing points in 3-space *)&lt;br /&gt;| public z ::= k. |&lt;br /&gt;)  (* end class header *)&lt;br /&gt;(  (*begin instance side *)&lt;br /&gt;   public printString = (&lt;br /&gt;    ˆsuper printString, ’ z = ’, z printString&lt;br /&gt;  )&lt;br /&gt;)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;One detail that’s new here is the superclass clause: &lt;span style="font-style: italic;"&gt;Point3D&lt;/span&gt; inherits from &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt;, and calls &lt;span style="font-style: italic;"&gt;Point2D’s&lt;/span&gt; primary constructor.  This is a requirement, enforced dynamically at instance creation time. It helps ensure that an object is always completely initialized.&lt;br /&gt;&lt;br /&gt;Unlike Smalltalk, one cannot call a superclass’ constructors on a subclass. This prevents you from partially instantiating an object, say by writing:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;Point3D x: 1 y: 2  (* illegal! *)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;without initializing &lt;span style="font-style: italic;"&gt;z&lt;/span&gt; as intended. Also, unlike Smalltalk, there’s no instance method that does the initialization on behalf of the class object. So you cannot initialize an object multiple times, unless the designer deliberately creates an API to allow it. The idea is to ensure every object is initialized once and only once, but without the downsides associated with constructors.&lt;br /&gt;&lt;br /&gt;Preventing malicious subclasses from undermining the superclass initialization takes care. We’re still considering potential solutions. The situation is no worse than in Java, it seems, and we may be able to make it better.&lt;br /&gt;&lt;br /&gt;A different concern is that the subclass must call the primary constructor of the superclass. So what happens when I want to change the primary constructor? Say I want to change &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt; to use polar representation. Can I make &lt;span style="font-style: italic;"&gt;rho:theta:&lt;/span&gt; the primary constructor? How can I do this without breaking subclasses of &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt;, such as &lt;span style="font-style: italic;"&gt;Point3D&lt;/span&gt;? We can't do it directly yet (though we should have a fix for that in not too long), but I can redefine &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt;&lt;br /&gt;as&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;class Point2D x: i y: j =  Polar2D rho:  ... theta: ... = ()()&lt;br /&gt;: ( “class side begins here”&lt;br /&gt;(* secondary constructor *)&lt;br /&gt;  public rho: r theta: t = (&lt;br /&gt;    ˆx: r * t cos y: r * t sin&lt;br /&gt;  )&lt;br /&gt;)&lt;/pre&gt; &lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now anyone who uses a &lt;span style="font-style: italic;"&gt;Point2D&lt;/span&gt; gets a point in polar representation, while preserving the existing interface. And anyone who wants to can of course create polar points using the secondary constructor. I can also arrange for that constructor to return instances of &lt;span style="font-style: italic;"&gt;Polar2D&lt;/span&gt; directly:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;pre&gt;public rho: r theta: t = (&lt;br /&gt;  ˆPolar2D rho: r theta: t&lt;br /&gt;)&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you find this interesting, you might want to read a short &lt;a href="http://dyla2007.unibe.ch/?download=dyla07-Gilad.pdf"&gt;position paper&lt;/a&gt; I wrote for the &lt;a href="http://dyla2007.unibe.ch/"&gt;Dynamic Languages Workshop&lt;/a&gt; at ECOOP. It only deals with one specific issue regarding the interaction of nested classes and inheritance, and it’s a just a position paper describing work in progress, but if you’ve gotten this far, you might take a look.&lt;br /&gt;&lt;br /&gt;I still haven’t explained why I see no need for dependency inversion frameworks. The short answer is that because &lt;span style="font-weight: bold;"&gt;Newspeak&lt;/span&gt; classes nest arbitrarily, we can define a whole class library nested inside a class, and parameterize that class with respect to any external classes the library depends on. That probably needs more explanation; indeed, I think there’s a significant academic paper to be written on the subject. Given the length of this post,  I won’t expand on the topic just yet.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-2103408822566191738?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/2103408822566191738/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=2103408822566191738' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/2103408822566191738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/2103408822566191738'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2007/08/object-initialization-and-construction.html' title='Object Initialization and Construction Revisited'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2447174102813539049.post-7589575897199820043</id><published>2007-06-24T20:47:00.000-07:00</published><updated>2007-06-24T20:53:01.278-07:00</updated><title type='text'>Constructors Considered Harmful</title><content type='html'>In mainstream object oriented programming languages, objects are created by invoking constructors. This is rather ironic, since you can say a lot about constructors, but you cannot honestly say that they are object oriented. Ok, so what? Isn’t “object-oriented” just an old buzzword? If constructors work well, who cares?&lt;br /&gt;&lt;br /&gt;Of course, the problem is that constructors don’t work very well all. Understanding why helps to understand what “object oriented” really means, and why it is important.&lt;br /&gt;&lt;br /&gt;Constructors come with a host of special rules and regulations: they cannot be overridden like instance methods; they need to call another constructor, or a superclass constructor etc. Try defining mixins in the presence of constructors - it’s tricky because the interface for creating instances gets bundled with the interface of the instances themselves. &lt;br /&gt;&lt;br /&gt;The basic issue is a failure of abstraction, as Dave Ungar put it in his OOPSLA keynote in 2003.&lt;br /&gt;&lt;br /&gt;Suppose we have a constructor C(x) and code that creates objects by calling it. What happens if we find that we actually need to return an instance of a class other than C? For example,  we might want to lazily load data from secondary storage, and need to return some sort of placeholder object that behaves just like a C, but isn’t? What if we want to avoid reallocating a fresh object, and use a cached one instead?&lt;br /&gt;&lt;br /&gt;So it’s clear that you don’t want to publicize your constructors to the clients of your API, as all they can do is tie you down.&lt;br /&gt; &lt;br /&gt;The standard recommended solution is to use a static factory. This however, does nothing to help the other victims of constructors - their callers. As a consumer of an API, you don’t want to use constructors: they induce a tight coupling between your code and specific implementations. You can’t abstract over static methods, just as you can’t abstract over constructors. In both cases, there is no object that is the target of the operation and a conventional interface declaration cannot describe it.  The absence of an object means that constructors don’t have the benefits objects bring - dynamic binding of method calls chief among these. Which is why constructors and static methods don’t work well, and incidentally, aren’t object oriented.&lt;br /&gt;&lt;br /&gt;Having dismissed constructors and static factories, it seems we need to define a factory class whose instances will  support an interface that includes a method that constructs the desired objects. How will you create the factory object? By calling a constructor? Or by defining a meta-factory? After how many meta-meta-meta- .. meta-factories do you give up and call a constructor?&lt;br /&gt;&lt;br /&gt;What about using a dependency injection framework (DIF)? Ignoring the imbecilic name, I think that if you’re stuck with a mainstream language, that may be a reasonable work around. It requires a significant degree of preplanning, and makes your application dependent on one more piece of machinery that has nothing to do with the actual problem the application is trying to solve. On the positive side, it helps guarantee employment for software engineers. That said, it’s important to understand that DIFs are just a work around for a deficiency in the underlying language. &lt;br /&gt;&lt;br /&gt;So why not get rid of constructors and have a class declaration create a factory object instead? Well, Smalltalk did just that a generation ago. Every time you define a class, you define the factory object for its instances. I won’t explain the Smalltalk metaclass hierarchy here. Suffice to say that it is a thing of beauty, resolving a potential infinite regress with an elegant circularity.&lt;br /&gt;&lt;br /&gt;Despite this, Smalltalk still does not provide an ideal solution for creating and initializing instances. While it preserves abstraction, it does not easily enable the class to ensure that every instance will always be properly initialized, or that initialization will take place only once. To be sure, these are difficult goals, and Java, despite enormous efforts and complexity focused on these goals, does not fully achieve them either. However, it comes close - at the cost of abstraction failure brought about by the use of constructors.&lt;br /&gt;&lt;br /&gt;So can we do better? Of course.  The solution combines elements of Scala constructors (which are cleaner than Java constructors) with elements of the Smalltalk approach.&lt;br /&gt;&lt;br /&gt;Scala defines a class as a parametric entity, with formal parameters that are in scope in the class body.  The class name, together with its formal parameters, define the primary constructor of the class. This allows the instance to initialize itself, accessing the parameters to the constructor without exposing an initialization method that can be called multiple times on an instance. The latter is one of the problems in Smalltalk.&lt;br /&gt;&lt;br /&gt;We use a similar device to provide access to the constructor parameters from within the instance. However, we require that the class provide a method header for its primary constructor. Instead of creating instances by a special construct (constructor invocation) as in Java or Scala, we create them via method invocation. The class declaration introduces a factory object that supports the primary constructor as an instance method.&lt;br /&gt;&lt;br /&gt;Because we create objects only by invoking methods on other objects, we preserve abstraction. We can create objects by invoking the constructor method on a parameter. We can always define alternative factory objects that support the same constructor method with different behavior, and pass them instead of the class. Furthermore, using a message based programming language, references to the class’ name are always virtual, and can be overridden.&lt;br /&gt;&lt;br /&gt;Unlike Smalltalk, the factory class is not a subclass of the superclass factory class. This prevents the possibility of calling superclass constructors and thereby creating partially initialized objects (doing this requires special effort in Smalltalk - one has to manually override the superclass constructors so that they fail; this is tedious and error prone, and not done much in practice).&lt;br /&gt;&lt;br /&gt;I know I should be writing examples to make this all clear.  However, this post is getting long, so that will wait for another post. I’ll be speaking about some of this work next month a the dynamic language workshop at ECOOP. By then, I imagine I’ll put out some examples of what we’ve been doing these past few months.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2447174102813539049-7589575897199820043?l=gbracha.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gbracha.blogspot.com/feeds/7589575897199820043/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2447174102813539049&amp;postID=7589575897199820043' title='29 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7589575897199820043'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2447174102813539049/posts/default/7589575897199820043'/><link rel='alternate' type='text/html' href='http://gbracha.blogspot.com/2007/06/constructors-considered-harmful.html' title='Constructors Considered Harmful'/><author><name>Gilad Bracha</name><uri>http://www.blogger.com/profile/17934280339206214042</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='04041253196112154746'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>29</thr:total></entry></feed>