Traits inscala

15
TRAITS IN SCALA JANMEJANI SOFTWARE CONSULTANT KNOLDUS SOFTWARE LLP

description

The basic introduction of traits in scala.

Transcript of Traits inscala

Page 1: Traits inscala

TRAITS IN SCALA

JANMEJANISOFTWARE CONSULTANT

KNOLDUS SOFTWARE LLP

Page 2: Traits inscala

INTRODUCTION

➢ Traits in Scala are similar to interfaces, but much more powerful.

➢ A trait encapsulates method and field definitions, which can then be reused by mixing them into classes.

➢ Unlike Java, Scala allows traits to be partially implemented; i.e. it is possible to define default implementations for some methods.

➢ Once a trait is defined, it can be mixed in to a class using either the “extends” or “with” keywords.

Page 3: Traits inscala

SIMPLE EXAMPLE USING EXTEND

trait Example {

def Test() {

println("Hello World!")

}

}

class Frog extends Example {

}

val frog = new Frog

frog.Test()

Page 4: Traits inscala

➢ If you wish to mix a trait into a class that explicitly extends a superclass, you use “extends” to indicate the superclass and “with” to mix in the trait.

class Animal

class Frog extends Animal with Example {

override def toString = "green"

}

class Animal

trait HasLegs

class Frog extends Animal with Example with HasLegs {

override def toString = "green"

}

Traits

Page 5: Traits inscala

Thin versus rich interfaces

➢ Rich has many methods (easier in theory for client)Clients can pick a method that exactly matches the functionality the

need.

➢ Thin has fewer – easier for implementerClients calling into a thin interface, however, have to write more code.

Traits can be used to create Rich Interfaces which are Thin, means traits can have lot of methods- Many of them are implemented in terms of the few unimplemented methods. So the class which mixes these traits provides the implementation for the few unimplemented methods.

Page 6: Traits inscala

For Example: class Point(val x: Int, val y: Int)

class Rectangle(val topLeft: Point, val bottomRight: Point) extends Rectangular { }trait Rectangular { def topLeft: Point def bottomRight: Point def left = topLeft.x def right = bottomRight.x def width = right - left}

Page 7: Traits inscala

Cont....

val rect = new Rectangle(new Point(1, 1),new Point(10,10)) println(rect.width)

Output: 9

Page 8: Traits inscala

THE ORDERED TRAIT➢ The Ordered trait in Scala is typically used when defining a class

of objects that know how to order themselves by comparing against other instances of that class.

For Example:

case class Rational(n: Int, d: Int) extends Ordered[Rational] {def compare(that: Rational) =(this.n * that.d) - (that.n * this.d)

}

It should return zero if the objects are the same, negative if receiver is less than the argument, and positive if the receiver is greater than the argument.

Page 9: Traits inscala

TRAITS WITH STACKABLE MODIFICATIONS

Stackable traits in Scala refers to being able to mix in multiple traits that work together to apply multiple modifications to a method.

How It Works:

In this pattern, a trait (or class) can play one of three roles:

➢ The base: defines an abstract interface

➢ A core or a stackable: implement the abstract methods and provides functionality

Page 10: Traits inscala

For Example:

abstract class IntQueue {

def get(): Int

def put(x: Int)

}

Now we’ll build a concrete class

import scala.collection.mutable.ArrayBuffer

class BasicIntQueue extends IntQueue { private val buf = new ArrayBuffer[Int] def get() = buf.remove(0) def put(x: Int) { buf += x } }

Page 11: Traits inscala

TRAIT USEFUL FOR QUEUE

trait Doubling extends IntQueue { abstract override def put(x: Int) { super.put(2 * x) } }

trait Incrementing extends IntQueue {     abstract override def put(x: Int) { super.put(x + 1) }  }

 trait Filtering extends IntQueue {    abstract override def put(x: Int) {       if (x >= 0) super.put(x)     }  }

Page 12: Traits inscala

Cont....

For more example go to: http://blog.knoldus.com/2012/11/27/scalaknol-understanding-traits-as-stackable-modifications/

Page 13: Traits inscala

TO TRAIT, OR NOT TO TRAIT?➢ If the behavior will not be reused, then make it a concrete

class. It is not reusable behavior after all.

➢ If it might be reused in multiple, unrelated classes, make it a trait. Only traits can be mixed into different parts of the class hierarchy.

➢ If you want to inherit from it in Java code, use an abstract class. Because a Scala trait with only abstract members translates directly to a

Java interface.

Page 14: Traits inscala

References

➢ Programming in Scala, Martin Odersky➢ A tour of Scala : Traits (see

http://www.scala-lang.org/node/126)➢

www.artima.com/scalazine/articles/stackable_trait_pattern.html

Page 15: Traits inscala