Download pdf - Scala 2.10

Transcript
Page 1: Scala 2.10

Scala 2.10

Czech Scala Enthusiasts May 28th 2013

Jakub Janeček

Page 2: Scala 2.10

What does it bring?

Page 3: Scala 2.10

Language Modularization

Page 4: Scala 2.10

Language Modularization

•  some features are confusing, “dangerous” or still experimental

Page 5: Scala 2.10

Language Modularization

•  some features are confusing, “dangerous” or still experimental

•  since 2.10 they have to be explicitly enabled (otherwise warning or error is emitted)

Page 6: Scala 2.10

Language Modularization

•  some features are confusing, “dangerous” or still experimental

•  since 2.10 they have to be explicitly enabled (otherwise warning or error is emitted)

•  for example: –  implicit conversions

Page 7: Scala 2.10

Language Modularization

•  some features are confusing, “dangerous” or still experimental

•  since 2.10 they have to be explicitly enabled (otherwise warning or error is emitted)

•  for example: –  implicit conversions – macros

Page 8: Scala 2.10

Example: Postfix Operators

List(1, 2, 3) reverse

Page 9: Scala 2.10

Example: Postfix Operators

List(1, 2, 3) reverse

import language.postfixOps

Page 10: Scala 2.10

Example: Postfix Operators

List(1, 2, 3) reverse

> scalac –language:postfixOps

Page 11: Scala 2.10

What does it bring?

Page 12: Scala 2.10

String Interpolation

Page 13: Scala 2.10

String Interpolation

•  Remember?

"1 + 1 = " + (1 + 1)

Page 14: Scala 2.10

String Interpolation

•  Remember?

"1 + 1 = " + (1 + 1)

•  No more!

s"1 + 1 = ${1 + 1}"

Page 15: Scala 2.10

String Interpolation

"1 + 1 = " + (1 + 1)

•  No more!

s"1 + 1 = ${1 + 1}"

Standard interpolator We also have f and raw

Escape character

•  Remember?

Page 16: Scala 2.10

Example: Interpolator rev

def rev(args: Any*): String = … •  Extension method of StringContext:

Page 17: Scala 2.10

Example: Interpolator rev

def rev(args: Any*): String = …

rev”this will be reversed"

•  Extension method of StringContext:

Page 18: Scala 2.10

What does it bring?

Page 19: Scala 2.10

“value”

Value Classes

Page 20: Scala 2.10

Value Classes

•  allow extends AnyVal

Page 21: Scala 2.10

Value Classes

•  allow extends AnyVal •  compiler can usually avoid allocating runtime

objects à performance with type safety

Page 22: Scala 2.10

Value Classes

•  allow extends AnyVal •  compiler can usually avoid allocating runtime

objects à performance with type safety •  they have limitations

Page 23: Scala 2.10

Example: MyInt

case class MyInt(val underlying: Int) extends AnyVal { def plusOne = MyInt(underlying + 1) } MyInt(5).plusOne

Page 24: Scala 2.10

Example: MyInt

case class MyInt(val underlying: Int) extends AnyVal { def plusOne = MyInt(underlying + 1) } MyInt(5).plusOne

Page 25: Scala 2.10

Example: MyInt

case class MyInt(val underlying: Int) extends AnyVal { def plusOne = MyInt(underlying + 1) } MyInt(5).plusOne

MyInt$.MODULE$.plusOne$extension(5)

Page 26: Scala 2.10

What does it bring?

Page 27: Scala 2.10

Cute Puppy

Page 28: Scala 2.10

Cute Puppy Implicit Classes

Page 29: Scala 2.10

Implicit Classes

•  remove a lot of boilerplate code

Page 30: Scala 2.10

Implicit Classes

•  remove a lot of boilerplate code •  class must have only one parameter in its

constructor's first parameter list

Page 31: Scala 2.10

Implicit Classes

•  remove a lot of boilerplate code •  class must have only one parameter in its

constructor's first parameter list •  implicit conversion is generated

Page 32: Scala 2.10

Implicit Classes

•  remove a lot of boilerplate code •  class must have only one parameter in its

constructor's first parameter list •  implicit conversion is generated •  often used with Value Classes

Page 33: Scala 2.10

Implicit Classes

•  remove a lot of boilerplate code •  class must have only one parameter in its

constructor's first parameter list •  implicit conversion is generated •  often used with Value Classes •  must be defined inside other class/trait/object

Page 34: Scala 2.10

Example: MyInt

implicit class MyInt(val i: Int) extends AnyVal { def plusOne = i + 1 } 2.plusOne

Page 35: Scala 2.10

Example: MyInt

implicit class MyInt(val i: Int) extends AnyVal { def plusOne = i + 1 } 2.plusOne

Page 36: Scala 2.10

What does it bring?

Page 37: Scala 2.10

Trait Dynamic

Page 38: Scala 2.10

Trait Dynamic

•  allows calling methods not existing in the static type

Page 39: Scala 2.10

Trait Dynamic

•  allows calling methods not existing in the static type

•  useful for DSLs and integration with dynamic languages

Page 40: Scala 2.10

Trait Dynamic

•  allows calling methods not existing in the static type

•  useful for DSLs and integration with dynamic languages

•  empty marker trait Dynamic

Page 41: Scala 2.10

Trait Dynamic

•  allows calling methods not existing in the static type

•  useful for DSLs and integration with dynamic languages

•  empty marker trait Dynamic •  if type check fails it is rewritten to one of

–  applyDynamic –  applyDynamicNamed –  selectDynamic –  updateDynamic

Page 42: Scala 2.10

Example: Dynamic DB

case class Record(id: Long, name: String)

Page 43: Scala 2.10

Example: Dynamic DB

case class Record(id: Long, name: String) object Record extends Dynamic { def applyDynamic(sel: String) (arg: Any): Record = … }

Page 44: Scala 2.10

Example: Dynamic DB

case class Record(id: Long, name: String) object Record extends Dynamic { def applyDynamic(sel: String) (arg: Any): Record = … }

Record.findById(1)

Page 45: Scala 2.10

Example: Dynamic DB

case class Record(id: Long, name: String) object Record extends Dynamic { def applyDynamic(sel: String) (arg: Any): Record = … }

Record.findById(1) Record.applyDynamic("findById")(1)

Page 46: Scala 2.10

What does it bring?

Page 47: Scala 2.10

Macros

Page 48: Scala 2.10

Macros

•  compile-time metaprogramming

Page 49: Scala 2.10

Macros

•  compile-time metaprogramming •  basically functions that are loaded and

executed by the compiler

Page 50: Scala 2.10

Macros

•  compile-time metaprogramming •  basically functions that are loaded and

executed by the compiler – given context and AST of arguments

Page 51: Scala 2.10

Macros

•  compile-time metaprogramming •  basically functions that are loaded and

executed by the compiler – given context and AST of arguments – returned AST inlined and type-checked at the

call site

Page 52: Scala 2.10

Example: logging

logger.debug(s"${expensive} message!")

Page 53: Scala 2.10

Example: logging

if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") }

Page 54: Scala 2.10

Example: logging

if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") }

macro

Page 55: Scala 2.10

Example: logging

if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") }

macro expanded at compile-time

Page 56: Scala 2.10

And last…

Page 57: Scala 2.10

Triple Question Mark

def complexMethod() = ??? •  Defined in class Predef def ??? : Nothing = throw new NotImplementedError

Page 58: Scala 2.10

Recommended