Scala 2.10

  • View
    496

  • Download
    0

Embed Size (px)

DESCRIPTION

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.

Text of Scala 2.10

  • 1.Scala 2.10Czech Scala EnthusiastsMay 28th 2013Jakub Janeek

2. What does it bring? 3. Language Modularization 4. Language Modularization some features are confusing,dangerous orstill experimental 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. 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. 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 conversionsmacros 8. Example: Postfix OperatorsList(1, 2, 3) reverse 9. Example: Postfix OperatorsList(1, 2, 3) reverseimport language.postfixOps 10. Example: Postfix OperatorsList(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 interpolatorWe also have f and rawEscape character Remember? 16. Example: Interpolator revdef rev(args: Any*): String = Extension method of StringContext: 17. Example: Interpolator revdef rev(args: Any*): String = revthis will be reversed" Extension method of StringContext: 18. What does it bring? 19. valueValue Classes 20. Value Classes allow extends AnyVal 21. Value Classes allow extends AnyVal compiler can usually avoid allocating runtimeobjects performance with type safety 22. Value Classes allow extends AnyVal compiler can usually avoid allocating runtimeobjects performance with type safety they have limitations 23. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne 24. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOne 25. Example: MyIntcase class MyInt(val underlying: Int)extends AnyVal {def plusOne = MyInt(underlying + 1)}MyInt(5).plusOneMyInt$.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 itsconstructors first parameter list 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. 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. 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. Example: MyIntimplicit class MyInt(val i: Int)extends AnyVal {def plusOne = i + 1}2.plusOne 35. Example: MyIntimplicit 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 statictype 39. Trait Dynamic allows calling methods not existing in the statictype useful for DSLs and integration with dynamiclanguages 40. Trait Dynamic allows calling methods not existing in the statictype useful for DSLs and integration with dynamiclanguages empty marker trait Dynamic 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. Example: Dynamic DBcase class Record(id: Long, name: String) 43. Example: Dynamic DBcase class Record(id: Long, name: String)object Record extends Dynamic {def applyDynamic(sel: String)(arg: Any): Record = } 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. 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. What does it bring? 47. Macros 48. Macros compile-time metaprogramming 49. Macros compile-time metaprogramming basically functions that are loaded andexecuted by the compiler 50. Macros compile-time metaprogramming basically functions that are loaded andexecuted by the compilergiven context and AST of arguments 51. Macros compile-time metaprogramming basically functions that are loaded andexecuted by the compilergiven context and AST of argumentsreturned AST inlined and type-checked at thecall site 52. Example: logginglogger.debug(s"${expensive} message!") 53. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")} 54. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro 55. Example: loggingif (logger.isDebugEnabled) {logger.debug(s"${expensive} message!")}macro expanded at compile-time 56. And last 57. Triple Question Markdef complexMethod() = ??? Defined in class Predefdef ??? : Nothing = throw new NotImplementedError