SlideShare a Scribd company logo
SCALA FUNDAMENTALS
JARGON DICTIONARY
(ADT, TYPECLASSES, EXTENSION METHODS,
CAKE, ETC …. )
// Ruslan Shevchenko
Legend:
— you start to learn scala and found a dozen of new words.
ADT
GADT
typeclasses
existential types
Pattern-matching
Call by {name, value}
Legend:
— words, idioms, …
— let’s made cheatsheet
ADT = { Algebraic Data Types }
ADT = { Abstract Data Types } (not SCALA)
— in context:
Algol 60
Simula
CLU
Algol 68
Scala
LISP
Flavours
ML
ADT = { Algebraic Data Types }
ADT = { Abstract Data Types } (not SCALA)
Pattern Matching
Nominative types, Structured Types
Generic = (Parameter Polymorphism)
Existential types
Type aliases F-Bounded Polymorphism
Traits = (mixins, flavors)
typeclasses = (typeclasses, concepts )
implicit.
extension methods.
ADT = { Abstract Data Types } (not SCALA)
CLU (1970, MIT)
"An abstract data type defines a class of abstract objects which is completely
characterized by the operations available on those objects. This means that an
abstract data type can be defined by defining the characterizing operations for
that type."
// 1974, ACM Sigplan, Congress of very hight-level languages.
From today-s point of view: Interface
cluster stack[t] is create push pop isEmpty
%rep struct {
arr: array[t]
idx: Int
}
create = proc() returns(cvt) ….
push = proc(x:t) signal(overflow)
trait Stack[T]
{
def push(x:T): Unit
…….
}
Barbara Leskov
ADT = { Algebraic Data Type }
HOPE (1970, Edinburg)
From today-s point of view: ADT ;)
data list alpha = nil
++ alpha :: list alpha
(A, B) == Pair[A,B]
(A+B) == emulated by sealed trait
(a:A,b:B)== case classes.
A | B ~~ partially emulated by traits
(will be implemented in dotty)
A & B ~~ partially emulated by A with B
(will be implemented in dotty)
A => B == Function[A,B]
Rod Burstall
David MacQueen
Some initial set of types: A, B, C, ….
Operations on types: Pair [A*B]
records (set of name-value-pairs)
discriminated unions (one of A or B)
functions: A=>B
// often (incorrectly): discr. union == ADT
Equality by value
ADT = { Algebraic Data Type }
data X = A ++ B
data A = ‘A#integer#integer
data B = ‘B#string
(a:A,b:B)== case classes [or objects].
sealed trait X
case class A(x:Int,y:Int) extends X
case class B(s:String) extends X
discriminated unions (one of A or B)
// often (incorrectly): discr. union == ADT
A+B
A+0
B+0
BOOL
BOOL
isA
isB
Pattern Matching
HOPE (1970, Edinburg)
data list alpha = nil
++ alpha::list alpha
sealed trait List[+A]
class Nil extends List[Nothing]
class Cons[A](head:A, tail:List[A]) extends List[A]
Rod Burstall
David MacQueen
dec length: list(alpha) -> int
— length nil <= 0
— length (a::l) <= length(l) + 1
def length[A](l: List[A]): Int =
l match {
case Nil => 0
case Cons(head,tail) => 1+length(tail)
}
Pattern Matching
SCALA
data list alpha = nil
++ alpha::list alpha
sealed trait List[+A]
class Nil extends List[Nothing]
class Cons[A](head:A, tail:List[A]) extends List[A]
dec length: list(alpha) -> int
— length nil <= 0
— length (a::l) <= length(l) + 1
def length[A](l: List[A]): Int =
l match {
case Nil => 0
case Cons(head,tail) => 1+length(tail)
}
Pattern Matching
SCALA
data list alpha = nil
++ alpha::list alpha
sealed trait List[+A]
class Nil extends List[Nothing]
class Cons[A](head:A, tail:List[A]) extends List[A]
dec length: list(alpha) -> int
— length nil <= 0
— length (a::l) <= length(l) + 1
def length[A](l: List[A]): Int =
l match {
case Nil => 0
case head :: tail => 1+length(tail)
}
Pattern Matching
Why Pattern Matching is better than sequence of IF-s ?
- Binding. (i.e. information from structure is extracted into variables)
We have not only algebraic, but object-oriented types.
- Views. (bridge, which represent object as algebraic type). Wadler, 1984
- Pattern objects. (Pattern-object method call return algebraic type)
- ODersky, 2006
- Exhaustive checking (if we miss something then we will see this)
x match {
case A(x,y) => y1
….
}
We have not only algebraic, but object-oriented types.
Pattern-Object
object A {
def unapply(x: X): Option[(A,B)]
}
extractor
Regular expressions:
final val StackElement =
“""W+([^)]+)(([^:]*):([^)]*))W*""".r
line match {
case StackElement(class,file,lineno) => ….
….
}
sealed trait Option[+A]
case class Some[A](a:A) extends Option[A]
case object None extends Option[Nothing]
val fac: Int => Int = {
case 0 => 1
case n => n*fac(n-1)
}
Partial functions syntax:
object Succ
{
def unapply(n: Int):Option[Int] =
if (n==0) None
else Some(n-1)
}
{ case (x,y) => (y,x) }
val positiveOne: Int => Int = {
case Succ(n) => 1
}
positiveOne.isDefinedAt(0)
false
positiveOne.isDefinedAt(3)
true
Partial functions:
val positiveOne: Int => Int = {
case Succ(n) => 1
}
positiveOne.isDefinedAt(0)
false
positiveOne.isDefinedAt(3)
true
PartialFunction[A,B]
B
Boolean
A
apply
isDefinedAt
Types: A <: B
Nominative typing (type == name)
{ def x:Int ; def y: Int } val a = A(1,2)
val b = new B(1,2)
f(a) ==> 3
f(b) ==> 3
Structured typing (type == structure)
case class A(x: Int, y: Int)
class B(x: Int, y: Int)
A != B
- Effective implementation in JVM
- Simula, Clu, C++, Java, …..
~ (A <: B)
~ (B <: A)
def f(p: { def x:Int ; def y: Int }):Int =
p.x + p.y
- implementation in JVM require reflection (can be better)
- ML, OCaml, Go
- theoretically have less corner cases than nominative
Refined type
Generics [Parametric Polymorphism]
B {
def z: Int
}
F[T]
- Structured type, based on nominative.
- Scala: structured types are refinement of AnyRef
Existential types: F[_] F[X] for Some X
Bounded type parameters:
Type aliases
F[T <: Closeable]
F[T <: { def close(): Unit }]
trait Expression[A]
{
type Value = A
}
trait Expression
{
type Value <: X
}
Undefined type alias
Scolem type
//CLU where
Traits:
trait Interpeter {
type Value
}
trait BaseInterpreter[A] extends Additive with Multiplicative with Show
{
type Value = A
}
Flavours (Flavours, [LISP dialects]) 1980, MIT
Mixing
trait Show {
this: Interpreter =>
def show
}
trait Additive {
this: Interpreter =>
def plus(x:Value, y:Value): Value
}
// Howard Cannon, David Moor
(CLOS, OCaml, Groovy, Python ..)
Traits:
trait LoggedInterpreter[A] extends BaseInterpreter[A]
with Logged with LoggedAdditive
trait LoggedAdditive extends Additive {
this => Logged
def plus(x:Value, y: Value) : Value =
{
log(s”(${x}+${y}”)
super.plus(x,y)
}
}
trait Additive {
this: Interpreter =>
def plus(x:Value, y:Value): Value
}
// AOP (aspect oriented programming)
// Flavours
: around
: before-next
: after-next
Type classes.
class Eq a where
== :: a -> a -> Bool
/= :: a -> a -> Bool
class (Eq a) => Ord a
where
compare :: a->a->Int
instance Ord Int where
compare x y = (x-y)
//addition to Haskell, Wadler, 1988
Constructor classes (type classes with multiple type parameters).
//addition to Haskell, Jone, 1993
instance (Eq a) (Eq b) => Eq(Pair a b) where
== (Pair x y) (Pair z w) =
x == z and y == w
classes = rules; instances = adaptors to this rules
Type classes.
trait Eq[A] {
def === (x:A, y:A):Boolean
def !== (x:A, y:A):Boolean = !(x===y)
}
trait Ord[A] extends Eq[A]
{
def compare :: a->a->Int
override
def ===(x:A, y:A):Boolean =
(compare(x,y)==0)
}
implicit val intOrd: Ord[Int] = new Ord[Int]
{
def compare(x:Int, y:Int) = (x-y)
}
//in scala as design pattern (implicit) & one word
implicit def pairEq[A,B]: Eq[Pair[A,B]]
( implicit eqA:Eq[A], eqB:Eq[B]) =
{
def ===(x: Pair[A,B],y:Pair[A,B]):Boolean=
eqA(x._1,y._1) && eqB(x._1, y._1)

}
implicit (val, def, classes )
- define rules of your world
- can be usable via implicit parameters
- implicit search <=> logical deduction
- can be dangerous.
implicit def stringToInt(s:String):Int = s.toInt
def f(x:Int):Int = x+1
f(“45”)
implicit (val, def, classes )
- define rules of your world
- can be usable via implicit parameters
- implicit search <=> logical deduction
- can be dangerous.
implicit def toJson(x:Int): Json = JsonNumeric(10)
def printAsJson[A](x:A)(implicit convert:A=>Json): String =
convert(x).prettyPrint
Type classes.
//Libraries: universal rules (like ontologies in philosophy)
scalaz : https://github.com/scalaz/scalaz
spire/algebra: https://github.com/non/algebra
(now - part of cats)
//Potenial problem: premature abstraction
// Human is an upright, featherless, ripped with broad, flat nails
+
Z / 10^64 Z << BigInt
Z{ |x| < 10^32} <<ERROR
Extension methods.
//implicit-based technique (pimp my library pattern [obsolete name])
implicit class WithPow(x: Int) {
def pow(y: Int): Int = Math.pow(x,y).toInt
}
scala> 2 pow 3
scala> res1: Int = 8
• pow — Int have no pow method
• => compiler search for implicit with pow
Scala types.
//whats-left for advanced talks:
42.type
Dynamic
// Homework:
Path dependency issues.
What’s left out of scala type system (?)
Call by ……….
Value.
Reference:
Name
Need
// value is copied.
// reference is copied by value.
// reference in language must exists
// Algol 68 (today - call by closure)
// lazy one-time evaluation
Call by ……….
Name // Algol 68 (today - call by closure)
def doWhile(x: => Boolean)(f : =>Unit)
{ while(x) { f } }
doWhile(x < 10)(x = x + 1)
Call by ……….
Need
def dupN[A](n: Int, v : =>A): Seq[A] =
{ lazy val neededV = v
for( i <- 1 to N) yield v
}
Scala.
Scala / Dotty : like grand unification theory
for large subset of type theories.
Mathematical jargon hide quite simple patterns.
Questions ?
// ruslan@shevchenko.kiev.ua
Scala jargon cheatsheet

More Related Content

PDF
Few simple-type-tricks in scala
PDF
Scala cheatsheet
PDF
SE 20016 - programming languages landscape.
PPTX
Scala Back to Basics: Type Classes
PDF
High Wizardry in the Land of Scala
PDF
Demystifying Shapeless
PPTX
Scala fundamentals
PDF
First-Class Patterns
Few simple-type-tricks in scala
Scala cheatsheet
SE 20016 - programming languages landscape.
Scala Back to Basics: Type Classes
High Wizardry in the Land of Scala
Demystifying Shapeless
Scala fundamentals
First-Class Patterns

What's hot (20)

PPTX
PDF
Programming in Scala: Notes
PDF
Scala categorytheory
PDF
Scala collections api expressivity and brevity upgrade from java
ODP
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
PPTX
Scala for curious
PPTX
Intro to Functional Programming in Scala
ODP
A Tour Of Scala
PPT
Scala - brief intro
PDF
Property based Testing - generative data & executable domain rules
PDF
Workshop Scala
PDF
Scala 2013 review
PDF
PDF
Starting with Scala : Frontier Developer's Meetup December 2010
PDF
Miles Sabin Introduction To Scala For Java Developers
PDF
Ponies and Unicorns With Scala
PPTX
Joy of scala
PPTX
Practically Functional
PDF
Stepping Up : A Brief Intro to Scala
PDF
Scala Intro
Programming in Scala: Notes
Scala categorytheory
Scala collections api expressivity and brevity upgrade from java
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala for curious
Intro to Functional Programming in Scala
A Tour Of Scala
Scala - brief intro
Property based Testing - generative data & executable domain rules
Workshop Scala
Scala 2013 review
Starting with Scala : Frontier Developer's Meetup December 2010
Miles Sabin Introduction To Scala For Java Developers
Ponies and Unicorns With Scala
Joy of scala
Practically Functional
Stepping Up : A Brief Intro to Scala
Scala Intro
Ad

Similar to Scala jargon cheatsheet (20)

PDF
Power of functions in a typed world
PDF
Type classes 101 - classification beyond inheritance
ODP
Introducing scala
PDF
Scala Bootcamp 1
PDF
A bit about Scala
PDF
The Scala Programming Language
PDF
(How) can we benefit from adopting scala?
PPTX
PDF
Comparing Haskell & Scala
PDF
Sequence and Traverse - Part 3
PDF
Scala: Functioneel programmeren in een object georiënteerde wereld
PDF
Monads and Monoids by Oleksiy Dyagilev
PDF
Meet scala
PDF
Monoids - Part 1 - with examples using Scalaz and Cats
PDF
Scala or functional programming from a python developer's perspective
PDF
Oh, All the things you'll traverse
PPT
Scala introduction
PDF
PPT
An introduction to scala
PPTX
Typeclasses
Power of functions in a typed world
Type classes 101 - classification beyond inheritance
Introducing scala
Scala Bootcamp 1
A bit about Scala
The Scala Programming Language
(How) can we benefit from adopting scala?
Comparing Haskell & Scala
Sequence and Traverse - Part 3
Scala: Functioneel programmeren in een object georiënteerde wereld
Monads and Monoids by Oleksiy Dyagilev
Meet scala
Monoids - Part 1 - with examples using Scalaz and Cats
Scala or functional programming from a python developer's perspective
Oh, All the things you'll traverse
Scala introduction
An introduction to scala
Typeclasses
Ad

More from Ruslan Shevchenko (20)

PDF
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
PDF
Svitla talks 2021_03_25
PDF
Akka / Lts behavior
PDF
Papers We Love / Kyiv : PAXOS (and little about other consensuses )
PDF
Scala / Technology evolution
PDF
{co/contr} variance from LSP
PDF
N flavors of streaming
PDF
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
PDF
Why scala is not my ideal language and what I can do with this
PDF
Java & low latency applications
PDF
Csp scala wixmeetup2016
PDF
R ext world/ useR! Kiev
PDF
Jslab rssh: JS as language platform
PDF
Behind OOD: domain modelling in post-OO world.
PDF
scala-gopher: async implementation of CSP for scala
PDF
Programming Languages: some news for the last N years
PDF
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
PDF
Ruslan.shevchenko: most functional-day-kiev 2014
PDF
Web architecture - overview of techniques.
Embedding Generic Monadic Transformer into Scala. [Tfp2022]
Svitla talks 2021_03_25
Akka / Lts behavior
Papers We Love / Kyiv : PAXOS (and little about other consensuses )
Scala / Technology evolution
{co/contr} variance from LSP
N flavors of streaming
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Why scala is not my ideal language and what I can do with this
Java & low latency applications
Csp scala wixmeetup2016
R ext world/ useR! Kiev
Jslab rssh: JS as language platform
Behind OOD: domain modelling in post-OO world.
scala-gopher: async implementation of CSP for scala
Programming Languages: some news for the last N years
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Ruslan.shevchenko: most functional-day-kiev 2014
Web architecture - overview of techniques.

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PPTX
Introduction to Artificial Intelligence
PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
medical staffing services at VALiNTRY
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
VVF-Customer-Presentation2025-Ver1.9.pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Design an Analysis of Algorithms II-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
Introduction to Artificial Intelligence
top salesforce developer skills in 2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
medical staffing services at VALiNTRY
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Choose the Right IT Partner for Your Business in Malaysia
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Odoo Companies in India – Driving Business Transformation.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf

Scala jargon cheatsheet

  • 1. SCALA FUNDAMENTALS JARGON DICTIONARY (ADT, TYPECLASSES, EXTENSION METHODS, CAKE, ETC …. ) // Ruslan Shevchenko
  • 2. Legend: — you start to learn scala and found a dozen of new words. ADT GADT typeclasses existential types Pattern-matching Call by {name, value}
  • 3. Legend: — words, idioms, … — let’s made cheatsheet ADT = { Algebraic Data Types } ADT = { Abstract Data Types } (not SCALA) — in context: Algol 60 Simula CLU Algol 68 Scala LISP Flavours ML
  • 4. ADT = { Algebraic Data Types } ADT = { Abstract Data Types } (not SCALA) Pattern Matching Nominative types, Structured Types Generic = (Parameter Polymorphism) Existential types Type aliases F-Bounded Polymorphism Traits = (mixins, flavors) typeclasses = (typeclasses, concepts ) implicit. extension methods.
  • 5. ADT = { Abstract Data Types } (not SCALA) CLU (1970, MIT) "An abstract data type defines a class of abstract objects which is completely characterized by the operations available on those objects. This means that an abstract data type can be defined by defining the characterizing operations for that type." // 1974, ACM Sigplan, Congress of very hight-level languages. From today-s point of view: Interface cluster stack[t] is create push pop isEmpty %rep struct { arr: array[t] idx: Int } create = proc() returns(cvt) …. push = proc(x:t) signal(overflow) trait Stack[T] { def push(x:T): Unit ……. } Barbara Leskov
  • 6. ADT = { Algebraic Data Type } HOPE (1970, Edinburg) From today-s point of view: ADT ;) data list alpha = nil ++ alpha :: list alpha (A, B) == Pair[A,B] (A+B) == emulated by sealed trait (a:A,b:B)== case classes. A | B ~~ partially emulated by traits (will be implemented in dotty) A & B ~~ partially emulated by A with B (will be implemented in dotty) A => B == Function[A,B] Rod Burstall David MacQueen Some initial set of types: A, B, C, …. Operations on types: Pair [A*B] records (set of name-value-pairs) discriminated unions (one of A or B) functions: A=>B // often (incorrectly): discr. union == ADT Equality by value
  • 7. ADT = { Algebraic Data Type } data X = A ++ B data A = ‘A#integer#integer data B = ‘B#string (a:A,b:B)== case classes [or objects]. sealed trait X case class A(x:Int,y:Int) extends X case class B(s:String) extends X discriminated unions (one of A or B) // often (incorrectly): discr. union == ADT A+B A+0 B+0 BOOL BOOL isA isB
  • 8. Pattern Matching HOPE (1970, Edinburg) data list alpha = nil ++ alpha::list alpha sealed trait List[+A] class Nil extends List[Nothing] class Cons[A](head:A, tail:List[A]) extends List[A] Rod Burstall David MacQueen dec length: list(alpha) -> int — length nil <= 0 — length (a::l) <= length(l) + 1 def length[A](l: List[A]): Int = l match { case Nil => 0 case Cons(head,tail) => 1+length(tail) }
  • 9. Pattern Matching SCALA data list alpha = nil ++ alpha::list alpha sealed trait List[+A] class Nil extends List[Nothing] class Cons[A](head:A, tail:List[A]) extends List[A] dec length: list(alpha) -> int — length nil <= 0 — length (a::l) <= length(l) + 1 def length[A](l: List[A]): Int = l match { case Nil => 0 case Cons(head,tail) => 1+length(tail) }
  • 10. Pattern Matching SCALA data list alpha = nil ++ alpha::list alpha sealed trait List[+A] class Nil extends List[Nothing] class Cons[A](head:A, tail:List[A]) extends List[A] dec length: list(alpha) -> int — length nil <= 0 — length (a::l) <= length(l) + 1 def length[A](l: List[A]): Int = l match { case Nil => 0 case head :: tail => 1+length(tail) }
  • 11. Pattern Matching Why Pattern Matching is better than sequence of IF-s ? - Binding. (i.e. information from structure is extracted into variables) We have not only algebraic, but object-oriented types. - Views. (bridge, which represent object as algebraic type). Wadler, 1984 - Pattern objects. (Pattern-object method call return algebraic type) - ODersky, 2006 - Exhaustive checking (if we miss something then we will see this)
  • 12. x match { case A(x,y) => y1 …. } We have not only algebraic, but object-oriented types. Pattern-Object object A { def unapply(x: X): Option[(A,B)] } extractor Regular expressions: final val StackElement = “""W+([^)]+)(([^:]*):([^)]*))W*""".r line match { case StackElement(class,file,lineno) => …. …. } sealed trait Option[+A] case class Some[A](a:A) extends Option[A] case object None extends Option[Nothing]
  • 13. val fac: Int => Int = { case 0 => 1 case n => n*fac(n-1) } Partial functions syntax: object Succ { def unapply(n: Int):Option[Int] = if (n==0) None else Some(n-1) } { case (x,y) => (y,x) } val positiveOne: Int => Int = { case Succ(n) => 1 } positiveOne.isDefinedAt(0) false positiveOne.isDefinedAt(3) true
  • 14. Partial functions: val positiveOne: Int => Int = { case Succ(n) => 1 } positiveOne.isDefinedAt(0) false positiveOne.isDefinedAt(3) true PartialFunction[A,B] B Boolean A apply isDefinedAt
  • 16. Nominative typing (type == name) { def x:Int ; def y: Int } val a = A(1,2) val b = new B(1,2) f(a) ==> 3 f(b) ==> 3 Structured typing (type == structure) case class A(x: Int, y: Int) class B(x: Int, y: Int) A != B - Effective implementation in JVM - Simula, Clu, C++, Java, ….. ~ (A <: B) ~ (B <: A) def f(p: { def x:Int ; def y: Int }):Int = p.x + p.y - implementation in JVM require reflection (can be better) - ML, OCaml, Go - theoretically have less corner cases than nominative
  • 17. Refined type Generics [Parametric Polymorphism] B { def z: Int } F[T] - Structured type, based on nominative. - Scala: structured types are refinement of AnyRef Existential types: F[_] F[X] for Some X Bounded type parameters: Type aliases F[T <: Closeable] F[T <: { def close(): Unit }] trait Expression[A] { type Value = A } trait Expression { type Value <: X } Undefined type alias Scolem type //CLU where
  • 18. Traits: trait Interpeter { type Value } trait BaseInterpreter[A] extends Additive with Multiplicative with Show { type Value = A } Flavours (Flavours, [LISP dialects]) 1980, MIT Mixing trait Show { this: Interpreter => def show } trait Additive { this: Interpreter => def plus(x:Value, y:Value): Value } // Howard Cannon, David Moor (CLOS, OCaml, Groovy, Python ..)
  • 19. Traits: trait LoggedInterpreter[A] extends BaseInterpreter[A] with Logged with LoggedAdditive trait LoggedAdditive extends Additive { this => Logged def plus(x:Value, y: Value) : Value = { log(s”(${x}+${y}”) super.plus(x,y) } } trait Additive { this: Interpreter => def plus(x:Value, y:Value): Value } // AOP (aspect oriented programming) // Flavours : around : before-next : after-next
  • 20. Type classes. class Eq a where == :: a -> a -> Bool /= :: a -> a -> Bool class (Eq a) => Ord a where compare :: a->a->Int instance Ord Int where compare x y = (x-y) //addition to Haskell, Wadler, 1988 Constructor classes (type classes with multiple type parameters). //addition to Haskell, Jone, 1993 instance (Eq a) (Eq b) => Eq(Pair a b) where == (Pair x y) (Pair z w) = x == z and y == w classes = rules; instances = adaptors to this rules
  • 21. Type classes. trait Eq[A] { def === (x:A, y:A):Boolean def !== (x:A, y:A):Boolean = !(x===y) } trait Ord[A] extends Eq[A] { def compare :: a->a->Int override def ===(x:A, y:A):Boolean = (compare(x,y)==0) } implicit val intOrd: Ord[Int] = new Ord[Int] { def compare(x:Int, y:Int) = (x-y) } //in scala as design pattern (implicit) & one word implicit def pairEq[A,B]: Eq[Pair[A,B]] ( implicit eqA:Eq[A], eqB:Eq[B]) = { def ===(x: Pair[A,B],y:Pair[A,B]):Boolean= eqA(x._1,y._1) && eqB(x._1, y._1) 
}
  • 22. implicit (val, def, classes ) - define rules of your world - can be usable via implicit parameters - implicit search <=> logical deduction - can be dangerous. implicit def stringToInt(s:String):Int = s.toInt def f(x:Int):Int = x+1 f(“45”)
  • 23. implicit (val, def, classes ) - define rules of your world - can be usable via implicit parameters - implicit search <=> logical deduction - can be dangerous. implicit def toJson(x:Int): Json = JsonNumeric(10) def printAsJson[A](x:A)(implicit convert:A=>Json): String = convert(x).prettyPrint
  • 24. Type classes. //Libraries: universal rules (like ontologies in philosophy) scalaz : https://github.com/scalaz/scalaz spire/algebra: https://github.com/non/algebra (now - part of cats) //Potenial problem: premature abstraction // Human is an upright, featherless, ripped with broad, flat nails + Z / 10^64 Z << BigInt Z{ |x| < 10^32} <<ERROR
  • 25. Extension methods. //implicit-based technique (pimp my library pattern [obsolete name]) implicit class WithPow(x: Int) { def pow(y: Int): Int = Math.pow(x,y).toInt } scala> 2 pow 3 scala> res1: Int = 8 • pow — Int have no pow method • => compiler search for implicit with pow
  • 26. Scala types. //whats-left for advanced talks: 42.type Dynamic // Homework: Path dependency issues. What’s left out of scala type system (?)
  • 27. Call by ………. Value. Reference: Name Need // value is copied. // reference is copied by value. // reference in language must exists // Algol 68 (today - call by closure) // lazy one-time evaluation
  • 28. Call by ………. Name // Algol 68 (today - call by closure) def doWhile(x: => Boolean)(f : =>Unit) { while(x) { f } } doWhile(x < 10)(x = x + 1)
  • 29. Call by ………. Need def dupN[A](n: Int, v : =>A): Seq[A] = { lazy val neededV = v for( i <- 1 to N) yield v }
  • 30. Scala. Scala / Dotty : like grand unification theory for large subset of type theories. Mathematical jargon hide quite simple patterns. Questions ? // [email protected]