0
Scala 2.10Czech Scala EnthusiastsMay 28th 2013Jakub Janeček
What does it bring?
Language Modularization
Language Modularization•  some features are confusing,“dangerous” orstill experimental
Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explic...
Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explic...
Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explic...
Example: Postfix OperatorsList(1, 2, 3) reverse
Example: Postfix OperatorsList(1, 2, 3) reverseimport language.postfixOps
Example: Postfix OperatorsList(1, 2, 3) reverse> scalac –language:postfixOps
What does it bring?
String Interpolation
String Interpolation•  Remember?"1 + 1 = " + (1 + 1)
String Interpolation•  Remember?"1 + 1 = " + (1 + 1)•  No more!s"1 + 1 = ${1 + 1}"
String Interpolation"1 + 1 = " + (1 + 1)•  No more!s"1 + 1 = ${1 + 1}"Standard interpolatorWe also have f and rawEscape ch...
Example: Interpolator revdef rev(args: Any*): String = …•  Extension method of StringContext:
Example: Interpolator revdef rev(args: Any*): String = …rev”this will be reversed"•  Extension method of StringContext:
What does it bring?
“value”Value Classes
Value Classes•  allow extends AnyVal
Value Classes•  allow extends AnyVal•  compiler can usually avoid allocating runtimeobjects à performance with type safety
Value Classes•  allow extends AnyVal•  compiler can usually avoid allocating runtimeobjects à performance with type safet...
Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne
Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne
Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOneMyI...
What does it bring?
Cute Puppy
Cute Puppy Implicit Classes
Implicit Classes•  remove a lot of boilerplate code
Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first paramete...
Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first paramete...
Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first paramete...
Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first paramete...
Example: MyIntimplicit class MyInt(val i: Int)extends AnyVal {def plusOne = i + 1}2.plusOne
Example: MyIntimplicit class MyInt(val i: Int)extends AnyVal {def plusOne = i + 1}2.plusOne
What does it bring?
Trait Dynamic
Trait Dynamic•  allows calling methods not existing in the statictype
Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclangua...
Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclangua...
Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclangua...
Example: Dynamic DBcase class Record(id: Long, name: String)
Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(a...
Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(a...
Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(a...
What does it bring?
Macros
Macros•  compile-time metaprogramming
Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler
Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler– given context and...
Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler– given context and...
Example: logginglogger.debug(s"${expensive} message!")
Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}
Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro
Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro expanded at compile-time
And last…
Triple Question Markdef complexMethod() = ???•  Defined in class Predefdef ??? : Nothing = throw new NotImplementedError
Scala 2.10
Upcoming SlideShare
Loading in...5
×

Scala 2.10

515

Published on

Presentation about new features of Scala 2.10. It was presented by Jakub Janeček at the May's Czech Scala Enthusiasts meetup held at Faculty of Information Technology in Prague.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
515
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Scala 2.10"

  1. 1. Scala 2.10Czech Scala EnthusiastsMay 28th 2013Jakub Janeček
  2. 2. What does it bring?
  3. 3. Language Modularization
  4. 4. Language Modularization•  some features are confusing,“dangerous” orstill experimental
  5. 5. Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explicitly enabled(otherwise warning or error is emitted)
  6. 6. Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explicitly enabled(otherwise warning or error is emitted)•  for example:– implicit conversions
  7. 7. Language Modularization•  some features are confusing,“dangerous” orstill experimental•  since 2.10 they have to be explicitly enabled(otherwise warning or error is emitted)•  for example:– implicit conversions– macros
  8. 8. Example: Postfix OperatorsList(1, 2, 3) reverse
  9. 9. Example: Postfix OperatorsList(1, 2, 3) reverseimport language.postfixOps
  10. 10. Example: Postfix OperatorsList(1, 2, 3) reverse> scalac –language:postfixOps
  11. 11. What does it bring?
  12. 12. String Interpolation
  13. 13. String Interpolation•  Remember?"1 + 1 = " + (1 + 1)
  14. 14. String Interpolation•  Remember?"1 + 1 = " + (1 + 1)•  No more!s"1 + 1 = ${1 + 1}"
  15. 15. String Interpolation"1 + 1 = " + (1 + 1)•  No more!s"1 + 1 = ${1 + 1}"Standard interpolatorWe also have f and rawEscape character•  Remember?
  16. 16. Example: Interpolator revdef rev(args: Any*): String = …•  Extension method of StringContext:
  17. 17. Example: Interpolator revdef rev(args: Any*): String = …rev”this will be reversed"•  Extension method of StringContext:
  18. 18. What does it bring?
  19. 19. “value”Value Classes
  20. 20. Value Classes•  allow extends AnyVal
  21. 21. Value Classes•  allow extends AnyVal•  compiler can usually avoid allocating runtimeobjects à performance with type safety
  22. 22. Value Classes•  allow extends AnyVal•  compiler can usually avoid allocating runtimeobjects à performance with type safety•  they have limitations
  23. 23. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne
  24. 24. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne
  25. 25. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOneMyInt$.MODULE$.plusOne$extension(5)
  26. 26. What does it bring?
  27. 27. Cute Puppy
  28. 28. Cute Puppy Implicit Classes
  29. 29. Implicit Classes•  remove a lot of boilerplate code
  30. 30. Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first parameter list
  31. 31. Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first parameter list•  implicit conversion is generated
  32. 32. Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first parameter list•  implicit conversion is generated•  often used withValue Classes
  33. 33. Implicit Classes•  remove a lot of boilerplate code•  class must have only one parameter in itsconstructors first parameter list•  implicit conversion is generated•  often used withValue Classes•  must be defined inside other class/trait/object
  34. 34. Example: MyIntimplicit class MyInt(val i: Int)extends AnyVal {def plusOne = i + 1}2.plusOne
  35. 35. Example: MyIntimplicit class MyInt(val i: Int)extends AnyVal {def plusOne = i + 1}2.plusOne
  36. 36. What does it bring?
  37. 37. Trait Dynamic
  38. 38. Trait Dynamic•  allows calling methods not existing in the statictype
  39. 39. Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclanguages
  40. 40. Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclanguages•  empty marker trait Dynamic
  41. 41. Trait Dynamic•  allows calling methods not existing in the statictype•  useful for DSLs and integration with dynamiclanguages•  empty marker trait Dynamic•  if type check fails it is rewritten to one of–  applyDynamic–  applyDynamicNamed–  selectDynamic–  updateDynamic
  42. 42. Example: Dynamic DBcase class Record(id: Long, name: String)
  43. 43. Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(arg: Any): Record = …}
  44. 44. Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(arg: Any): Record = …}Record.findById(1)
  45. 45. Example: Dynamic DBcase 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. 46. What does it bring?
  47. 47. Macros
  48. 48. Macros•  compile-time metaprogramming
  49. 49. Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler
  50. 50. Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler– given context and AST of arguments
  51. 51. Macros•  compile-time metaprogramming•  basically functions that are loaded andexecuted by the compiler– given context and AST of arguments– returned AST inlined and type-checked at thecall site
  52. 52. Example: logginglogger.debug(s"${expensive} message!")
  53. 53. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}
  54. 54. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro
  55. 55. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro expanded at compile-time
  56. 56. And last…
  57. 57. Triple Question Markdef complexMethod() = ???•  Defined in class Predefdef ??? : Nothing = throw new NotImplementedError
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×