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

Functions

dangreen edited this page Jan 2, 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 var)
  • export to export function or property from module
  • async to async using with Promises (read more)

From Dart we have borrowed arrow-functions:

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

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();                                             // Hello World!

// 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!');
}
Clone this wiki locally