Introduction to Scala @ TWJUG 2010/07

Introduction to Scala @ TWJUG 2010/07

Introduction to Scala @ Taiwan Java User Group 2010/07

2377fa6c1b0f089b7e3229dfe2f6ae7f?s=128

Brian Hsu

July 31, 2010
Tweet

Transcript

  1. Introduction Scala BrianHsu @ TWJUG

  2. 關於我

  3. http://bone.twbbs.org.tw/blog/ 墳墓 (BrianHsu)

  4. 其實我不懂 Scala

  5. Scala 非常難

  6. None
  7. None
  8. 我只會簡單的部份 但我用得很高興!

  9. 特別是拿來當 Script Language

  10. 工欲善其事 必先利其器

  11. 所有的武林高手 都有趁手的兵刃

  12. 程式員的兵器 程式語言

  13. 我的個性

  14. 粗心、懶

  15. 所以 我的兵器在哪?

  16. 動態語言

  17. 3P 好好玩 Python/Perl/PHP

  18. 後起之秀 Ruby

  19. 因為粗心

  20. 討厭執行期爆炸 def boom puts "Hello World" end puts " 一個小小的

    typo" booom
  21. 對不起,我是懦夫 我不敢用這些語言

  22. 靜態語言

  23. 真男人就是要冠 C C 語言

  24. D 語言 Haskell

  25. 我們最愛的 Java Programming Langauge

  26. 我們最恨的 Java Programming Langauge

  27. 因為懶

  28. public static void main (String [] args) { System.out.println(“Hello World”)

    }
  29. public static void main (String [] args) { System.out.println(“Hello World”)

    } 浪費生命!
  30. println ("Hello World")

  31. None
  32. What is Scala?

  33. 樓 梯

  34. A Postfunctional Programming Language on Java Virtual Machine

  35. Compile to JVM .class Byte Code

  36. 理論上 Full Java Interoperability

  37. What is Scala?

  38. A Scalable Language

  39. 會隨 User 變形的程式語言

  40. A Scalable Language

  41. A Scalable Language

  42. Why Scala?

  43. None
  44. 三個願望一次滿足

  45. REPL Scripting Compiling

  46. REPL

  47. Scripting

  48. Compiling

  49. 三個願望一次滿足

  50. Procedure Object Oriented Functional

  51. Procedure Programming

  52. Object Oriented Programming

  53. Functional Programming

  54. 三個願望一次滿足

  55. Concise Static Typing Duck Typing

  56. Concise (Ruby)

  57. Concise (Scala)

  58. Static Typing (Ruby)

  59. Static Typing (Scala)

  60. 真、 Duck Typing (Ruby)

  61. 偽‧ Duck Typing (Scala)

  62. 結論

  63. 有 Scala 、無懦夫

  64. Scala vs Java

  65. Scala compared to Java Scala adds Scala removes + a

    pure object system - static members + operator overloading - primitive types + closures - break, continue + mixin composition with traits - special treatment of interfaces + existential types - wildcards + abstract types - raw types + pattern matching - enums Modeled in libraries: assert, enums, properties, events, actors, using, queries, …
  66. Scala cheat sheet (1): Definitions Scala method definitions: def fun(x:

    Int): Int = { result } def fun = result Scala variable definitions: var x: Int = expression val x: String = expression Java method definition: int fun(int x) { return result } (no parameterless methods) Java variable definitions: int x = expression final String x = expression
  67. Scala cheat sheet (2): Expressions Scala method calls: obj.meth(arg) obj

    meth arg Scala choice expressions: if (cond) expr1 else expr2 expr match { case pat 1 => expr 1 .... case pat n => expr n } Java method call: obj.meth(arg) (no operator overloading) Java choice expressions, stmts: cond ? expr1 : expr2 if (cond) return expr1; else return expr2; switch (expr) { case pat 1 : return expr 1 ; ... case pat n : return expr n ; } // statement only
  68. Scala cheat sheet (3): Objects and Classes Scala Class and

    Object class Sample(x: Int, val p: Int) { def instMeth(y: Int) = x + y } object Sample { def staticMeth(x: Int, y: Int) = x * y } Java Class with statics class Sample { private final int x; public final int p; Sample(int x, int p) { this.x = x; this.p = p; } int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) { return x * y; } }
  69. Scala cheat sheet (4): Traits Scala Trait trait T {

    def abstractMth(x: String): Int def concreteMth(x: String) = x + field var field = “!” } Scala mixin composition: class C extends Super with T Java Interface interface T { int abstractMth(String x) } (no concrete methods) (no fields) Java extension + implementation: class C extends Super implements T
  70. Scala 的特異功能

  71. 這次沒唬你 萬物皆物件

  72. 你可以這樣 1.toString

  73. 我們都知道 toString 是 Method

  74. 1+1 + 也是 Method !

  75. 1+2 = 1 .+ (2)

  76. 向 static 說再見 預設的 Singleton

  77. List.range(1, 5) = List(1,2,3,4,5)

  78. List 是 class range 是 class method

  79. List 是一個 Singleton 物件!

  80. range(1,2) 是一個 instance method

  81. 只有一個 唯一的 List 物件

  82. None
  83. 懶人的好幫手之一 Tuple

  84. 簡單資料結構

  85. 座標 鍵值

  86. None
  87. 懶人的好幫手之二 case calss

  88. None
  89. None
  90. 加強版的 swtich Pattern Matching

  91. None
  92. None
  93. None
  94. 加強版(幾乎沒在用)的 FOR expression

  95. None
  96. 建忘的救星 (2.8.0 才有喔! ) Default / Named Arguments

  97. None
  98. Function Programming Function Literal

  99. Function Programming Closure

  100. Function Programming High-Order Function

  101. None
  102. Lazy

  103. None
  104. 可以有實作的 Interface Trait

  105. None
  106. 等等,這是孔明的陷阱!

  107. Scala 的限制與臭蟲 • 限制 • Cannot acess static protected Java

    field. – 對不起, Google Maps API 用到了。 • 臭蟲 • Static Java Inner class 造成 classfile borken / missing dependcy – Google Maps API 踩到地雷了 – Fixed in latest SVN trunk • 解決方式 • 用 Java 寫個 Wrapper 唄 Missing dependency
  108. Thanks!