Post on 12-May-2015
transcript
PRESENTATION ANDEVCON • MAY 14, 2012
Do More with Less:Building Android Apps with ScalaTony Hillerson • Tack Mobile
AnDevCon III
@thillerson @tackmobile
PRESENTATION tackmobile.com
ABOUT ME
Tony HillersonSoftware Architect & Partner
• O’Reilly Screencasts• Computer Nerd• Co-creator of FieldTest
@thillersontony@tackmobile.com
Mobile design and development company based in Denver, CO.
@tackmobiletackmobile.com
PRESENTATION tackmobile.com
Why Would I Consider Scala?
• Write less boilerplate code• More powerful features than Java• Move towards Functional paradigms
PRESENTATION tackmobile.com
N.B. I Am Not a Scala Expert
• You can probably stump me• This talk is about how you can explore what
Scala might do for you
PRESENTATION tackmobile.com
My Goals
• Introduce you to Scala• Introduce you to Scala with Android• Give you a few good reasons to use Scala• Encourage you to become a Polyglot
PRESENTATION tackmobile.com
Be a Polyglot
PRESENTATION tackmobile.com
“- The Pragmatic Programmers
Learn one new language per year
PRESENTATION tackmobile.com
Seven Languages in Seven Weekshttp://pragprog.com/book/btlang/seven-languages-in-
seven-weeks
PRESENTATION ANDEVCON • MAY 14, 2012
What is Scala?
PRESENTATION tackmobile.com
What is Scala?
• JVM Language• Take advantage of any Java library• Bridges the divide between OO and
Functional spaces
PRESENTATION tackmobile.com
scala-lang.org
PRESENTATION tackmobile.com
REPL
• Read Execute Print Loop• Command line: scala• Test code quickly
PRESENTATION tackmobile.com
Strongly, Statically Typed
• Compile time type checking• Restrictions on how types can be used
together• For comparison:
• Javascript - Weakly typed, Dynamic• Ruby - Strongly typed, Dynamic
PRESENTATION tackmobile.com
≠ Boilerplate
• Perens are (sometimes) optional• Semicolons are (mostly) optional• Types are inferred, thus:
ArrayList<GameTileMotionDescriptor> descriptors = new ArrayList<GameTileMotionDescriptor>();
VS.val descriptors = new ArrayBuffer[GameTileMotionDescriptor]
PRESENTATION tackmobile.com
More OO than Java
• Everything is an object• Operators are actually methods• For instance, override the “+” method
PRESENTATION tackmobile.com
Functional Language
• Functions are first class citizens• High order functions: Functions that
operate on functions• Avoids mutable state• Could get pretty academic• Scala bridges the gap between OO and
Functional
PRESENTATION tackmobile.com
A Javascript Function
var doSomething = function(data) { doSomethingElseWithData(data);}
PRESENTATION tackmobile.com
A Scala Function
def doSomething(data:A):B = { doSomethingElseWithData(data)}
PRESENTATION tackmobile.com
Concurrent
• Optional immutable state• Actors - Higher concept than Threads/
Thread Pools
PRESENTATION ANDEVCON • MAY 14, 2012
Example Conversion Project
PRESENTATION tackmobile.com
Slider Puzzle
PRESENTATION tackmobile.com
Experiment: Convert from Java to Scala
• Android• https://github.com/thillerson/Android-Slider-Puzzle
• Scala• https://github.com/thillerson/scala-android-slider-puzzle
• Mixed results.
• I have a lot to learn about Scala
PRESENTATION tackmobile.com
Mike Burns
• Thoughtbot• Android and Scala at Android Open• http://robots.thoughtbot.com/post/
5836463058/scala-a-better-java-for-android• Ohlaunch - https://github.com/mike-burns/
ohlaunch
PRESENTATION ANDEVCON • MAY 14, 2012
Scala with Android
PRESENTATION tackmobile.com
SBT
• Build tool (use for Java too)• https://github.com/harrah/xsbt/wiki/
Getting-Started-Welcome
PRESENTATION tackmobile.com
android-plugin
• https://github.com/jberkel/android-plugin/wiki/getting-started
• ADB/DDMS Integration• Build and Release
PRESENTATION tackmobile.com
But I Need My Eclipse!
• http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android
PRESENTATION ANDEVCON • MAY 14, 2012
Scala’s Featuresand Idioms
PRESENTATION tackmobile.com
vals and vars
• var - A variable definition. Required to be initialized when in a function
• val - A value definition. Once set, cannot be changed• Basically a “final”
• Important consideration for concurrent programming
PRESENTATION tackmobile.com
Methods
• Declaration conventions• Suffix notation• Infix notation
PRESENTATION tackmobile.com
Looping
• for ( item <- items )• ranges
PRESENTATION tackmobile.com
Scala Collections
• Scala really shines on collections• A good introduction to functional power
PRESENTATION tackmobile.com
Anonymous Function Syntax
• ( args ) => ReturnType• ( x:Int ) => x * x
PRESENTATION tackmobile.com
Inheritance in Java
• Single Inheritance• Restricting!
PRESENTATION tackmobile.com
Inheritance in C++
• Multiple Inheritance• Mind Blown
PRESENTATION tackmobile.com
Traits
• Mixin pattern• Abstract/Interface + implementation
PRESENTATION tackmobile.com
Extending the Language
http://naturalsoftware.blogspot.com/2009/05/ruby-unless-scala.html
class UnlessClass[T](block: => T) { def unless(b: Boolean): Unit = { if (!b) block }}
implicit deftoUnlessClass[T](block: => T): UnlessClass[T] = { new UnlessClass[T](block)}
...foo unless flag
PRESENTATION ANDEVCON • MAY 14, 2012
Resources
PRESENTATION tackmobile.com
Books on Scala
• http://www.scala-lang.org/node/959
PRESENTATION tackmobile.com
Positronic
• Framework for Android in Scala• Makes use of Actors• http://rst.github.com/tutorial.html
PRESENTATION tackmobile.com
Assembla
• http://www.assembla.com/wiki/show/scala-ide/Developing_for_Android
PRESENTATION tackmobile.com
Seven Languages in Seven Weekshttp://pragprog.com/book/btlang/seven-languages-in-
seven-weeks
PRESENTATION tackmobile.com
Recap
• Scala is perfect for Android• Consider expanding your mind• Be a Polyglot
PRESENTATION ANDEVCON • MAY 14, 2012
Thank You@thillerson @tackmobile