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

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Visual Basic .NET for Microsoft Platforms only.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

 

cover Dark Basic Professional Edition - It is better to get this professional edition

cover This is a version of basic designed for building games, for example to rotate a cube you might do the following:
make object cube 1,100
for x=1 to 360
rotate object 1,x,x,0
next x

cover Game Programming with Darkbasic - book for above software

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

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