I am a software engineer at Google on the Android project and the creator of the Java testing framework TestNG. When I'm not updating this weblog with various software-related posts or speaking at conferences, I am busy snowboarding, playing squash, tennis, golf or volleyball or scuba diving. Cedric is a DZone MVB and is not an employee of DZone and has posted 90 posts at DZone. You can read more from them at their website. View Full User Profile

The Fan Programming Language

06.19.2008
| 10562 views |
  • submit to reddit

I just came across a language that I never heard of before -  Fan. After a few hours surveying the language, I have to say that Fan seems to get a lot of things right. Here is a short list of its features:

  • Reuses the C# syntax for properties.
  • Optional static typing and type inference. Use "." to enable static typing, "->" for dynamic typing. Non-existent methods are sent to a catch-all "trap" method, which is the equivalent of Ruby's method_missing. It is also possible to create types at runtime, which Fan calls -- a bit confusingly albeit accurately -- "dynamic types".
  • Currying with the & operator.
  • Closures. I also like the fact that closures and Fun functions can be transparently converted to methods (think "delegates") via currying. See below for an example.
  • override and virtual keywords (as opposed to Java, Fan methods are not virtual by default).
  • Untyped annotations (called Facets). Facets are pairs of (key, value) and they can be introspected at run time. The system reserves a few facet names for itself, such as @transient or @serializable.
  • Immutability (classes can be created const and objects can be made immutable).
  • Mix-ins (represented by interfaces that can have implementation). Fan mix-ins can have properties, although these need to be abstract (an approach that I find less restrictive than Scala's).
  • Runs on both the JVM and .Net (wow!).
  • Uses := for assignment. I remember liking this symbol a lot when I was programming in Pascal and Modula 2, but I haven't used it in about twenty years, so it might be a bit disconcerting at first.
  • Interesting approach to modularity leveraging REST URI's to denote namespaces. This area is still in development, but here is a quote that summarizes Fan's approach:


    Java and .NET to a lesser degree separate the concepts of namespace and deployment. For example in Java packages are used to organize code into a namespace, but JAR files are used to organize code for deployment. The problem is there isn't any correspondence between these concepts. This only exacerbates classpath hell - you have a missing class, but the class name doesn't give you a clue as to what JAR file the class might live in.
  • "once" methods (a feature I can only remember ever seeing in Eiffel).
  • Constructor declarations need to be prefixed with the keyword "new" but can take any arbitrary name, although the prefix "make" is commonly used. Constructing an object is done by invoking that constructor as a static method on the type (like Ruby), which I find intuitive.
  • Good-looking web site with extended documentation.
Here are a few code samples illustrating the main features of Fan.


Constructor

class Point
{  
  new make(Int x, Int y) {    this.x = x; this.y = y;   }  
  Int x  
Int y } // make a point pt := Point.make(30, 40)


 

Properties

class Person
{  
    Str name  
    Int age {  set  { checkAge(val);  @age = val  } }
}

In the code above, @age is used to refer to the actual field and not the property.

Facet

@version=Version("1.2")
@todo=["fix it", "really fix it"]
class Account
{
}

You can pass methods when closures are expected. For example, Thread.make expects a closure that takes a Thread in parameters and returns an Obj:

new make(Str name := null, |Thread -> Obj| run := null)

You don't need to create an object to invoke that method:

static Void readerRun(Thread t) { 
// ...
}
reader := Thread.make("reader", &readerRun).start

Here are some of my pet peeves about Fan:

  • The typing syntax. I would have preferred "f: File->Bool" instead of "File f->Bool" so that formal parameters and the type of the closure can be more easily told apart.

     

  • No generics. All successful languages eventually get there, so I hope Fan will as well, but the creators seem to be hostile to the idea:
    Our philosophy is that generics are a pretty complicated solution to a fairly insignificant problem.
    I'm sure they will change their mind in due time, and in the meantime, List, Map and Func offer a limited type of genericity.

     

  • There doesn't seem to be any IDE support. I'm not sure how old Fan is, but the documentation states that it is approaching v1.0, so I'm hoping that the Fan developers will learn from Groovy's mistakes and focus their attention on IDE support as soon as possible.

     

  • I prefer for constructors to have a fixed name instead of one arbitrarily chosen by the developer. While class declarations make it easy to pinpoint which methods are constructors (just look for "new"), it's not as easy to tell when you are on the call site, since a constructor invocation cannot be differentiated from a static call. From that respect, I still find that Ruby's approach (Person.new) represents the best of both worlds, although I think it could be perfected even further by eliminating the duality "new"/"init" completely. For example, instead of:

    // Valid Fan
    class Point {  
       new make(Int x, Int y) { this.x = x; this.y = y; }  
       Int x  
       Int y
    }
    p = Point.make(2, 3)


    Why not:

    // Invalid Fan
    class Point 
    {  new(Int x, Int y) { this.x = x; this.y = y; }  
        Int x  
        Int y
    }
    p = Point.new(2, 3)

    ? (the same remark can be made about Ruby)

Except for partial generics, Fan doesn't seem to add as many innovations as its predecessors did, which is not necessarily a bad thing. It's pretty clear that the Fan authors have a solid knowledge of multiple languages and they ported these concepts, sometimes hardly modified, into Fan. And speaking of porting, I am thoroughly impressed by the fact that Fan works on both the JVM and .Net.

Overall, it looks like Fan is being driven by motivations that are very similar to what started Groovy: pick the best features of the current popular languages and try to blend them into a coherent set. I find myself particularly fond of Fan because I happen to agree with a lot of the choices that the designers made, which is exactly how I felt about Groovy in the beginning. Of course, Fan has the advantage of hindsight and it borrows from a few additional languages than Groovy did (namely, C#, Scala and a bit of Erlang), so I find the result quite promising.

From http://beust.com/weblog

Published at DZone with permission of Cedric Beust, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Serge Bureau replied on Fri, 2008/06/20 - 7:49am

JavaLobby ?

Again, have a languages section and leave this one alone.

Who cares about fan, knife , fork, burgers ? Do you get it ? It's JavaLobby 

Mikael Grev replied on Fri, 2008/06/20 - 8:17am

Hey Serge, chilld down. Fan is interesting because it has been touted as a good candidate for "Java 3".

Also, it is not JavaExclusiveLobby. ;)

Cheers,
Mikael

Serge Bureau replied on Fri, 2008/06/20 - 8:40am in response to: Mikael Grev

Hello Mikael,

 I am failing to see your point (and of many others it seems),

Thre is a lot of section now in DZone, so there is no more need to put every damn news about any subject in JavaLobby.

As far as Java 3, to me it is already there and is called Groovy that is only a library added.

I could not care less about fan.

Have a fan zone if you wish or a new languages zone, but there is a proliferation of useless subject at JavaLobby it's getting really annoying.

Mikael Grev replied on Fri, 2008/06/20 - 10:06am in response to: Serge Bureau

I think whining in the forum section will not do much good. Why not mail Rick directly instead?

You mention your opinions, but that is your opinions. I for instance, as a Java programmer, am interested in Fan. It is always good to know what is happening.

And, the title say pretty much what the subject is about, a new language. You don't need to click it.

I just think that it is pretty bad manners to hijack a thread like this with whining. If I was a moderator I would move it to another thread, one for opinions.

Serge Bureau replied on Fri, 2008/06/20 - 11:39am in response to: Mikael Grev

Why is there many Zones ? To separate issues.

I do not want new movies advertizing ?

Where is the squad that was filtering Javalobby before ? You are the one that hijacks  the Javalobby thread.

Go cry to Rick, you obviously do not want to understand, your next message will be a wheater report, "it is always good to know what is happening"

Stefan Schulz replied on Fri, 2008/06/20 - 6:05pm in response to: Serge Bureau

What's your point, Serge? Do you want DZone to open a damn new zone for every news you think not fitting to some existing zone? Just wonder.

Fan is an interesting language. It runs on the JVM (as well as on .NET!) and incorporates quite a lot of features being around as enhancement idea for Java. Another good overview is given by Stephen Colebourne on his blog.

 

phil swenson replied on Fri, 2008/06/20 - 6:41pm in response to: Serge Bureau

I agree with Serge.  I'm sick of learning.  New things frighten me.

 I do however enjoy all the messages that complain about things being off topic.

 

Perhaps we should have all submitted stories go through the Serge approval process?

Mohamed El-beltagy replied on Mon, 2008/06/23 - 2:16am

come on guys, you have been commenting about something that is irrelevant to the subject. I am totally Java developer too. And I sure don't like to learn new scary stuff. But at the same time:

- I have to know what else is there other than Java (Ruby, Groovy, Fan... etc).

- There is no other zone to post the blog. And of course, it woun't be that easy to either open a new zone for each blog entry nor to wait till a new one is opened.

Besides, if I am not interested in the title, why would I open and read the entry?!!

And to tell you the truth, Fan seems to be promising.. :)

