Scala 2.10

Scala 2.10

Presented at Czech Scala Enthusiasts meetup in May 2013.

6048e3cd01eb6e6aaa0c469b6ed7ad08?s=128

Jakub Janeček

May 28, 2013
Tweet

Transcript

  1. Scala 2.10 Czech Scala Enthusiasts May 28th 2013 Jakub Janeček

  2. What does it bring?

  3. Language Modularization

  4. Language Modularization •  some features are confusing, “dangerous” or still

    experimental
  5. 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)
  6. 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
  7. 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
  8. Example: Postfix Operators List(1, 2, 3) reverse

  9. Example: Postfix Operators List(1, 2, 3) reverse import language.postfixOps

  10. Example: Postfix Operators List(1, 2, 3) reverse > scalac –language:postfixOps

  11. What does it bring?

  12. String Interpolation

  13. String Interpolation •  Remember? "1 + 1 = " +

    (1 + 1)
  14. String Interpolation •  Remember? "1 + 1 = " +

    (1 + 1) •  No more! s"1 + 1 = ${1 + 1}"
  15. String Interpolation "1 + 1 = " + (1 +

    1) •  No more! s"1 + 1 = ${1 + 1}" Standard interpolator We also have f and raw Escape character •  Remember?
  16. Example: Interpolator rev def rev(args: Any*): String = … • 

    Extension method of StringContext:
  17. Example: Interpolator rev def rev(args: Any*): String = … rev”this

    will be reversed" •  Extension method of StringContext:
  18. What does it bring?

  19. “value” Value Classes

  20. Value Classes •  allow extends AnyVal

  21. Value Classes •  allow extends AnyVal •  compiler can usually

    avoid allocating runtime objects à performance with type safety
  22. Value Classes •  allow extends AnyVal •  compiler can usually

    avoid allocating runtime objects à performance with type safety •  they have limitations
  23. Example: MyInt case class MyInt(val underlying: Int) extends AnyVal {

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

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

    def plusOne = MyInt(underlying + 1) } MyInt(5).plusOne MyInt$.MODULE$.plusOne$extension(5)
  26. What does it bring?

  27. Cute Puppy

  28. Cute Puppy Implicit Classes

  29. Implicit Classes •  remove a lot of boilerplate code

  30. Implicit Classes •  remove a lot of boilerplate code • 

    class must have only one parameter in its constructor's first parameter list
  31. 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
  32. 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
  33. 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
  34. Example: MyInt implicit class MyInt(val i: Int) extends AnyVal {

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

    def plusOne = i + 1 } 2.plusOne
  36. What does it bring?

  37. Trait Dynamic

  38. Trait Dynamic •  allows calling methods not existing in the

    static type
  39. Trait Dynamic •  allows calling methods not existing in the

    static type •  useful for DSLs and integration with dynamic languages
  40. Trait Dynamic •  allows calling methods not existing in the

    static type •  useful for DSLs and integration with dynamic languages •  empty marker trait Dynamic
  41. 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
  42. Example: Dynamic DB case class Record(id: Long, name: String)

  43. Example: Dynamic DB case class Record(id: Long, name: String) object

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

    Record extends Dynamic { def applyDynamic(sel: String) (arg: Any): Record = … } Record.findById(1)
  45. 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)
  46. What does it bring?

  47. Macros

  48. Macros •  compile-time metaprogramming

  49. Macros •  compile-time metaprogramming •  basically functions that are loaded

    and executed by the compiler
  50. Macros •  compile-time metaprogramming •  basically functions that are loaded

    and executed by the compiler – given context and AST of arguments
  51. 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
  52. Example: logging logger.debug(s"${expensive} message!")

  53. Example: logging if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") }

  54. Example: logging if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") } macro

  55. Example: logging if (logger.isDebugEnabled) { logger.debug(s"${expensive} message!") } macro expanded

    at compile-time
  56. And last…

  57. Triple Question Mark def complexMethod() = ??? •  Defined in

    class Predef def ??? : Nothing = throw new NotImplementedError
  58. None