Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Functions

Onoshko Dan edited this page Jul 3, 2015 · 6 revisions

You have possibility to declarate functions without function keyword, with type and modificators:

function f1() {
    return "classic js function definition";	
}
	
export void f2() {
    console.log("void function");	
}
	
covert window.f3() {
    console.log("dynamic function");
}

list of modificators:

  • covert to define unenumerable property (not function)
  • export to export function or property from module (read more)
  • async to async using with Promises (read more)

From Dart we have borrowed arrow-functions:

String helloString(name) => "Hello @name!";

You can define getters and setters by this way:

covert int get Math.rand() => 123;
covert int set Math.rand(int val) => console.log(val);

Arguments have positional or named types and can have default value:

hello(String name:) => console.log("Hello @name!");
hello(name: 'dangreen');                             // Hello dangreen!
		
hello(name: "World") => console.log("Hello @name!"); // Hello World!
hello();                                             // Error

// or

hello(String name = "World") => console.log("Hello @name!");
hello('dangreen');                                   // Hello dangreen!
hello();                                             // Hello World!

As in CoffeScript we can declare splated argument

info(name, skills...) {
    console.log("My name is @name, my skills:");
    skills.forEach((skill) => console.log("@skill,"));
}

With operator ? you can sign an argument as not-required

int sqr(int x) => x ** 2;
		
sqr(2); // 4
sqr();  // Exception
		
int sqrt(int x?) => x ** 2;
sqr();  // NaN

All main functions in root namespace will be called on start:

// lib.cola
main() {
    console.log('Hello World from lib.cola!');
}

// main.cola
@require "lib.cola";
		
main() {
    console.log('Hello World!');
}