Scala Parameters and Variables

This page discusses the Scala language and its parameters and variables.

Functional programming can be more efficient if we work with non-changable (immutable) values. When we declare objects we therefore distinguish between:

Whenever possible it is best to use 'val' which may give the compiler more possibilities for optimisation.

The form of the declaration is:

var name: type=value;

where:

This compares with java as follows:

  Java Scala
Variable declaration int i=0; var i: int=0;
Constant declaration final int i=0; val i: int=0;

Some differences between value (val) and variable (var)

  val var
value The value must be constant values The value can include symbolic types
value evaluated when declared when needed

Co-Data Types

algebraic data type:

datatype BinTree = Empty | Node(Int, BinTree, BinTree)

we could use case classes:

abstact class BinTree

case class EmptyTree() extends BinTree

case class Node(elem: Int, left : BinTree , right : BinTree) extends Bintree

or Either

Either[Int, String]


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.