Your SlideShare is downloading. ×
Intro To Scala
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro To Scala


Published on

High level overview of some Scala concepts.

High level overview of some Scala concepts.

  • Be the first to comment

  • Be the first to like this

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


  • 1. Intro to Scala
  • 2. What is Scala?
    • Hybrid Language, blending OO and FP
    • Statically Typed
    • Runs on the JVM
  • 3. OO Features
  • 4. Classes
    • No need to add getters/setters for parameters
    Default Constructor
  • 5. Objects
    • No Concept of Static in Scala
    • Standalone
    • Companion
  • 6. Standalone Object Calling the method
  • 7. Companion Object
    • Object and Class must have the same name
    • Declared in the same file with the class def is contained within the object def
  • 8. Methods Don’t need a return statement. The last line of the method is What gets returned.
  • 9. Imports & Packages Import Statements Scoped Packages
  • 10. Type System
  • 11. Type System
    • Java
    • List<String>
    • Contravariant
      • List<T super Foo>
    • Covariant
      • List<T extends Foo>
    • Supplied when a class is used
    • Scala
    • List[String]
    • Contravariant
      • List[-T]
    • Covariant
      • List[+T]
    • Supplied when a class is defined
  • 12. Type System
    • Covariant: narrower -> wider.
      • Double -> float
    • Contravariant: wider -> narrower
      • Float -> double
    • Invariant: Cannot convert
  • 13. Type System
  • 14. Type System Call drawAll on Canvas with a list of circles
  • 15. Type System
    • Looks interesting but what's the catch?
      • All examples used immutable types. When working with mutable types in Scala they have to be invariant.
    Immutable Mutable
  • 16. Implicits
  • 17. Implicit Conversion Implicit Conversions provide a nice way to extend libraries and functionality without changing the original code. Example: Add methods to String
  • 18. Implicit Conversion Must Import the Object in order to use the new methods It looks like we are calling the repeat method on the string class!
  • 19. Implicit Parameters Implicit parameters are used in a functions parameter list to denote that the parameter Is optional. Declare the parameter to be implicit Label the list as implicit so the compiler knows Which list to use.
  • 20. Traits
  • 21. Traits Like an interface with an implementation. Traits are mixed into classes by using the keyword with. Allowed to have abstract as well as concrete implementation of methods. Version 1
  • 22. Traits Version 2
  • 23. Traits It is possible to have multiple traits that have the same method. This is known as stackable modifications By extending Arithmetic we are saying that this is only valid for classes that extend Arithmetic Calls the next compute method in line A hint to the compiler that you are “stacking” traits
  • 24. Traits Output: 8 Output: 16
  • 25. Traits If you remember from the last example the result between the two runs were different. This is because we changed the order of the traits between the two executions. Ordering of traits makes a difference! By using something called linearization classes and all of the traits are put into a linear order (right to left).  By putting them in this order, each time you call super  the next method up the chain is invoked. 2 + 2 = 4 4 * 4 = 16 2 * 2 = 4 4 + 4 = 8
  • 26. Functional Features
  • 27. Immutability
    • First class concept in Scala
    • By default all Collection implementations are immutable
    • val & var
      • var can be reassigned
      • val cannot, and must be assigned to something when first declared.
  • 28. Concurrency
    • Actor Based. With each actor having a mailbox.
    • Message sends are asynch
    • Implemented on top of Java Threads
  • 29. Concurrency Event Based Actors In event based actors a thread pool is used and each actor is given a thread to execute.
  • 30. Concurrency Thread Based Actors With thread based actors, every actor is tied to a thread.
  • 31. Concurrency Starting an actor and sending a message:
  • 32. Pattern Matching
    • Allow for pattern matching on objects
    • “ added value” when you declare a case class
      • Factory method with name of class
      • Parameters are maintained as fields
    • Patterns are meant to be linear
  • 33. Pattern Matching Basic Example: Matching a String or Int Output: “Look a String!” Output: “Look an Int!” Simple instanceof check
  • 34. Pattern matching Implementing the visitor pattern:
  • 35. Functions
  • 36. Functions Partial Function: Passing a function as a parameter: Returning a function from a method:
  • 37. Other Notable Features
  • 38.
    • Named & Default Args
    • Continuations
    • Lazy evaluation (lazy keyword)
    • REPL
    • Scoped imports
    • Collections API