On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
Type System Call drawAll on Canvas with a list of circles
Looks interesting but what's the catch?
All examples used immutable types. When working with mutable types in Scala they have to be invariant.
Implicit Conversion Implicit Conversions provide a nice way to extend libraries and functionality without changing the original code. Example: Add methods to String
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!
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.
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
Traits Version 2
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
Traits Output: 8 Output: 16
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
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.
Actor Based. With each actor having a mailbox.
Message sends are asynch
Implemented on top of Java Threads
Concurrency Event Based Actors In event based actors a thread pool is used and each actor is given a thread to execute.
Concurrency Thread Based Actors With thread based actors, every actor is tied to a thread.
Concurrency Starting an actor and sending a message:
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
Pattern Matching Basic Example: Matching a String or Int Output: “Look a String!” Output: “Look an Int!” Simple instanceof check
Pattern matching Implementing the visitor pattern:
Functions Partial Function: Passing a function as a parameter: Returning a function from a method: