Scala Cheat Sheet V.0.1

ADVERTISEMENT

=Scala=
VARIABLE
MIXIN CLASS COMPOSITION
var var_name: type = init_value;
Mixin:
trait RichIterator extends AbsIterator {
eg. var i : int = 0;
def foreach(f: T => Unit) {
default values:
while (hasNext) f(next)
private var myvar: T = _ // "_" is a default
CHEAT SHEET
}
value
v.0.1
}
scala.Unit is similar to void in Java, except
Mixin Class Composition:
“Every value is an object & every operation is a message send.”
Unit can be assigned the () value.
The first parent is called the superclass of Iter,
unnamed2: Unit = ()
PACKAGE
whereas the second (and every other, if present)
default values:
Java style:
parent is called a mixin.
0 for numeric types
package com.mycompany.mypkg
object StringIteratorTest {
false for the Boolean type
applies across the entire file scope
def main(args: Array[String]) {
() for the Unit type
class Iter extends StringIterator(args(0))
Package "scoping" approach: curly brace delimited
null for all object types
with
RichIterator
package com
val iter = new Iter
{
iter foreach println
package mycompany
CONSTANT
}
{
Prefer val over var.
}
package scala
note the keyword "with" used to create a mixin
form: val var_name: type = init_value;
{
package demo
composition of the parents StringIterator and
val i : int = 0;
{
RichIterator.
object HelloWorld
STATIC
{
No static members, use Singleton, see Object
TRAITS
import java.math.BigInteger
// just to show nested importing
Like Java interfaces, defines object types by
def main(args : Array[String]) :
CLASS
specifying method signatures, can be partially
Unit =
Every class inherits from scala.Any
implemented. See example in Mixin.
{ Console.println("Hello there!")
2 subclass categories:
}
}
scala.AnyVal (maps to java.lang.Object)
GENERIC CLASS
}
class Stack[T] {
scala.AnyRef
}
// members here
form: abstract class(pName: PType1,
}
}
}
pName2: PType2...) extends SuperClass
Usage:
with optional constructor in the class definition:
object GenericsTest extends Application {
IMPORT
val stack = new Stack[Int]
class Person(name: String, age: int) extends
import p._
// imports all members of p
// do stuff here
Mammal {
// (this is analogous to import p.* in Java)
}
// secondary constructor
def this(name: String) {
note: can also define generic methods
import p.x
// the member x of p
// calls to the "primary" constructor
import p.{x => a} // the member x of p renamed
this(name, 1);
// as a
INNER CLASS
}
import p.{x, y}
// the members x and y of p
example:
// members here
import p1.p2.z
// the member z of p2,
}
class Graph {
// itself member of p1
class Node {
predefined function classOf[T] returns Scala
import p1._, p2._ // is a shorthand for import
var connectedNodes: List[Node] = Nil
class type T
// p1._; import p2._
def connectTo(node: Node) {
implicit imports:
if
the package java.lang
OBJECT
(connectedNodes.find(node.equals).isEmpty) {
connectedNodes = node :: connectedNodes
the package scala
A concrete class instance and is a singleton.
}
object RunRational extends Application
and the object scala.Predef
}
{
Import anywhere inside the client Scala file, not just
}
// members here
// members here
at the top of the file, for scoped relevance, see
}
}
example in Package section.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2