Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala 2.10

Scala 2.10

Presented at Czech Scala Enthusiasts meetup in May 2013.

Jakub Janeček

May 28, 2013

Transcript

58 slides
  1. 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)
  2. 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
  3. 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
  4. String Interpolation •  Remember? "1 + 1 = " +

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

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

    will be reversed" •  Extension method of StringContext:
  7. Value Classes •  allow extends AnyVal •  compiler can usually

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

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

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

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

    def plusOne = MyInt(underlying + 1) } MyInt(5).plusOne MyInt$.MODULE$.plusOne$extension(5)
  12. Implicit Classes •  remove a lot of boilerplate code • 

    class must have only one parameter in its constructor's first parameter list
  13. 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
  14. 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
  15. 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
  16. Trait Dynamic •  allows calling methods not existing in the

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

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

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

    Record extends Dynamic { def applyDynamic(sel: String) (arg: Any): Record = … } Record.findById(1)
  21. 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)
  22. Macros •  compile-time metaprogramming •  basically functions that are loaded

    and executed by the compiler – given context and AST of arguments
  23. 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
  24. Triple Question Mark def complexMethod() = ??? •  Defined in

    class Predef def ??? : Nothing = throw new NotImplementedError

More Decks by Jakub Janeček

Other Decks in Programming