11 Sept 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 Sept 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 Sept 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/).

22 Mar 2012

Classes: introduction

First of all, I want to say several words about classes in CoffeeScript. In fact, any class here is just a map, where keys are field names and values are anything you want - numbers, strings, functions... Here is very simple example:
class Animal
  name: undefined
  @numberOfRegisteredAnimals: 0
  
  constructor: (@name) ->
    Animal.numberOfRegisteredAnimals += 1

  @printNumberOfRegistedAnimals: () ->
    console.log "The number of registered animals is equal to", 
            Animal.numberOfRegisteredAnimals 
  
  sayName: () ->
    console.log "My name is " + @name + ", " + @voice() 

class Cat extends Animal
  voice: () ->
    return "meow!!!"

class Dog extends Animal
  voice: () ->
    return "woof!!!"

Animal.printNumberOfRegistedAnimals()
cat = new Cat "Kuzya"
cat.sayName()
Animal.printNumberOfRegistedAnimals()
dog = new Dog "Sharik"
dog.sayName()
Animal.printNumberOfRegistedAnimals()

20 Mar 2012

Why?

Welcome dear reader!
I open this blog to share my experience of programming on CoffeeScript with NodeJS and doing other things.
As you probably know, CoffeeScript is a little language that compiles into JavaScript (as it is said here). As for NodeJS, it is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications (nodejs.org). There are tons of blogs, articles and tutorials devoted to both of them. Why do I open a new one?
Well, because I have very specific project that I work on now. I want to implement the transactional key-value store based on event-driven architecture. EDA is nice when you, well, want not to stop work while waiting for server response. However, when you want to write lots of data in one file you are in a trouble, because data in NodeJS is written asynchronously and the order of writing is not quarantined. Generally speaking, when you start working with EDA it firstly breaks your brain and then, if you are still alive, causes lots of problems and additional lines in your code.
The one more funny thing in my project is that I have complete database written on Java and need to repeat interfaces as much as possible. This causes even more problems. How to make function overload in the language where OOP is based on prototypes? How to make several constructors for one class? Why changing data in one class instance can affect other instances?
I will try to answer these questions and many others. I will not explain the very basics (you can easily find them yourself) except, maybe, the things that were kind of problem for me. And I hope to see your comments!