Do More with Less:          Building Android Apps with Scala          Tony Hillerson • Tack Mobile          AnDevCon III  ...
ABOUT METony HillersonSoftware Architect & Partner   Mobile design and development                               company b...
Why Would I Consider Scala?    •   Write less boilerplate code    •   More powerful features than Java    •   Move towards...
N.B. I Am Not a Scala Expert    •   You can probably stump me    •   This talk is about how you can explore what        Sc...
My Goals    •   Introduce you to Scala    •   Introduce you to Scala with Android    •   Give you a few good reasons to us...
Be a PolyglotPRESENTATION                   tackmobile.com
“              Learn one new               language per year               - The Pragmatic ProgrammersPRESENTATION        ...
SevenLanguages inSeven Weekshttp://pragprog.com/book/btlang/seven-languages-in-        seven-weeksPRESENTATION            ...
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     ...
scala-lang.orgPRESENTATION      tackmobile.com
REPL    •   Read Execute Print Loop    •   Command line: scala    •   Test code quicklyPRESENTATION                      t...
Strongly, Statically Typed    •   Compile time type checking    •   Restrictions on how types can be used        together ...
≠ Boilerplate •   Perens are (sometimes) optional •   Semicolons are (mostly) optional •   Types are inferred, thus:     A...
More OO than Java    •   Everything is an object    •   Operators are actually methods    •   For instance, override the “...
Functional Language    •   Functions are first class citizens    •   High order functions: Functions that        operate o...
A Javascript Functionvar doSomething = function(data) {  doSomethingElseWithData(data);}PRESENTATION                     t...
A Scala Functiondef doSomething(data:A):B = {  doSomethingElseWithData(data)}PRESENTATION                      tackmobile....
Concurrent    •   Optional immutable state    •   Actors - Higher concept than Threads/        Thread PoolsPRESENTATION   ...
Example Conversion               ProjectPRESENTATION                   ANDEVCON • MAY 14, 2012
Slider PuzzlePRESENTATION     tackmobile.com
Experiment: Convert from Java to Scala    •   Android         • https://github.com/thillerson/Android-Slider-Puzzle    •  ...
Mike Burns    •   Thoughtbot    •   Android and Scala at Android Open    •   http://robots.thoughtbot.com/post/        583...
Scala with AndroidPRESENTATION                        ANDEVCON • MAY 14, 2012
SBT    •   Build tool (use for Java too)    •   https://github.com/harrah/xsbt/wiki/        Getting-Started-WelcomePRESENT...
android-plugin    •   https://github.com/jberkel/android-plugin/        wiki/getting-started    •   ADB/DDMS Integration  ...
But I Need My Eclipse!    •   http://www.assembla.com/wiki/show/scala-        ide/Developing_for_AndroidPRESENTATION      ...
Scala’s Features               and IdiomsPRESENTATION                      ANDEVCON • MAY 14, 2012
vals and vars    •   var - A variable definition. Required to be        initialized when in a function    •   val - A valu...
Methods    •   Declaration conventions    •   Suffix notation    •   Infix notationPRESENTATION                      tackm...
Looping    •   for ( item <- items )    •   rangesPRESENTATION                    tackmobile.com
Scala Collections    •   Scala really shines on collections    •   A good introduction to functional powerPRESENTATION    ...
Anonymous Function Syntax    •   ( args ) => ReturnType    •   ( x:Int ) => x * xPRESENTATION                     tackmobi...
Inheritance in Java    •   Single Inheritance    •   Restricting!PRESENTATION                 tackmobile.com
Inheritance in C++    •   Multiple Inheritance    •   Mind BlownPRESENTATION                   tackmobile.com
Traits    •   Mixin pattern    •   Abstract/Interface + implementationPRESENTATION                                  tackmo...
Extending the Language           class UnlessClass[T](block: => T) {             def unless(b: Boolean): Unit = {         ...
ResourcesPRESENTATION               ANDEVCON • MAY 14, 2012
Books on Scala    •   http://www.scala-lang.org/node/959PRESENTATION                                 tackmobile.com
Positronic    •   Framework for Android in Scala    •   Makes use of Actors    •   http://rst.github.com/tutorial.htmlPRES...
Assembla    •   http://www.assembla.com/wiki/show/scala-        ide/Developing_for_AndroidPRESENTATION                    ...
SevenLanguages inSeven Weekshttp://pragprog.com/book/btlang/seven-languages-in-        seven-weeksPRESENTATION            ...
Recap    •   Scala is perfect for Android    •   Consider expanding your mind    •   Be a PolyglotPRESENTATION            ...
Thank You          @thillerson @tackmobilePRESENTATION                        ANDEVCON • MAY 14, 2012
Upcoming SlideShare
Loading in …5
×

Scala for android

2,521 views

Published on

Published in: Technology

Scala for android

  1. 1. Do More with Less: Building Android Apps with Scala Tony Hillerson • Tack Mobile AnDevCon III @thillerson @tackmobilePRESENTATION ANDEVCON • MAY 14, 2012
  2. 2. ABOUT METony HillersonSoftware Architect & Partner Mobile design and development company based in Denver, CO.• O’Reilly Screencasts• Computer Nerd @tackmobile• Co-creator of FieldTest tackmobile.com@thillersontony@tackmobile.comPRESENTATION tackmobile.com
  3. 3. Why Would I Consider Scala? • Write less boilerplate code • More powerful features than Java • Move towards Functional paradigmsPRESENTATION tackmobile.com
  4. 4. 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 youPRESENTATION tackmobile.com
  5. 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 PolyglotPRESENTATION tackmobile.com
  6. 6. Be a PolyglotPRESENTATION tackmobile.com
  7. 7. “ Learn one new language per year - The Pragmatic ProgrammersPRESENTATION tackmobile.com
  8. 8. SevenLanguages inSeven Weekshttp://pragprog.com/book/btlang/seven-languages-in- seven-weeksPRESENTATION tackmobile.com
  9. 9. What is Scala?PRESENTATION ANDEVCON • MAY 14, 2012
  10. 10. What is Scala? • JVM Language • Take advantage of any Java library • Bridges the divide between OO and Functional spacesPRESENTATION tackmobile.com
  11. 11. scala-lang.orgPRESENTATION tackmobile.com
  12. 12. REPL • Read Execute Print Loop • Command line: scala • Test code quicklyPRESENTATION tackmobile.com
  13. 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, DynamicPRESENTATION tackmobile.com
  14. 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. 15. More OO than Java • Everything is an object • Operators are actually methods • For instance, override the “+” methodPRESENTATION tackmobile.com
  16. 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 FunctionalPRESENTATION tackmobile.com
  17. 17. A Javascript Functionvar doSomething = function(data) { doSomethingElseWithData(data);}PRESENTATION tackmobile.com
  18. 18. A Scala Functiondef doSomething(data:A):B = { doSomethingElseWithData(data)}PRESENTATION tackmobile.com
  19. 19. Concurrent • Optional immutable state • Actors - Higher concept than Threads/ Thread PoolsPRESENTATION tackmobile.com
  20. 20. Example Conversion ProjectPRESENTATION ANDEVCON • MAY 14, 2012
  21. 21. Slider PuzzlePRESENTATION tackmobile.com
  22. 22. 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 ScalaPRESENTATION tackmobile.com
  23. 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/ ohlaunchPRESENTATION tackmobile.com
  24. 24. Scala with AndroidPRESENTATION ANDEVCON • MAY 14, 2012
  25. 25. SBT • Build tool (use for Java too) • https://github.com/harrah/xsbt/wiki/ Getting-Started-WelcomePRESENTATION tackmobile.com
  26. 26. android-plugin • https://github.com/jberkel/android-plugin/ wiki/getting-started • ADB/DDMS Integration • Build and ReleasePRESENTATION tackmobile.com
  27. 27. But I Need My Eclipse! • http://www.assembla.com/wiki/show/scala- ide/Developing_for_AndroidPRESENTATION tackmobile.com
  28. 28. Scala’s Features and IdiomsPRESENTATION ANDEVCON • MAY 14, 2012
  29. 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 programmingPRESENTATION tackmobile.com
  30. 30. Methods • Declaration conventions • Suffix notation • Infix notationPRESENTATION tackmobile.com
  31. 31. Looping • for ( item <- items ) • rangesPRESENTATION tackmobile.com
  32. 32. Scala Collections • Scala really shines on collections • A good introduction to functional powerPRESENTATION tackmobile.com
  33. 33. Anonymous Function Syntax • ( args ) => ReturnType • ( x:Int ) => x * xPRESENTATION tackmobile.com
  34. 34. Inheritance in Java • Single Inheritance • Restricting!PRESENTATION tackmobile.com
  35. 35. Inheritance in C++ • Multiple Inheritance • Mind BlownPRESENTATION tackmobile.com
  36. 36. Traits • Mixin pattern • Abstract/Interface + implementationPRESENTATION tackmobile.com
  37. 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.htmlPRESENTATION tackmobile.com
  38. 38. ResourcesPRESENTATION ANDEVCON • MAY 14, 2012
  39. 39. Books on Scala • http://www.scala-lang.org/node/959PRESENTATION tackmobile.com
  40. 40. Positronic • Framework for Android in Scala • Makes use of Actors • http://rst.github.com/tutorial.htmlPRESENTATION tackmobile.com
  41. 41. Assembla • http://www.assembla.com/wiki/show/scala- ide/Developing_for_AndroidPRESENTATION tackmobile.com
  42. 42. SevenLanguages inSeven Weekshttp://pragprog.com/book/btlang/seven-languages-in- seven-weeksPRESENTATION tackmobile.com
  43. 43. Recap • Scala is perfect for Android • Consider expanding your mind • Be a PolyglotPRESENTATION tackmobile.com
  44. 44. Thank You @thillerson @tackmobilePRESENTATION ANDEVCON • MAY 14, 2012

×