Andres Almiray replied on Mon, 2008/06/23 - 12:21pm

<sarcasm>Yeah, give me Java-only news or gime death! </sarcasm>

Has anyone noticed that Fan is (perhaps) the first language to bridge the gap between Java and .Net platforms. I mean it is not a port of a previous existing language (Python,Ruby) to both platforms, but an specific language for both platforms. Scary but interesting, the next step into polyglot programming? once you cross over to the other platfor thanks to Fan, what will stop you to accommodate a specific need in said platform by coding in another language supported by that platform? Also consider what we may bring back to the Java platform (crosspollination anyone?)

John Denver replied on Tue, 2008/06/24 - 11:02am

I have a similar comment in another article, a girl promoting ruby and rails as always in Javalobby I think DZone guys mistaked to merge Javalobby with DZone, Javalobby is not the same as before!.

Like the first comment Who the F*ing hell care as a Java programmer and here in Javalobby about

FAN, FUN, ruby, fifi, burger or sh*t language!!. Please post this kind of articles in another zone. Create one that is called the new and wonderful NEXT BEST Thing coming into the programming world!.And in that zone you can talk about what is better and the greatest ruby or FAN or scala or fifi, burger or sh*t language.

This programming languages talkings I remember are like when I was in highschool talking some guys about cars heheh the ferrari 50 is better than mercedez because have a bigger motor and more hp's blah blah I just want a car to get the job done. And in programming or computer world that is Java.

 

 

 

 

 

Mikael Grev replied on Tue, 2008/06/24 - 11:11am in response to: John Denver

/I was in highschool/I am in highschool/

:)

Serge Bureau replied on Thu, 2008/06/26 - 8:58am in response to: Andres Almiray

As I sated in another discussion, polyglot programming is a stupid concept.

It makes debugging and supporting a nightmare.

As far as Java only news, that is what I expect from JavaLobby, there is other section for other things, like the Groovy one you help working which I like.

But defending the polyglot concept and the poluting of the JavaLobby section make me wonder if your the same person writing the nice Groovy articles ? 

Andres Almiray replied on Thu, 2008/06/26 - 11:44am in response to: Serge Bureau

Serge, I understand your position in wanting Java only news in JAVALobby, but the truth is: what Java news should JAVALobby consider? JVM only? JSL ony? or Java the language only? besides the whole point of polyglot programming (at least on the JVM) is that there exists a good level of integration with Java the language and other languages, not just the JVM or JSL. So how would you expect "pollution" (as you called it) not to be present in JAVALobby discussions?

And yes, I am the same guy from Groovy Zone. We at Groovy Zone posts cross zone articles with JAVALobby when it makes sense (not just because of polyglot programming), not all news from that zone make it to this one. The same thing happens with Ruby Zone and the tools zones (Eclipse, IDEA & NetBeans). The whole point of the zones network is to communicate what is out there, and as you have seen many news do not belong to a particular zone. I'm sorry you feel anger when reading an alledgedly non-Java post at JAVALobby, but believe me, there is more than 1 person looking at the moderation queue in every zone, so articles do not get promoted because of a personal whim.

Serge Bureau replied on Thu, 2008/06/26 - 1:15pm in response to: Andres Almiray

Hello Andres,

thanks for a reply that does address the issue.

I agree it is not a simple matter and is not so clean cut. But Fan is an easy reject.

Do not get me wrong, it definitely has it's place in DZone, but not in JavaLobby in my opinion.

I do not (and I am sure you neither) have the time to read all news about all subjects, I count on moderators to at least parse the entries. So a perfect "pollution filter" cannot be realised, but it could be much better.

While I do not think it is on purpose a good percentage of subject are not well classified.

How specific to Java is "Canonical Data Model " for example.

To me this is an unfortunate kind of spam.

Thank you for the reply, and I do appreciate your articles in the Groovy section.

Serge 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.