Android development with Scala

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    Android development with Scala - Presentation Transcript

    1. Android development with Scala @jberkel
    2. Scala? • a better, more modern Java • based on the JVM, Java (binary) compatible • multi-paradigm: functional, oop • soon mainstream? twitter, ebay...
    3. better? • purer OO (no primitives) • less boilerplate ([gs]etter) • less code (type inference) • no checked exceptions • no 1:1 file - class mapping
    4. type inference
    5. Map<String,String> m = new HashMap<String,String>(); m.put("foo", "bar");
    6. val m = Map("foo"- >"bar")
    7. sound type checking which doesn’t get in your way
    8. traits = fat interfaces
    9. 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) } } }
    10. 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
    11. class MyService extends android.app.Service with LogHelper { override def onCreate() { withExceptionHandling { log("about to run") doDangerousStuff(true) } } }
    12. 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”
    13. traits are “stackable”
    14. class ServiceStack extends Service with DbSupport with LogHelper { // }
    15. functional programming style
    16. use functions instead of one-method interfaces !
    17. locationManager.addGpsStatusListener( new GpsStatus.Listener { public void onGpsStatusChanged(int event) { System.out.println(event); } }) vs. locationManager.addGpsStatusListener(println(_))
    18. pimp my library cursors
    19. 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.
    20. query("select * from foo") { cursor => cursor.map { row => val v = new ContentValues DatabaseUtils.cursorRowToContentValues(row, v) v } }
    21. XML handling
    22. validate xml at compile time! 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
    23. Problems • incomplete “convenience” layer • resulting APKs are big, use proguard • not many tools / experienced devs • fewer libraries, but growing
    24. Programming in Scala
    25. Scala for Java Refugees, D. Spiewak • http://www.codecommit.com/ blog/scala/roundup-scala-for-java- refugees
    26. Community • scala-lang.org, #scala • very responsive & helpful • growing the language is a community effort
    27. • eclipse: http://www.novoda.com/ blog/?p=154 • simple-build-tools (sbt) http:// github.com/harrah/android-plugin • scala-android: http:// code.google.com/p/scala-android/

    + Jan BerkelJan Berkel, 2 months ago

    custom

    853 views, 2 favs, 2 embeds more stats

    slides from talk given at droidcamp Berlin, 2009

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 853
      • 841 on SlideShare
      • 12 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 13
    Most viewed embeds
    • 11 views on http://irohacoding.com
    • 1 views on http://translate.googleusercontent.com

    more

    All embeds
    • 11 views on http://irohacoding.com
    • 1 views on http://translate.googleusercontent.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories