Scala Objects

This page dicusses the Scala language and its object structure.

object heirarchy

In Scala everything is an object, including numbers, so an expression like 1+2 is evaluated by a method call on the number '1' as follows:

1.+(2)

Scala classes are either:

Functions

Functions are also objects, we can pass a function (instanciated method) to another method as shown here:

def eventReporter( callback: () =>unit) {
   ...
   callback()
   ...
}

where we pass the function to eventReporter which then calls the function.

The parameter 'callback: () =>unit' says pass a function with no parameters which returns the type 'unit'.

Objects

Objects have 2 special methods:

For example in this:

value match {
 case myObject(param1,param2) => println("this is called ") 
}

if value matches myObject(param1,param2) then myObject.unapply(value:myObject) gets called.

Case Classes

When case classes are created like:

abstract class SExpression
case class SString(sstring:String) extends SExpression
case class SSymbol(ssymbol:String) extends SExpression
case class SInteger(sint:Integer) extends SExpression
case class SFloat(sfloat:Double) extends SExpression
case class SList(sop:String,slist:List [SExpression]) extends SExpression

then a matching static object is created for each case class is created, this has apply and unapply methods, which means:

In addition:


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.