Conservative Language Use For Fun and Profit

Conservative Language Use For Fun and Profit

Scala provides many tools for building programs. Depending on how you
look at it, this can be a great thing, or a terrifying thing. I will
take a look at the scala "sub-set" that I adopted over years of pain,
and hopefully demonstrate that you can get more benefit from scala by
adopting less of it. In the end my sub-set may be different to yours,
but it might give you some different ideas on how to approach
programming in scala (or in general).

42d9867a0fee0fa6de6534e9df0f1e9b?s=128

Mark Hibberd

February 10, 2016
Tweet

Transcript

  1. Conservative Mark Hibberd Scala

  2. You can make better use of tools by having deep

    knowledge of a tool, but only applying a specialised subset of its features. Claim #1
  3. You can’t effectively control your use of tools to a

    specialised subset unless you have very deep knowledge of the tool. Claim #2
  4. Programming languages are just tools. Claim #3

  5. Conservative usage patterns generalise to lots of tools not just

    languages. Claim #4
  6. None
  7. SCALA a primer

  8. 1 scala> val x = List("1", "2", "3", "4").toSet() +

    "5" 2 3 scala> println(x) 4 5 output: 6 7 ???
  9. 1 scala> val x = List("1", "2", "3", "4").toSet() +

    "5" 2 3 scala> println(x) 4 5 output: 6 7 false5
  10. 1 scala> println({} + "") 2 3 output: 4 5

    ???
  11. 1 scala> println({} + "") 2 3 output: 4 5

    ()
  12. 1 scala> val x = Option(1).zip(Option(1)) == 2 Option((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 ???
  13. 1 scala> val x = Option(1).zip(Option(1)) == 2 Option((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 false
  14. 1 scala> val x = Option(1).zip(Option(1)) == 2 List((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 ???
  15. 1 scala> val x = Option(1).zip(Option(1)) == 2 List((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 true
  16. 1 scala> val x = Option(1).zip(Option(1)) == 2 Vector((1, 1))

    3 4 scala> println(x) 5 6 output: 7 8 true
  17. None
  18. scalac thinks your code is bad

  19. 1 List("1", "2", "3", "4").toSet() + “5"

  20. 1 List("1", "2", "3", "4").toSet() + "5" 2 3 //

    example.scala:1: error: not enough arguments for 4 // method apply: (elem: Any)Boolean in trait GenSet. 5 // Unspecified value parameter elem. -Yno-adapted-args
  21. 1 def update(data: String): Future[Unit] = for { 2 n

    <- get 3 _ <- save(n, data) 4 } yield set(n + 1) 5 6 def get: Future[Int] = 7 ??? 8 9 def set(n: Int): Future[Int] = 10 ??? 11 12 def save(n: Int, data: String): Future[Unit] = 13 ???
  22. 1 def update(data: String): Future[Unit] = for { 2 n

    <- get 3 _ <- save(n, data) 4 } yield set(n + 1) 5 6 //example.scala:4: warning: discarded non-Unit value -Ywarn-value-discard
  23. -Xlint / -Ywarn-all https://github.com/scala/scala/blob/v2.10.3/src/compiler/scala/tools/nsc/settings/ Warnings.scala#L18-L44

  24. -Xfatal-warnings

  25. -Yno-predef

  26. scala scala vs

  27. Lets reduce the surface area of the language we have

    to worry about
  28. Lets not save characters by risking correctness

  29. Lets not make it “easy” by building APIs using ever

    scala feature ever
  30. Lets do more with less

  31. Syntax

  32. Types & Inference

  33. Closed better than Open

  34. Don’t confuse Types and Data

  35. Scala does not have Type Inference

  36. Variance or Not

  37. Any is the same as not having Types

  38. Implicits

  39. Type Classes vs Global Mutable HashMaps

  40. Syntactic Extensions

  41. Scala is just a Tool

  42. Performance vs Correctness

  43. Concurrent Scala is not a Win

  44. Integration not Ecosystem

  45. End Game