Skip to content

CompaJ Lang

Danila Rassokhin edited this page Mar 23, 2023 · 1 revision

CompaJ uses Groovy underhood with some tweaks to reduce code size. You can learn groovy on it's official documentation.

Types

By default all real numbers in Groovy are typed with BigDouble. But in CompaJ they are typed with double, you don't need to add d suffix.

n = 5.0 // BigDouble in Groovy
n = 5.0 // double in CompaJ

Types erasion

CompaJ automatically erases type before variable creation. If you write something like this:

String myString = "someString"

It will be translated to Groovy as

myString = "someString"

But type wont be erased from class fields. So next example wont be changed during translation:

class MyClass {
   private String myString
}

Object creation

To create object in CompaJ use : operator:

myModel = :BaseModel("MyModel")

You can omit braces if there is no arguments:

myModel = :BaseModel

Dynamic class changes

ComapJ allows you to add methods to classes dynamically.

Local

Local methods will be visible only after their definition. It means you can't you this methods in code above. To create local methods use next syntax:

ClassName:::methodName(arg0, arg1, ...) { }

Example

class MyClass {
  void m() {
    println "Hi"
  }
}

/**
c = :MyClass
c.say("Hello") // Error! Method say is not yet defined!
**/

MyClass:::say(String s) {
  println s
}

myClass = :MyClass
myClass.m() //Will print Hi
myClass.say("Hello") //Will print Hello

Global

Global methods will be visible everywhere no matter where they are defined. To create global methods use next syntax:

ClassName::!methodName(arg0, arg1, ...) { }

Example

class MyClass {
  void m() {
    println "Hi"
  }
}

myClass = :MyClass
myClass.m() //Will print Hi
myClass.say("Hello") //Will print Hello

MyClass:::say(String s) {
  println s
}
Clone this wiki locally