Your SlideShare is downloading. ×
  • Like
An Intro to Scala for PHP Developers
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

An Intro to Scala for PHP Developers


Joseph Price, a software engineer at The Huffington Post gave a talk about the basics of Scala. These slides are a great resource for anyone who's interested in learning more about Scala. PHP …

Joseph Price, a software engineer at The Huffington Post gave a talk about the basics of Scala. These slides are a great resource for anyone who's interested in learning more about Scala. PHP knowledge is not required.

Published in Software
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 2. ● ‘scalable language’ o scales from small to large problems ● object-functional ● runs on JVM ● performant ● concise o type inference o reduced repetition Scala
  • 3. Object Orientation ● Everything’s an object o 1.toDouble ● Supports typical OO functionality - classes, methods, interfaces, inheritance, polymorphism, encapsulation, access modifiers ● Traits o interface with implementation o allows multiple inheritance o avoids ‘diamond problem’ through a technique called linearization
  • 4. Functional programming ● ‘first-class’ functions o anonymous o function values o passed to and returned from functions o higher order functions  map, filter, flatMap ● immutable data structures ● isolation of state, side effects
  • 5. Immutability ● most methods return a new object o immutable data structures ● val / var ● input params cannot change ● iteration - contrast to imperative style o prefer map / fold over stateful looping
  • 6. Type safety ● more confident in changes o library / api updates o refactoring ● static analysis ● find errors more quickly ● avoid common errors ● compiler can optimize typed code
  • 7. Type inference Typing required for ● Method signatures ● Class members ● Recursive methods Optional elsewhere Should be annotated on public methods ● Avoid returning too generic/specific type ● Api stability
  • 8. Concurrency ● immutability enables thread safety o concurrent access to shared mutable state ● Promises / Futures ● actors ● parallel collections
  • 9. val - immutable and immediately evaluated var - mutable and immediately evaluated def ● calculates value on each access (same as a method) ● uniform access principle lazy val ● calculated on first access ● avoid recalculating Variables vs Values
  • 10. Classes, Case classes, Objects classes ● body is the constructor ● getters / setters generated ● public by default o private, protected, restricted private[controllers] case classes ● pattern match ● simplify common operations when working with classes as data ● generated apply, toString, equals, copy methods singleton objects ● common pattern that can be implemented in many (mostly incorrect) ways ● companion objects - shares name and file, holds ‘static’ methods and has privileged access
  • 11. Pattern Matching
  • 12. Option represents presence/absence of a result algebraic data structure ● can be either Some or None null handling ● empty(), is_null(), isset() ● if x != null ● callers are forced to handle absence
  • 13. Try ● represents success or failure of an expression ● Success / Failure ● handle result by calling map, handle failure by calling recover ● moves try/catch control structure into type system
  • 14. Future ● simplifies asynchronous computation ● similar interface to Try ● read-only view of a promise’s result ● Promises produce values asynchronously, future allows binding of callbacks ● similar to jQuery Deferred / Angular promise
  • 15. Collections immutable / mutable ● immutable preferred Most commonly used are Map, List, Seq
  • 16. Magic / dragons ● can be abused, affect compile times ● simplify apis by reducing overhead implicit parameters ● avoid passing parameters repetitively extension methods ● via implicit classes / conversions ● ad-hoc polymorphism Implicit scope
  • 17. DSLs domain specific language internal ● Slick, Scalikejdbc, sbt ● make use of implicits, operator overloading, macros, advanced type system features external ● library support via parser combinators
  • 18. DSLs - ScalikeJDBC val programmers: List[Long] = DB readOnly { implicit session => withSQL { select .from(Programmer as p) .leftJoin(Company as c).on(p.companyId, .where.eq(p.isDeleted, false) .orderBy(p.createdAt) .limit(10) .offset(0) }.map(Programmer(p, c)).list.apply()} Typesafe api that compiles to SQL Makes use of advanced features like Macros and Type Dynamic (like php’s ‘magic methods’ or Ruby’s method_missing)
  • 19. DSLs - Slick ‘Functional relational mapping’ makes use of familiar collections api can drop down to raw SQL
  • 20. DSLs - sbt
  • 21. Actor Model asynchronous message passing between lightweight processes isolate mutable state lifecycle ● let it crash™ ● fault tolerance ● remoting
  • 22. Play ● MVC web framework ● Asynchronous ● Non-blocking ● Built on Akka, Netty ● Highly customizable ● Industry support
  • 23. Environment JVM ● garbage collection, JIT compilation ● reuse existing libraries ● Java interop - call Java code from Scala and vice versa ● tooling, instrumentation SBT IDEs ● IntelliJ, Eclipse o weak support initially, but steadily improving
  • 24. Resources - for IntelliJ - for Eclipse - scala’s build tool - find dependencies - db access using collections-like interface - example slick code - sql dsl - actors, software transactional memory - akka blog - many example projects to get you started - collections complexity - immutable collections class hierarchy - mutable collections class hierarchy
  • 25. Thanks!