This project is read-only.

Methods

Methods are named pieces of code that can be executed. They can receive parameters and possibly return a value. Methods are always bound to a specific type.

The very basic method can be defined as follows:

type testy
{
  void greet
  {
    print "hello world"
  }
} 

And the method is invoked like this:

var obj = new testy
obj.greet // "hello world" 

You don't need any parens if your method accepts no parameters.

Parameters

Method might receive a number of parameters, which will be visible inside it's body just like local variables. You need to specify their types and names, separated by commas, after the method name in brackets:

type testy
{
  void greet(string who, int count)
  {
    for idx in 1..count do
      println "hello " + who
  }
} 

When calling a method, parameters may or may not be put into parens:

var obj = new testy
obj.greet "world", 2
obj.greet("world", 2) 

Parens actually matter when you write complex calls, like a method call chain or nested calls:

math:sin(1).to_s // math:sin 1 .to_s would work as math:sin(1.to_s) which is obviously wrong 

Return value

Methods might return a value. In this case, they must be marked with the return type and then the return keyword makes the magic come true.
type zomg
{
  string greet(string who, int count)
  {
    var str = ("hello " + who + " ") * count
    return str
  }
}

var result = new zomg.greet "everyone", 3
println result 

Methods can only return one value. If you need more than one, there are two options:
  • If the values are of the same type, you can pack them into an array. You can also benefit from Variable Splatting.
  • If values are all different (e.g. int, string and bool), define a new type with needed fields and return an instance of it

For methods marked as void, return statement cannot accept an expression. It will be used to prematurely stop the method execution instead:
type test
{
  static void generate(int count)
  {
    for i in 1..count do
    {
      println i
      if i > 10 do return
    }
  }
} 

Method scope

Each method has it's own lexical scope, so all methods might have equally named variables which will not overlap. Global variables, being a bad programming practice, are not implemented in Mirelle. However, if you need to access a variable across many methods of different types, you can define a new type and create a static field in it, which will work similarly.

Special methods

There are three kinds of special methods in Mirelle, which have special meaning to the language and have restrictions on return value types.

"This" Pointer

All methods (except the static ones) can access a special variable named with a single atmark sign (@). It is the pointer to current object, on which the method is invoked, similar to "this" in C++, C#, Java or "self" in Ruby and Python.

Usually it's not needed, because fields and methods can be accessed locally, omitting the @ pointer. However, it's quite useful when you're building a branchy structure (a tree or a graph), or making a type supporting chainable calls, like this:

type chain
{
  chain greet
  {
    print "hello "
    return @
  }

  chain person(string whom)
  {
    print whom
    return @
  }

  chain really
  {
    print "!"
    return @
  }
}

var obj = new chain
obj.greet.person("world").really 

Last edited May 13, 2011 at 3:48 PM by impworks, version 9

Comments

No comments yet.