Do More with Less:
          Building Android Apps with Scala
          Tony Hillerson • Tack Mobile
          AnDevCon III
          @thillerson @tackmobile




PRESENTATION                             ANDEVCON • MAY 14, 2012
ABOUT ME




Tony Hillerson
Software Architect & Partner   Mobile design and development
                               company based in Denver, CO.
•   O’Reilly Screencasts
•   Computer Nerd              @tackmobile
•   Co-creator of FieldTest    tackmobile.com


@thillerson
tony@tackmobile.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
“              Learn one new
               language per year
               - The Pragmatic Programmers



PRESENTATION                                 tackmobile.com
Seven
Languages in
Seven Weeks
http://pragprog.com/book/
btlang/seven-languages-in-
        seven-weeks




PRESENTATION                 tackmobile.com
What is Scala?



PRESENTATION                    ANDEVCON • MAY 14, 2012
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                                    tackmobile.com
Example Conversion
               Project



PRESENTATION                   ANDEVCON • MAY 14, 2012
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                                    tackmobile.com
Scala with Android



PRESENTATION                        ANDEVCON • MAY 14, 2012
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                                  tackmobile.com
Scala’s Features
               and Idioms



PRESENTATION                      ANDEVCON • MAY 14, 2012
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
           class UnlessClass[T](block: => T) {
             def unless(b: Boolean): Unit = {
               if (!b) block
             }
           }

           implicit def
           toUnlessClass[T](block: => T): UnlessClass[T] = {
             new UnlessClass[T](block)
           }



                                             ...
                           foo unless flag

               http://naturalsoftware.blogspot.com/2009/05/ruby-unless-scala.html
PRESENTATION                                                                        tackmobile.com
Resources



PRESENTATION               ANDEVCON • MAY 14, 2012
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 Weeks
http://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                           tackmobile.com
Thank You
          @thillerson @tackmobile




PRESENTATION                        ANDEVCON • MAY 14, 2012

Scala for android

  • 1.
    Do More withLess: Building Android Apps with Scala Tony Hillerson • Tack Mobile AnDevCon III @thillerson @tackmobile PRESENTATION ANDEVCON • MAY 14, 2012
  • 2.
    ABOUT ME Tony Hillerson SoftwareArchitect & Partner Mobile design and development company based in Denver, CO. • O’Reilly Screencasts • Computer Nerd @tackmobile • Co-creator of FieldTest tackmobile.com @thillerson tony@tackmobile.com PRESENTATION tackmobile.com
  • 3.
    Why Would IConsider Scala? • Write less boilerplate code • More powerful features than Java • Move towards Functional paradigms PRESENTATION tackmobile.com
  • 4.
    N.B. I AmNot 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
  • 5.
    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
  • 6.
  • 7.
    Learn one new language per year - The Pragmatic Programmers PRESENTATION tackmobile.com
  • 8.
  • 9.
    What is Scala? PRESENTATION ANDEVCON • MAY 14, 2012
  • 10.
    What is Scala? • JVM Language • Take advantage of any Java library • Bridges the divide between OO and Functional spaces PRESENTATION tackmobile.com
  • 11.
  • 12.
    REPL • Read Execute Print Loop • Command line: scala • Test code quickly PRESENTATION tackmobile.com
  • 13.
    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
  • 14.
    ≠ 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
  • 15.
    More OO thanJava • Everything is an object • Operators are actually methods • For instance, override the “+” method PRESENTATION tackmobile.com
  • 16.
    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
  • 17.
    A Javascript Function vardoSomething = function(data) { doSomethingElseWithData(data); } PRESENTATION tackmobile.com
  • 18.
    A Scala Function defdoSomething(data:A):B = { doSomethingElseWithData(data) } PRESENTATION tackmobile.com
  • 19.
    Concurrent • Optional immutable state • Actors - Higher concept than Threads/ Thread Pools PRESENTATION tackmobile.com
  • 20.
    Example Conversion Project PRESENTATION ANDEVCON • MAY 14, 2012
  • 21.
  • 22.
    Experiment: Convert fromJava 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
  • 23.
    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 tackmobile.com
  • 24.
    Scala with Android PRESENTATION ANDEVCON • MAY 14, 2012
  • 25.
    SBT • Build tool (use for Java too) • https://github.com/harrah/xsbt/wiki/ Getting-Started-Welcome PRESENTATION tackmobile.com
  • 26.
    android-plugin • https://github.com/jberkel/android-plugin/ wiki/getting-started • ADB/DDMS Integration • Build and Release PRESENTATION tackmobile.com
  • 27.
    But I NeedMy Eclipse! • http://www.assembla.com/wiki/show/scala- ide/Developing_for_Android PRESENTATION tackmobile.com
  • 28.
    Scala’s Features and Idioms PRESENTATION ANDEVCON • MAY 14, 2012
  • 29.
    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
  • 30.
    Methods • Declaration conventions • Suffix notation • Infix notation PRESENTATION tackmobile.com
  • 31.
    Looping • for ( item <- items ) • ranges PRESENTATION tackmobile.com
  • 32.
    Scala Collections • Scala really shines on collections • A good introduction to functional power PRESENTATION tackmobile.com
  • 33.
    Anonymous Function Syntax • ( args ) => ReturnType • ( x:Int ) => x * x PRESENTATION tackmobile.com
  • 34.
    Inheritance in Java • Single Inheritance • Restricting! PRESENTATION tackmobile.com
  • 35.
    Inheritance in C++ • Multiple Inheritance • Mind Blown PRESENTATION tackmobile.com
  • 36.
    Traits • Mixin pattern • Abstract/Interface + implementation PRESENTATION tackmobile.com
  • 37.
    Extending the Language class UnlessClass[T](block: => T) { def unless(b: Boolean): Unit = { if (!b) block } } implicit def toUnlessClass[T](block: => T): UnlessClass[T] = { new UnlessClass[T](block) } ... foo unless flag http://naturalsoftware.blogspot.com/2009/05/ruby-unless-scala.html PRESENTATION tackmobile.com
  • 38.
    Resources PRESENTATION ANDEVCON • MAY 14, 2012
  • 39.
    Books on Scala • http://www.scala-lang.org/node/959 PRESENTATION tackmobile.com
  • 40.
    Positronic • Framework for Android in Scala • Makes use of Actors • http://rst.github.com/tutorial.html PRESENTATION tackmobile.com
  • 41.
    Assembla • http://www.assembla.com/wiki/show/scala- ide/Developing_for_Android PRESENTATION tackmobile.com
  • 42.
  • 43.
    Recap • Scala is perfect for Android • Consider expanding your mind • Be a Polyglot PRESENTATION tackmobile.com
  • 44.
    Thank You @thillerson @tackmobile PRESENTATION ANDEVCON • MAY 14, 2012