0
Scala LiftOff Recap and Refactoring to DSLs by David Orme Eclipse, Rich web, and Scala consulting [email_address] Presente...
Meeting Agenda <ul><li>Agenda: </li><ul><li>Scala LiftOff Recap
Refactoring to DSLs </li><ul><li>Techniques for migrating to Scala </li></ul></ul></ul>
Meeting Agenda <ul><li>Agenda: </li><ul><li>Scala LiftOff Recap
Refactoring to DSLs </li><ul><li>Techniques for migrating to Scala </li></ul></ul></ul>
Scala LiftOff <ul><li>Unconference format </li><ul><li>Began with Martin Odersky prepared keynote
(Some) Attendees came prepared to speak </li><ul><li>Created the schedule
People made suggestions/improvements
Sessions were voluntarily merged
...then we did it </li></ul><li>With so many really smart people present, it worked very well </li></ul></ul>
Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
Enterprise adoption: Status and opportunities
Killer applications / uses
Thinking in functions </li></ul></ul>
Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
Enterprise adoption: Status and opportunities
Killer applications / uses
Thinking in functions </li></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>New collections </li><ul><li>Consistency between muta...
Paid off technical debt – eliminated implementation duplication throughout the library
Consistent implcit conversions to and from Java collections and Scala collections </li></ul></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Named parameters
def resize(width: Int, height: Int) = { ... }
resize(width = 120, height = 42) resize(height = 42, width = 120) </li></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Default parameter values
def f(elems: List[Int],    x: Int = 0,    cond: Boolean = true)
f(List(1))
f(Nil, cond = false) </li></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>break  is now implemented … as a library function!
import scala.util.control.Breaks._
breakable  {
for (x <- elems) {
println(x*2)
if (x > 0)  break
}
} </li></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>New and improved  tool support </li><ul><li>New tooli...
Brand new, much more stable Eclipse plugin.
Tight Java tooling integration
No more ”Project/Clean” or closing/reopening projects to reboot the compiler!
Same tool support also ported to NetBeans
Martin definitely ”gets it” about the importance of having high quality Scala tool support </li></ul></ul></ul>
Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Dave's verdict: </li><ul><li>Important changes at all...
2.8 could be what causes Scala to cross the chasm into the mainstream </li></ul></ul></ul>
Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
Enterprise adoption: Status and opportunities
Killer applications / uses
Thinking in functions </li></ul></ul>
Scala LiftOff <ul><li>Enterprises that have adopted Scala </li><ul><li>Web 2.0 startups </li><ul><li>Twitter
Facebook
RTM
... </li></ul><li>Financial industry, particularly in trading </li><ul><li>EDF Trading
Clarify
Upcoming SlideShare
Loading in...5
×

Refactoring to Scala DSLs and LiftOff 2009 Recap

2,342

Published on

An introductory presentation given to the Chicago Area Scala Enthusiasts (a) beginning with a recap of the Scala LiftOff unconference in Reston, VA, 2009, and (b) describing how to gradually migrate Scala code translated directly from Java into a DSL that facilitates readability and maintainability of client code. We then observe that this technique makes it possible to use Scala DSLs as a way to introduce Scala in a Java shop.

