Concepts of Scala Chris, Hristo, Pavlos & Niels Agenda Basics - - PowerPoint PPT Presentation

concepts of scala
SMART_READER_LITE
LIVE PREVIEW

Concepts of Scala Chris, Hristo, Pavlos & Niels Agenda Basics - - PowerPoint PPT Presentation

Concepts of Scala Chris, Hristo, Pavlos & Niels Agenda Basics Self types Type Hierarcy Compound types Classes Type bounds Traits Inner classes Higher-order functions Generic classes


slide-1
SLIDE 1

Concepts of Scala

Chris, Hristo, Pavlos & Niels

slide-2
SLIDE 2

Agenda

  • Basics
  • Type Hierarcy
  • Classes
  • Traits
  • Higher-order functions
  • Currying
  • Case Classes
  • Pattern Matching
  • Extractor Objects
  • For Comprehensions
  • Self types
  • Compound types
  • Type bounds
  • Inner classes
  • Generic classes
  • Parameters
  • Annotations
  • Type inference
  • Polymorphic methods
slide-3
SLIDE 3

Main Features

  • Statically typed
  • Object Oriented
  • Functional
  • Extensible

○ polymorphic methods ○ compound types ○ generic classes

slide-4
SLIDE 4

Basics

  • Expressions

○ Values ■ Immutable ○ Variables ■ Mutable

slide-5
SLIDE 5

Basics

  • Blocks

○ combine expressions with { }

  • Functions

○ expressions that take parameters

slide-6
SLIDE 6

Basics

  • Methods

○ Similar behaviour to functions ○ Defined by keyword: def

slide-7
SLIDE 7

Types Hierarchy

slide-8
SLIDE 8

Types Hierarchy

Type casting:

slide-9
SLIDE 9

Types Hierarchy

Type casting:

ScalaFiddle.scala:3: error: type mismatch; found : scala.this.Float required: scala.this.Long val z: Long = y

slide-10
SLIDE 10

Operators

  • Nothing special, just like java
  • Arithmetic Operators

Multiplication *, Divides /, Modulo %, add + , subtract -

  • Relational Operators

Equal ==, Not equal !=, greater/smaller than > < , or >= or <=

  • Logical Operators

Or || , and &&, not !.

  • Assigment Operators

=, +=, -=

slide-11
SLIDE 11

Default Paramers

slide-12
SLIDE 12

Named Parameters

  • parameter with name..
slide-13
SLIDE 13

By-name Parameters

  • ‘By-value’ uses ‘:’ notation

○ (x : int) ○ 1 evaluation for all references in method

  • ‘By-name’ uses ‘: =>’ notation

○ (x : => int) ○ Evaluated individually for each reference

slide-14
SLIDE 14

By-name Parameters - example

slide-15
SLIDE 15

Classes

Members:

  • Methods

○ constructors ○ setters / getters

  • Values
  • Variables
  • Types
  • Objects
  • Traits
  • Classes
slide-16
SLIDE 16

Classes

Constructors:

  • Primary
slide-17
SLIDE 17

Classes

Constructors:

  • Auxiliary
slide-18
SLIDE 18

Traits

➢ Share interfaces and fields between classes ➢ Classes and Objects can extend traits ➢ Traits cannot be instantiated

slide-19
SLIDE 19

Class Composition with Mixins

➢ Traits are used to compose a class

slide-20
SLIDE 20

Higher-Order Functions

  • Functions into functions
  • Functions that return other functions
slide-21
SLIDE 21

Higher-Order Functions

slide-22
SLIDE 22

Currying

slide-23
SLIDE 23

Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments

slide-24
SLIDE 24

Case Classes

slide-25
SLIDE 25

Definition

Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching Keyword case class An identifier (Optional) Parameter list Case classes have an apply method which handles object construction

new

slide-26
SLIDE 26

When a case class is created. The parameters are public vals

slide-27
SLIDE 27

Comparison

By structure, not by reference.

slide-28
SLIDE 28

Copying

(Shallow) copies of an instance of a case class are available via the copy method (Optional) Change constructor arguments

slide-29
SLIDE 29

Pattern matching

slide-30
SLIDE 30

Definition

Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts

slide-31
SLIDE 31

Syntax

