Classes and objects

From Reia

Jump to: navigation, search

"I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning -- it took a while to see how to do messaging in a programming language efficiently enough to be useful)."

Alan Kay, creator of Smalltalk and OOP

Reia takes separation of objects and messages to an extreme. Objects don't share a heap, and for all intents and purposes the only way they can communicate is with messages. These messages are not some metaphorical construct either, they are quite literally messages being sent between objects, which are enqueued for later processing.

Furthermore, like biological cells or individual computers on a network, objects in Reia are concurrent. Whereas in traditional OOP langauges where only one object is processing messages at a given time, in Reia all objects are always waiting for new messages and several can be active at a given time. You might think of an object in Reia as the combination of an object and a thread, although a better analogy is to an operating system process. Objects in Reia don't share state like threads; they exchange state like networked computers: by sending messages to each other.

Contents

Declaring classes

All classes are declared with the class keyword followed by the name of the class, which must begin with a capital letter.

class Foo

Following the class declaration is a list of methods contained within the class, each preceeded by the def keyword and the name of the method, in lower case.

class Foo
  def bar(arg1, arg2, arg3)
    ...
  
  def baz
    ...

Instantiating classes

Classes are instantiated by calling the class name like a function:

f = Foo(1, 2, 3)

The arguments passed are then given to the special "initialize" method of the given class:

>> class Foo
..   def initialize(a, b, c)
..     (@a, @b, @c) = (a, b, c)
..   def a
..     @a
.. 
=> ~ok
>> f = Foo(1, 2, 3)
=> #<Object>
>> f.a()
=> 1

By default, the newly created object will be linked to the current one. If you don't want it to be linked, add a ! to the end of the class name:

f = Foo!(1, 2, 3)

Storing state

Objects store state in instance variables. Instance variables are declared by placing an @ sign in front of an identifier.

@foo

Instance variables work just like other variables and can be used in complex pattern matching expressions.

(foo, @bar) = (baz, 42)

Invoking methods

Methods are covered in the methods section.

Personal tools