1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,342
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
63
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Refactoring to Scala DSLs and LiftOff 2009 Recap"

  1. 1. Scala LiftOff Recap and Refactoring to DSLs by David Orme Eclipse, Rich web, and Scala consulting [email_address] Presented to Chicago Area Scala Enthusiasts CASE
  2. 2. Meeting Agenda <ul><li>Agenda: </li><ul><li>Scala LiftOff Recap
  3. 3. Refactoring to DSLs </li><ul><li>Techniques for migrating to Scala </li></ul></ul></ul>
  4. 4. Meeting Agenda <ul><li>Agenda: </li><ul><li>Scala LiftOff Recap
  5. 5. Refactoring to DSLs </li><ul><li>Techniques for migrating to Scala </li></ul></ul></ul>
  6. 6. Scala LiftOff <ul><li>Unconference format </li><ul><li>Began with Martin Odersky prepared keynote
  7. 7. (Some) Attendees came prepared to speak </li><ul><li>Created the schedule
  8. 8. People made suggestions/improvements
  9. 9. Sessions were voluntarily merged
  10. 10. ...then we did it </li></ul><li>With so many really smart people present, it worked very well </li></ul></ul>
  11. 11. Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
  12. 12. Enterprise adoption: Status and opportunities
  13. 13. Killer applications / uses
  14. 14. Thinking in functions </li></ul></ul>
  15. 15. Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
  16. 16. Enterprise adoption: Status and opportunities
  17. 17. Killer applications / uses
  18. 18. Thinking in functions </li></ul></ul>
  19. 19. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>New collections </li><ul><li>Consistency between mutable/immutable implementations with a single abstract parent defining the API for both
  20. 20. Paid off technical debt – eliminated implementation duplication throughout the library
  21. 21. Consistent implcit conversions to and from Java collections and Scala collections </li></ul></ul></ul>
  22. 22. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Named parameters
  23. 23. def resize(width: Int, height: Int) = { ... }
  24. 24. resize(width = 120, height = 42) resize(height = 42, width = 120) </li></ul></ul>
  25. 25. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Default parameter values
  26. 26. def f(elems: List[Int], x: Int = 0, cond: Boolean = true)
  27. 27. f(List(1))
  28. 28. f(Nil, cond = false) </li></ul></ul>
  29. 29. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>break is now implemented … as a library function!
  30. 30. import scala.util.control.Breaks._
  31. 31. breakable {
  32. 32. for (x <- elems) {
  33. 33. println(x*2)
  34. 34. if (x > 0) break
  35. 35. }
  36. 36. } </li></ul></ul>
  37. 37. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>New and improved tool support </li><ul><li>New tooling supported directly in scala compiler
  38. 38. Brand new, much more stable Eclipse plugin.
  39. 39. Tight Java tooling integration
  40. 40. No more ”Project/Clean” or closing/reopening projects to reboot the compiler!
  41. 41. Same tool support also ported to NetBeans
  42. 42. Martin definitely ”gets it” about the importance of having high quality Scala tool support </li></ul></ul></ul>
  43. 43. Scala LiftOff <ul><li>Scala 2.8 – Selected Improvements </li><ul><li>Dave's verdict: </li><ul><li>Important changes at all levels of the Scala stack
  44. 44. 2.8 could be what causes Scala to cross the chasm into the mainstream </li></ul></ul></ul>
  45. 45. Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
  46. 46. Enterprise adoption: Status and opportunities
  47. 47. Killer applications / uses
  48. 48. Thinking in functions </li></ul></ul>
  49. 49. Scala LiftOff <ul><li>Enterprises that have adopted Scala </li><ul><li>Web 2.0 startups </li><ul><li>Twitter
  50. 50. Facebook
  51. 51. RTM
  52. 52. ... </li></ul><li>Financial industry, particularly in trading </li><ul><li>EDF Trading
  53. 53. Clarify
  54. 54. Swiss Quote
  55. 55. (some smaller financial firms) </li></ul></ul></ul>
  56. 56. Scala LiftOff <ul><li>How to introduce into an enterprise? </li><ul><li>Non-Prod uses </li><ul><li>More readable unit/integration tests
  57. 57. Build tooling, database loading, scripting, etc... </li></ul><li>Production uses </li><ul><li>A targeted module in a larger system
  58. 58. Often a DSL of some form </li></ul></ul></ul>
  59. 59. Scala LiftOff <ul><li>Remaining challenges </li><ul><li>Commercial support </li><ul><li>Formal training
  60. 60. Someone to sue
  61. 61. This situation is improving, especially in Europe </li></ul><li>Tooling (IDEs, CI infrastructure) </li><ul><li>2.8 significantly improves this </li></ul><li>Licensing (perceived challenge) </li><ul><li>If it's free, it can't be any good
  62. 62. Yes, some people still say this, even in 2009! </li></ul></ul></ul>
  63. 63. Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
  64. 64. Enterprise adoption: Status and opportunities
  65. 65. Killer applications / uses
  66. 66. Thinking in functions </li></ul></ul>
  67. 67. Scala LiftOff <ul><li>Killer Applications/Uses </li><ul><li>DSLs in particular; more generally, anywhere greater abstraction is useful/needed
  68. 68. XML processing </li><ul><li>(since XML is a part of native Scala syntax)
  69. 69. Content-management systems
  70. 70. Web templating engines </li></ul><li>Testing </li><ul><li>Specs, ScalaCheck </li></ul><li>Build, development lifecycle tooling </li><ul><li>SBT keeps coming up! </li></ul></ul></ul>
  71. 71. Scala LiftOff <ul><li>Overall themes: </li><ul><li>Scala 2.8 improvements
  72. 72. Enterprise adoption: Status and opportunities
  73. 73. Killer applications / uses
  74. 74. Thinking in functions </li></ul></ul>
  75. 75. Scala LiftOff <ul><li>Thinking in Functions </li><ul><li>This thought came up again and again: </li><ul><li>Java (OO/Procedural) programs are a series of state mutations </li><ul><li>Change objects' state in-place; the ”result value” changes over time
  76. 76. A, A', A'', A''', … , A (n) </li></ul></ul></ul></ul>
  77. 77. Scala LiftOff <ul><li>Thinking in Functions </li><ul><li>This thought came up again and again: </li><ul><li>Functional-style programs are a series of state transformations </li><ul><li>Transform one result to another repeatedly over time
  78. 78. A -> B -> C -> D -> … -> Z </li></ul><li>Notice how the identity/value of A always stays the same
  79. 79. Notice how this helps with threading and concurrency! </li></ul></ul></ul>
  80. 80. Scala LiftOff <ul><li>Thinking in Functions </li><ul><li>This thought came up again and again: </li><ul><li>Functional-style programs are a series of state transformations </li><ul><li>Transform one result to another repeatedly over time
  81. 81. A -> B -> C -> D -> … -> Z </li></ul></ul><li>See also: Rich Hickey's presentation on InfoQ: </li><ul><li>Are we there yet? </li><ul><li>http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey?utm_source=twitterfeed&utm_medium=twitter </li></ul></ul></ul></ul>
  82. 82. Scala LiftOff <ul><li>Thinking in Functions </li><ul><li>Ironically, LIFTWeb is a very stateful (OO-style) framework.
  83. 83. -> Make sure that your load-balancing server uses sticky sessions (that the same session always goes to the same server) </li></ul></ul>
  84. 84. Scala LiftOff <ul><li>Any questions about the Scala LiftOff?
  85. 85. Overall themes: </li><ul><li>Scala 2.8 improvements
  86. 86. Enterprise adoption: Status and opportunities
  87. 87. Killer applications / uses
  88. 88. Thinking in functions </li></ul></ul>
  89. 89. Meeting Agenda <ul><li>Agenda: </li><ul><li>Scala LiftOff Recap
  90. 90. Refactoring to DSLs </li><ul><li>Techniques for migrating to Scala </li></ul></ul></ul>
  91. 91. Refactoring to DSLs <ul><li>Refactoring to DSLs: Why? </li><ul><li>A good way to introduce Scala
  92. 92. Provides visible, incremental benefits </li></ul></ul>
  93. 93. Refactoring to DSLs <ul><li>Refactoring to DSLs: How? </li><ul><li>Start with something painful or impossible in Java
  94. 94. Translate the basic idea into Scala
  95. 95. Apply Scala idiom(s) to improve code
  96. 96. ”Can we do better?”
  97. 97. Lather, rinse, repeat... </li></ul></ul>
  98. 98. Refactoring to DSLs <ul><li>Example: The O/S Shell DSL </li><ul><li>Simple file operations
  99. 99. Running O/S command and processing results </li><ul><li>I.e.: Perl ”back-tick operator” with variable contents interpolation </li><ul><li>@files = `ls $install_dir`
  100. 100. -> List files in $install_dir; place into @files array </li></ul></ul></ul></ul>
  101. 101. Refactoring to DSLs <ul><li>@files = `ls $install_dir` </li><ul><li>Can we make Scala do (something like) this? </li></ul></ul>
  102. 102. Refactoring to DSLs <ul><li>@files = `ls $install_dir` </li><ul><li>Interpolate variable contents into strings
  103. 103. Run a shell command and return results
  104. 104. Avoid cluttering code with </li><ul><li>new File(”something”) </li></ul><li>line noise when we already know we're working with files anyway </li></ul></ul>
  105. 105. Refactoring to DSLs <ul><li>@files = `ls $install_dir` </li><ul><li>Interpolate variable contents into strings </li><ul><li>Translate the basic idea into Scala
  106. 106. Apply Scala idiom(s) to improve code
  107. 107. ”Can we do better?”
  108. 108. Lather, rinse, repeat... </li></ul></ul></ul>
  109. 109. Refactoring to DSLs <ul><li>Start with: The Java Way, translated to Scala
  110. 110. val folder = &quot;/home&quot;;
  111. 111. println(&quot;ls &quot; + folder)
  112. 112. Can we do better? </li><ul><li>Not easily–if we start from this syntax
  113. 113. Plan B: Is there a way to interpolate ANYTHING into a String using regular Java or Scala? </li></ul></ul>
  114. 114. Refactoring to DSLs <ul><li>What about this idea?
  115. 115. val folder = &quot;/home&quot;;
  116. 116. val formatter = new java.util.Formatter()
  117. 117. println(formatter.format(&quot;ls %s&quot;, folder)) </li><ul><li>Pro: Now we're actually interpolating into the string
  118. 118. Con: More verbose! </li><ul><li>Can we do better? </li></ul></ul></ul>
  119. 119. Refactoring to DSLs <ul><li>What about this idea?
  120. 120. val folder = &quot;/home&quot;;
  121. 121. val formatter = new java.util.Formatter()
  122. 122. println(formatter. format (&quot;ls %s&quot;, folder)) </li><ul><li>What if we use Scala implicits to add the format method to java.lang.String? </li></ul></ul>
  123. 123. Refactoring to DSLs <ul><li>Actually, Scala does that for us already. ;-P
  124. 124. val folder = &quot;/home&quot;;
  125. 125. println(&quot;ls %s&quot;.format(folder)) </li><ul><li>Notice: In order to refactor TO a DSL, all we need to do is to switch the format method to use infix notation </li></ul></ul>
  126. 126. Refactoring to DSLs <ul><li>Actually, Scala does that for us already. ;-P
  127. 127. val folder = &quot;/home&quot;;
  128. 128. println(&quot;ls %s&quot;.format(folder))
  129. 129. println(&quot;ls %s&quot; format folder) </li></ul>
  130. 130. Refactoring to DSLs <ul><li>Actually, Scala does that for us already. ;-P
  131. 131. val folder = &quot;/home&quot;;
  132. 132. println(&quot;ls %s&quot;.format(folder))
  133. 133. println(&quot;ls %s&quot; format folder) </li><ul><li>Pros: This isn't bad OO code
  134. 134. Cons: Asking a String (a data structure) to format another string, when it's really substituting that other string into itself doesn't seem right. </li><ul><li>Can we do better? </li></ul></ul></ul>
  135. 135. Refactoring to DSLs <ul><li>Add our own implicit conversion supplying synonyms for the #format method
  136. 136. val folder = &quot;/home&quot;;
  137. 137. println(&quot;ls %s&quot; substituting folder)
  138. 138. println(&quot;ls %s&quot; << folder)
  139. 139. Notice: We have now created a simple DSL for variable interpolation into strings. </li></ul>
  140. 140. Refactoring to DSLs <ul><li>Let's go play with the code. :-) </li></ul>
  141. 141. Refactoring to DSLs <ul><li>@files = `ls $install_dir` </li><ul><li>Interpolate variable contents into strings
  142. 142. Run a shell command and return results </li><ul><li>Translate the basic idea into Scala
  143. 143. Apply Scala idiom(s) to improve code
  144. 144. ”Can we do better?”
  145. 145. Lather, rinse, repeat... </li></ul></ul></ul>
  146. 146. Refactoring to DSLs <ul><li>Start with: The Java Way, translated to Scala
  147. 147. new PlatformExec(&quot;cat /etc/hosts&quot;).execPrint() </li><ul><li>Write a class that encapsulates Runtime.exec() nastiness
  148. 148. Instantiate it
  149. 149. Execute a method on it </li><ul><li>Can we do better? </li></ul></ul></ul>
  150. 150. Refactoring to DSLs <ul><li>What if we make that big OO thing into a single function call?
  151. 151. execute(&quot;cat /etc/passwd&quot;) </li><ul><li>Pros: Definitely simpler/easier to read
  152. 152. Cons: Still not nearly as convenient as back-tick notation </li><ul><li>Can we do better? </li></ul></ul></ul>
  153. 153. Refactoring to DSLs <ul><li>What if we add the execute method directly to java.lang.String using Scala's implicit type conversions? </li><ul><li>This enables us to write this:
  154. 154. for (line <- &quot;ls&quot;.exec().split(&quot; &quot;)) {
  155. 155. println(&quot;-> &quot; + line)
  156. 156. } </li></ul></ul>
  157. 157. Refactoring to DSLs <ul><li>What if we add the execute method directly to java.lang.String as a lexically-scoped implicit? </li><ul><li>Or this:
  158. 158. &quot;ls&quot;.exec().split(&quot; &quot;).foreach { file =>
  159. 159. println(&quot;=> &quot; + file)
  160. 160. } </li></ul><li>Not bad. And sometimes this is perfect. </li><ul><li>But sometimes we just want a backtick operator! </li><ul><li>Can we do better? </li></ul></ul></ul>
  161. 161. Refactoring to DSLs <ul><li>We can add methods to java.lang.String, and operators are just methods.
  162. 162. Is there an operator that could reasonably mean ”execute” to a String? </li></ul>
  163. 163. Refactoring to DSLs <ul><li>How about unary_! as Scala's backtick operator? Then we can write: </li><ul><li>!&quot;cat /etc/passwd&quot; </li></ul><li>Or: </li><ul><li>val name = &quot;Dave&quot; !(&quot;echo I'm sorry, %s, I just can't do that&quot; substituting name)
  164. 164. Pro: We now have a reasonable ”backtick” operator, and a lot of OO/FP flexibility as well.
  165. 165. Con: A lot of these operations would be better done with a straight java.io.File </li><ul><li>Can we do better? </li></ul></ul></ul>
  166. 166. Refactoring to DSLs <ul><li>@files = `ls $install_dir` </li><ul><li>Interpolate variable contents into strings
  167. 167. Run a shell command and return results
  168. 168. Avoid cluttering code with </li><ul><li>new File(”something”) </li></ul><li>line noise when we already know we're working with files anyway </li></ul></ul>
  169. 169. Refactoring to DSLs <ul><li>Simply add an implicit conversion converting a java.lang.String into a java.io.File: </li><ul><li>implicit def string2File(s : String) = new java.io.File(s) </li></ul><li>Then we can write: </li><ul><li>for (file <- &quot;/home/djo&quot;.list()) { println(&quot;==> %s&quot; << file) } </li></ul></ul>
  170. 170. Refactoring to DSLs <ul><li>Simply add an implicit conversion converting a java.lang.String into a java.io.File: </li><ul><li>implicit def string2File(s : String) = new java.io.File(s) </li></ul><li>Then we can write: </li><ul><li>for (file <- &quot;/home/djo&quot;.list()) { println(&quot;=> %s&quot; << file) } </li></ul><li>Or even:
  171. 171. &quot;/home/djo&quot;.list.foreach(println(&quot;=> %s&quot; << _) ) </li></ul>
  172. 172. Refactoring to DSLs <ul><li>Let's go play with the code. :-) </li></ul>
  173. 173. Refactoring to DSLs <ul><li>By carefully applying Scala idioms, we find ourselves naturally refactoring our code into a DSL that fits our problem domain </li><ul><li>-> Use Scala's infix notation to make OO code read like English
  174. 174. -> Wrap repetitive Java code in ”bridge classes”
  175. 175. -> When appropriate, make those ”bridge classes” available via implicit conversions
  176. 176. -> Carefully use operator overloading </li></ul></ul>
  177. 177. Refactoring to DSLs <ul><li>By carefully applying Scala idioms, we find ourselves naturally refactoring our code into a DSL that fits our problem domain
  178. 178. This provides us with a simple, repeatable migration path off of Java, onto Scala </li></ul>
  179. 179. CASE <ul><li>Questions? Comments?
  180. 180. Presentation available on SlideShare </li></ul>
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×