Posts

Showing posts with the label Fantom

Pattern: How to start a thread of execution in Fantom

In Fantom, how do you start a new thread of execution? The pattern below is very simple and nice. const class Main { Void main() { svc := Actor(ActorPool()) { doSomething }.send(null) } Obj? doSomething() { // Do something like binding to a port and listening for requests. // Or you can call another method that does that. return null } } The code above might seem a little odd at first. All that it does is to create an Actor with a code block that will execute the doSomething method. By sending a dummy message (".send(null)"), the Actor instance is started. The key here is that sending a dummy message to an Actor starts the Actor. This same trick may be applicable to other Actor based languages as well.

Lazy initializing default values

One of the tasks you will come across often is to look up a value in a map, while providing a default value for the looked up value. For e.g. Properties prop = ... Object value = prop.get("key", "defaultValue") The issue with this pattern is that, what if arriving at default value is a costly operation. Even worse, there may be no need to compute the default value at all, may be because the get operation will always find a value in the properties. During such times "lazy evaluation" comes to the rescue. This is not a new pattern, it has been around for long time. Just that some APIs are written with lazy evaluation in mind, and some not. In the example above, instead of taking the value itself, if the get method takes an argument that is callable or a lambda function, then it can use that function only under the situation that the value is not present. The example below is from Fantom language when you attempt to get a value from a map. class M...

Initial thoughts on Fantom

Recently I started playing with Fantom language . So far my reaction is "Wow!". Its a beautiful language with a lot of cool things. I also ran into a few gotchas, but none that falls in the category of "ugly!". Here are the things I really liked about the language (not necessarily in any order): Before any technical merits, the first thing I liked is the documentation. Most of the time, when I look for some documentation for a open source project, they notoriously suck. Fantom is the second project whose documentation I really liked and found my way around most of the time. (The first one is SLF4J.) The language is statically typed, with room for dynamically invoking methods on objects using a mechanism called "trap". One central theme I observed in the language is being able to reliably create an immutable objects ("const" classes). This simplifies a lot of analysis when it comes to concurrent programming. Also the language supports cre...