27
Android development with Scala @jberkel

Android development with Scala

Embed Size (px)

Citation preview

Page 1: Android development with Scala

Android development with

Scala

@jberkel

Page 2: Android development with Scala

Scala?

• a better, more modern Java

• based on the JVM, Java (binary) compatible

• multi-paradigm: functional, oop

• soon mainstream? twitter, ebay...

Page 3: Android development with Scala

better?

• purer OO (no primitives)

• less boilerplate ([gs]etter)

• less code (type inference)

• no checked exceptions

• no 1:1 file - class mapping

Page 4: Android development with Scala

type inference

Page 5: Android development with Scala

Map<String,String> m = new HashMap<String,String>();m.put("foo", "bar");

Page 6: Android development with Scala

val m = Map("foo"->"bar")

Page 7: Android development with Scala

sound type checking which doesn’t get in your way

Page 8: Android development with Scala

traits = fat interfaces

Page 9: Android development with Scala

import android.util.Log

trait LogHelper { def log(s: String) = Log.d(getClass.getSimpleName, s) def logException(e: Throwable) = Log.e(getClass.getSimpleName, "error", e) def withExceptionHandling(what: => Unit) { try { what } catch { case ex: Exception => logException(ex) } }}

Page 10: Android development with Scala

import android.util.Log

trait LogHelper { def log(s: String) = Log.d(getClass.getSimpleName, s) def logException(e: Throwable) = Log.e(getClass.getSimpleName, "error", e) def withExceptionHandling(what: => Unit) { try { what } catch { case ex: Exception => logException(ex) } }}

what: passed in as function

Page 11: Android development with Scala

class MyService extends android.app.Service with LogHelper {

override def onCreate() { withExceptionHandling { log("about to run") doDangerousStuff(true) } }}

Page 12: Android development with Scala

trait DbSupport { def getDb: android.database.sqlite.SQLiteDatabase

def query[T](sql: String, args: String*)(fun: Cursor=>T) = { val cursor = getDb.rawQuery(sql, args.toArray[String]) try {

fun(cursor) } finally {

cursor.close } }}

“loan pattern”

Page 13: Android development with Scala

traits are “stackable”

Page 14: Android development with Scala

class ServiceStack extends Service with DbSupport with LogHelper { // }

Page 15: Android development with Scala

functional programming style

Page 16: Android development with Scala

use functions instead of one-methodinterfaces !

Page 17: Android development with Scala

locationManager.addGpsStatusListener( new GpsStatus.Listener { public void onGpsStatusChanged(int event) { System.out.println(event); }})

locationManager.addGpsStatusListener(println(_))

vs.

Page 18: Android development with Scala

pimp my library

cursors

Page 19: Android development with Scala

implicit def RichCursor(c: Cursor):RichCursor = new RichCursor(c) class RichCursor(underlying: Cursor) extends Iterable[Cursor] { override def elements = new Iterator[Cursor] { def hasNext = !underlying.isLast def next = { underlying.moveToNext() underlying } } } the trait Iterable

provides find, map, filter etc.

Page 20: Android development with Scala

query("select * from foo") { cursor =>cursor.map { row => val v = new ContentValues DatabaseUtils.cursorRowToContentValues(row, v) v}

}

Page 21: Android development with Scala

XML handling

Page 22: Android development with Scala

val event = <event> <artists> <artist>Screaming Tea Party</artist> <artist>Ungdomskulen</artist> <artist>Invasion</artist> </artists></event>

val artists = (event \ "artist" \ "artists").map(_.text).mkString(", ")

inline XML with xpath style query operator

validate xml at compile time!

Page 23: Android development with Scala

Problems• incomplete “convenience” layer

• resulting APKs are big, use proguard

• not many tools / experienced devs

• fewer libraries, but growing

Page 24: Android development with Scala

Programming in Scala

Page 26: Android development with Scala

Community

• scala-lang.org, #scala

• very responsive & helpful

• growing the language is a community effort