Scala for android

44
PRESENTATION ANDEVCON • MAY 14, 2012 Do More with Less: Building Android Apps with Scala Tony Hillerson • Tack Mobile AnDevCon III @thillerson @tackmobile

Transcript of Scala for android

Page 1: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Do More with Less:Building Android Apps with ScalaTony Hillerson • Tack Mobile

AnDevCon III

@thillerson @tackmobile

Page 2: Scala for android

PRESENTATION tackmobile.com

ABOUT ME

Tony HillersonSoftware Architect & Partner

• O’Reilly Screencasts• Computer Nerd• Co-creator of FieldTest

@[email protected]

Mobile design and development company based in Denver, CO.

@tackmobiletackmobile.com

Page 3: Scala for android

PRESENTATION tackmobile.com

Why Would I Consider Scala?

• Write less boilerplate code• More powerful features than Java• Move towards Functional paradigms

Page 4: Scala for android

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

Page 5: Scala for android

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

Page 6: Scala for android

PRESENTATION tackmobile.com

Be a Polyglot

Page 7: Scala for android

PRESENTATION tackmobile.com

“- The Pragmatic Programmers

Learn one new language per year

Page 9: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

What is Scala?

Page 10: Scala for android

PRESENTATION tackmobile.com

What is Scala?

• JVM Language• Take advantage of any Java library• Bridges the divide between OO and

Functional spaces

Page 11: Scala for android

PRESENTATION tackmobile.com

scala-lang.org

Page 12: Scala for android

PRESENTATION tackmobile.com

REPL

• Read Execute Print Loop• Command line: scala• Test code quickly

Page 13: Scala for android

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

Page 14: Scala for android

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]

Page 15: Scala for android

PRESENTATION tackmobile.com

More OO than Java

• Everything is an object• Operators are actually methods• For instance, override the “+” method

Page 16: Scala for android

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

Page 17: Scala for android

PRESENTATION tackmobile.com

A Javascript Function

var doSomething = function(data) { doSomethingElseWithData(data);}

Page 18: Scala for android

PRESENTATION tackmobile.com

A Scala Function

def doSomething(data:A):B = { doSomethingElseWithData(data)}

Page 19: Scala for android

PRESENTATION tackmobile.com

Concurrent

• Optional immutable state• Actors - Higher concept than Threads/

Thread Pools

Page 20: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Example Conversion Project

Page 21: Scala for android

PRESENTATION tackmobile.com

Slider Puzzle

Page 22: Scala for android

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

Page 24: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Scala with Android

Page 26: Scala for android

PRESENTATION tackmobile.com

android-plugin

• https://github.com/jberkel/android-plugin/wiki/getting-started

• ADB/DDMS Integration• Build and Release

Page 28: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Scala’s Featuresand Idioms

Page 29: Scala for android

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

Page 30: Scala for android

PRESENTATION tackmobile.com

Methods

• Declaration conventions• Suffix notation• Infix notation

Page 31: Scala for android

PRESENTATION tackmobile.com

Looping

• for ( item <- items )• ranges

Page 32: Scala for android

PRESENTATION tackmobile.com

Scala Collections

• Scala really shines on collections• A good introduction to functional power

Page 33: Scala for android

PRESENTATION tackmobile.com

Anonymous Function Syntax

• ( args ) => ReturnType• ( x:Int ) => x * x

Page 34: Scala for android

PRESENTATION tackmobile.com

Inheritance in Java

• Single Inheritance• Restricting!

Page 35: Scala for android

PRESENTATION tackmobile.com

Inheritance in C++

• Multiple Inheritance• Mind Blown

Page 36: Scala for android

PRESENTATION tackmobile.com

Traits

• Mixin pattern• Abstract/Interface + implementation

Page 37: Scala for android

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

Page 38: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Resources

Page 39: Scala for android

PRESENTATION tackmobile.com

Books on Scala

• http://www.scala-lang.org/node/959

Page 40: Scala for android

PRESENTATION tackmobile.com

Positronic

• Framework for Android in Scala• Makes use of Actors• http://rst.github.com/tutorial.html

Page 43: Scala for android

PRESENTATION tackmobile.com

Recap

• Scala is perfect for Android• Consider expanding your mind• Be a Polyglot

Page 44: Scala for android

PRESENTATION ANDEVCON • MAY 14, 2012

Thank You@thillerson @tackmobile