Send the link below via email or IMCopy
Present to your audienceStart remote presentation
- Invited audience members will follow you as you navigate and present
- People invited to a presentation do not need a Prezi account
- This link expires 10 minutes after you close the presentation
- A maximum of 30 users can follow your presentation
- Learn more about this feature in our knowledge base article
Do you really want to delete this prezi?
Neither you, nor the coeditors you shared it with will be able to recover it again.
Make your likes visible on Facebook?
Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.
From Java to Scala (with notes)
Transcript of From Java to Scala (with notes)
Composite What's left out Better Generics
Lazy members, args
Existential types Generalized type constraints
Path dependent types
Delimited continuations Lines of code Java: 764
Scala: 382 To On The Scala logo is a trademark of EPFL The Java logo is a trademark of Oracle The Android logo is a trademark of Google Traits Multiple inheritance in code,
single inheritance in bytecode Enhanced Functionality Need to merge cached feed with current
Sort & merge Groups
feed url ArticleInfo
url Domain Model Not just getters and setters Pattern Matching -
Declerations, not Conditions Article reader @ tikalk.com Settings Activity "a single, focused thing that the user can do"
The controller of a GUI Screen
Create view programmatically or from XML layout
React to events ListActivity An activity for displaying lists
onListItemClicked ListAdapter Converts list elements to row views Get all groups
Populate list view using adapter
Get previously selected groups
Tick list items
onListItemClicked: Add/remove selected groups Flow Settings SharedPreferences can store simple types Groups Get selected groups from Settings
Populate list view using adapter
onListItemClicked: Open ArticlesActivity Flow Articles An XML for arranging widgets
"Inflated" to objects
Optional widget ID with auto generated constant
IDs used in activity to set values in widgets Layout Conclusion Is Scala Complex? Class Definition Collections Disadvantages:
Modifications between next() invalidate state
next() may throw NoSuchElementException loop on all elements, apply a function Type Inference Functions "Classes are really the only modeling tool Java provides you. So whenever a new idea occurs to you, you have to sculpt it or wrap it or smash at it until it becomes a thing, even if it began life as an action, a process, or any other non-"thing" concept." Steve Yegge
Execution in the Kingdom of Nouns Inversion of Control The compiler works for us Beyond syntactic sugar Implicit Arguments Additional Functionality Go parallel "Java@http://www.tikalk.com/java,.Net@http://www.tikalk.com/net" "well, as a novice, i did get confused by the examples, and could not explain to myself why they behave differently." Ittay Dror, 04/04/06 Is it worth learning Hibernate, Spring, JEE, JSF, ...? Is it worth it? What we saw Case classes
Named arguments, default values
Collections / Functions
Objects Ittay Dror
firstname.lastname@example.org Iterator foreach This presentation shows several examples
from a toy application on Android and how they
could be written differently in Scala
It also explains a bit about Android The application: The Select desired groups: Main screen (selected groups) Clicking a group opens articles list Two entities: Java A lot of boilerplate
Adding/removing a field requires updating all these methods Compiler generates boilerplate code Case Classes types follow names
'extends' not 'implements' Many times we wish to clone such values and modify
an attribute: Or: Tedious... Compiler generated: Usage: (what would we do if url was String?) Named Arguments Accessor method:
Parenthesis optional for no arg methods
No get/set prefix by convention Implicit Conversions:
mechanism for instructing compiler how to convert an object (String) without a method (capitalize) to something with (StringOps, which is in SDK) Default argument values URL is an "extractor"
2 lines of code Code for doing the matching is generated by the compiler
We can create custom ones... constructor arguments follow class name
if accessed by a method body, compiler will
generate a field 'val' means final field and getter
'var' means field and getter+setter
no type definition...(see next) Good also for refactoring.
After changing return type of method, just recompile the clients. No need to change their code The string of selected groups as stored in SharedPreferences: last expression is the result.
compiler infers result type of function to be Group Annonymous function
compiler infers argument type to be String since map was called on Array[String] Arrays are like any other collection (implicit conversion) parenthesis for accessing elements
Array[A] are functions Int => A Look ma, no loops! `par` converts to a parallel array class whose `map` implementation uses fork-join to do the job. So far we've seen how functions can make the client's code cleaner and reusable.
Lets look how functions help authors of classes no risk of a resource leak
functions allow us to easily code better Code Runtime Decorator pattern (AOP in code) `abstract override` means the compiler will make sure a class that uses CachingParser also extends a trait / class with a concrete implementation for fetch concrete implementation implementation is messy and prone to runtime errors
not clear to a client that using the empty constructor means the elements must implement Comparable Lets see how to use implicit arguments to fix these errors
First, we'll explain what it means Scala REPL: a scala shell. Code is immediately compiled and executed Methods can have several argument lists An implicit argument list Scala comes with predefined implementations for basic types. In particular, Ordering[Int] Ordering is like Java's Comparator One implementation of `sort` can be used with an explicit Ordering value or without Ordering is always used to compare
Implementation cleaner than Java,
no runtime errors for casting, no confusion for the user secondary constructor How is the implicit value of type Ordering[RssItem] found? An `object` is a singleton value (sort of like a global variable) for an anonymous class
Used as an alternative to Java's querky static methods (methods that are not invoked on objects)
If an object has the same name as a class it is the class' 'companion'.
The compiler looks for implicit values involving a class T in the companion object of T
So 'new TreeSet[RssItem]' is really: compiler generates code so `new` is not required (we can do that for regular classes) Note that we defined that RssItem has an Ordering without using inheritance
Implicit arguments provide an alternative to inheritance. Less code -> faster to write, less bugs
We saw we also got more robust code Scala is complex, but that is not the question All of these frameworks are more complex than the "simple" alternative. Yet we learn them because using them makes us more productive and our code becomes better If 'split' is changed to return List[String], the rest of the code stays the same