11 Sep 2012

Interfaces, nested classes and inheritance

In one of my previous posts I said that interfaces in CoffeeScript can be written as usual classes and extended by other classes (if only you want to write Java-style interface, of course). I was wrong.
There are two main problems with Java-style interfaces in CoffeeScript:
  1. Every class in Java can have only one parent but implement as many interfaces as you like. CoffeeScript classes can extend only one class. That's it. If CoffeeScript allowed multiple inheritance everything would be as easy as I said before. However, it doesn't.
  2. Every class in Java is instance of all the interfaces it implements. In CoffeeScript instanceof operator return true only for classes in objects' prototype chain. No exclusions, no possibility to override.
So, I've tried to solve both of this problems.

6 Sep 2012

Just few words

I just want to tell two things.
Firstly, I've create git repository for my examples. You can find them now at  http://github.com/mariyadavydova/coffeescript_nodejs_examples (the link is always available above, in list of pages).
And secondly, I've changed syntax highlighter. I use SyntaxHighlighter now. I have a small problem: I didn't found CoffeeScript theme and use Ruby instead, thus some things are highlighted incorrectly. I also use Bash theme for console output, so not be surprised if something is suddenly highlighted there. I will work on these issues but even now I see that by blog looks much more better.
Hope these innovations make my blog more useful for you!

To be continued...

Enums

Enums are the next problem in Java to CoffeeScript conversion. Let's see, how to write a enum in CoffeeScript.
The first way is quite simple and obvious. If the only thing that we want from enum is providing a set of values that we can compare with each other, we can write it as follows:
Seasons = {WINTER : 0, SPRING : 1, SUMMER : 2, AUTUMN : 3}
Simple, isn't it? We just declare a new Object, that contains several properties - our enum constants. Now we can use it:
commentSeason = (season) ->
  switch season
    when Seasons.WINTER
      console.log "The coldest season"
    when Seasons.SUMMER
      console.log "The hottest season"
    else
      console.log "The rainy season"

commentSeason Seasons.WINTER
commentSeason Seasons.SUMMER
commentSeason Seasons.SPRING
console.log "Are spring and autumn the same? %s",
  if (Seasons.SPRING == Seasons.AUTUMN) then "Yes" else "No"
The output is:
The coldest season  
The hottest season  
The rainy season  
Are spring and autumn the same? No  
Well, this variant is incredibly simple and will be enough for most of the situations when you decide to use enum in your code. But what should you do if you want to use some of these Java enum features like getting constant name, or finding constant by name, or using constructors for creating constants and adding methods to them? My solution is below.

5 Sep 2012

Nested classes

As I've already told in previous post, I need to use nested classes. The main problem is that nested classes in CoffeeScript behave not like nested classes in Java. In Java any nested class can access fields and methods of the outer class instance in that the nested class instance was created. It is widely used, e.g. for iterators. In CoffeeScript you can define a nested class and create its' instance from the outer one but this nested class can't access outer's fields and methods.
So, you need somehow tell the nested class instance who created it. I've found two ways of solution.
The first one is C# way. You pass outer class instance reference to nested class constructor. This works fine. I do not like this way because you need to modify every constructor in every nested class (CoffeeScript allows you to determine only one constructor for each class, but you can use static methods as constructor; I will tell about this later).

Anonymous classes

I must apologise for the long silence. Every time I make a plan of what I want to tell about in some blog, I miss lots of things I could tell but didn't because they are not planned, and do not tell about planned things because do not have an appropriate mood for that. Anyway, I've decided to post here what I want to in every moment, though it will be a mess.
I had to work much with classes in CoffeeScript for last few days. The main problem of my work is that I convert Java code to CoffeeScript. Thus I need to have all the Java-like constructions in my disposal: anonymous classes, interfaces, nested classes, enums...
To be honest, interfaces are absolutely useless in CoffeeScript (as you can never declare a typed variable) but in Java they sometimes contain constants that you may need in other parts of code. So, making interface in CoffeeScript is pretty simple - you just declare the class with all the necessary fields (and empty method - if you wish to make your code more readable and self-documented).
The interesting thing I found out several days ago is that CoffeeScript allows making anonymous classes quite easily, though I failed to find this option in its' official docs (http://coffeescript.org/).