Archive for July, 2009

Static Typing

Sunday, July 26th, 2009

I’m not big on comparing the merits of different languages. Every language I’ve used serves a good purpose for some task. So I generally pick the best language for the task at hand.

In general, though, I always tend to prefer static languages for anything but a small or quick and dirty project. Especially when its a code base I intend to maintain for some time.

I just spent the weekend refactoring Fan’s JavaScript compiler to change how I model Fan fields in JavaScript. The way I had previously done it looked like this:

// Fan
class Foo { Int x := 5 }

// JavaScript
Foo.prototype.x$get = function() { return this.x; }
Foo.prototype.x$set = function(val) { this.x = val; }
Foo.prototype.x = 5;

Which isn’t great, but didn’t really have alot of options. But it seemed nice enough. It grouped the accessors and storage neatly together. And in most cases, since I can access the storage directly, the code overall was fairly clean.

Unfortunately that model made it very difficult to make certain use cases of fields and methods work (like overriding a const field with a method). So I changed the JavaScript to look like this:

// Fan
class Foo { Int x := 5 }

// JavaScript
Foo.prototype.x = function() { return this.m_x; }
Foo.prototype.x$ = function(val) { this.m_x = val; }
Foo.prototype.m_x = 5;

That change would have been a bit painful in any language, since there was just alot of code to update (the sys library and all the native implementations are written directly in JavaScript).

However, since JavaScript does not have static type checking, I pretty much had to tackle errors one-by-one using the test suite and Fresco (the web UI I’m developing at SkyFoundry).

This is probably the biggest reason I prefer static languages. When you have to make sweeping changes on a large code base, you simply can’t verify your changes 100% in any language. But a compiler with static type checking can help you tremendously. Especially for code that is difficult to unit test, like UIs.

Mounting a JavaFX applet from Javascript

Wednesday, July 1st, 2009

There’s not a whole lot of documentation surrounding the Javascript deployment scripts the new Java Plug-in and JavaFX use (actually I couldn’t find any). Luckily if you beautify the minimized code, its not too hard to figure out how things tick.

Out of the box, those scripts use document.write in place, which makes them very problematic if the script is not inlined in the initial HTML page. Trying to call them after the page is loaded will just nuke your document.

Luckily there’s a function (javaFxString) in the dtfx.js script that serializes the necessary code to a string which you can use:

var s = javafxString({
          codebase: "/s/fwt/javafx/dist/",
          archive: "Canvas.jar",
          draggable: true,
          width:  200,
          height: 200,
          code: "fan.fwt.Canvas",
          name: "Canvas",
          id: "app"
      });
someElement.innerHTML = s;

This will let you insert the JavaFx applet code anywhere in your DOM. I only tried this in IE8 so far (since this is a workaround for IE-only). But I assume this should work in all browsers.