A match expression has a value, the match keyword and at least one case clause.

slide-32
SLIDE 32

Matching on case classes

Usefull for pattern matching

slide-33
SLIDE 33
slide-34
SLIDE 34

Pattern Guards

Boolean expressions which are used to make cases more specific

slide-35
SLIDE 35

Sealed classes

Traits and classes can be marked sealed which means all subtypes must be declared in the same file. This assures that all subtypes are known

slide-36
SLIDE 36

Extractor Objects

slide-37
SLIDE 37

Patterns can be defined independently of case classes Method unapply to yield an extractor Returns the argument of the case class Used in pattern matching and partial function

slide-38
SLIDE 38
slide-39
SLIDE 39

Unapply method

Can be used to assign a value Can return different types Boolean in case of test ex. case even() Single sub-value of type T. ex. Option[T] Multiple sub-values. Group in tuple Option[{T1, …, Tn}]

slide-40
SLIDE 40

For Comprehensions

slide-41
SLIDE 41

Scala offers a lightweight notation for expressing sequence comprehensions Comprehensions have the form for (enumerator) yield e, where enumerators refers to a semicolon-separated list of enumerators An enumerator is either a generator which introduces new variables, or it is a filter. A comprehension evaluates the body e for each binding generated by the enumerators and returns a sequence of these values.

slide-42
SLIDE 42
slide-43
SLIDE 43

Generic Classes

Work as they do in other languages such as C# class Stack[A] { private var elements: List[A] = Nil def push(x: A) { elements = x :: elements } def peek: A = elements.head def pop(): A = { val currentTop = peek elements = elements.tail currentTop } }

slide-44
SLIDE 44

Inner Classes

Suppose we want the compiler to prevent us, at compile time, from mixing up which nodes belong to what graph. Path-dependent types (also called Inner classes) provide a solution. They allow us to define inner classes which are restricted to use a particular instance. val graph1: Graph = new Graph val node1: graph1.Node = graph1.newNode val node2: graph1.Node = graph1.newNode node1.connectTo(node2) // legal val graph2: Graph = new Graph val node3: graph2.Node = graph2.newNode node1.connectTo(node3) // illegal because node3 belongs to another graph than node1

slide-45
SLIDE 45

Type Bounds

In Scala, type parameters and abstract types may be constrained by a type bound. 1. Upper type bound T <: A declares that type variable T refers to a subtype of type A. 2. Lower type bounds declare a type to be a supertype of another type.

slide-46
SLIDE 46

Compound Types

Sometimes it is necessary to express that the type of an object is a subtype of several other

  • types. In Scala this can be expressed with the help of compound types, which are intersections of
  • bject types.

Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members. The general form is: A with B with C …

slide-47
SLIDE 47

Example

def cloneAndReset(obj: ?): Cloneable = { val cloned = obj.clone()

  • bj.reset

cloned } def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { //... }

slide-48
SLIDE 48

Self-type

Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn’t directly extend it. That makes the members of the dependency available without imports. A self-type is a way to narrow the type of this or another identifier that aliases this. The syntax looks like normal function syntax but means something entirely different.

slide-49
SLIDE 49

trait User { def username: String } trait Tweeter { this: User => // reassign this def tweet(tweetText: String) = println(s"$username: $tweetText") }

slide-50
SLIDE 50

Implicit Conversions

  • Automatic conversion in scope
  • Type S->T
  • Compiler
  • ‘implicit’ Keyword
  • Usualy named ‘typea2typeb’
slide-51
SLIDE 51

Implicit Conversions

  • Declare in same scope:
  • What the compiler actually does for d:
slide-52
SLIDE 52

Polymorphic Methods

  • Syntax close to generic classes
  • Method with type: A -> [A]
slide-53
SLIDE 53

Local Type Inference

  • Built in local type inference
  • Compiler guesses type
slide-54
SLIDE 54

Local Type Inference

  • Recursive does not work
slide-55
SLIDE 55

Annotations

  • Information for compiler

○ Class, Field or Method..

  • Scala & Java annotations
slide-56
SLIDE 56

Annotations

  • Examples

○ @deprecated ○ @override ○ @volatile ○ @unchecked

slide-57
SLIDE 57

Thank you!

Bedankt!