Scala on-android

Download Scala on-android

Post on 28-Jan-2015




0 download

Embed Size (px)


Scala on Android: My Experience


<ul><li> 1. Scala on Android</li></ul> <p> 2. Android Development Write lots of Java code Write UI and other resources in XML Generate Java code from resources Compile Java code into .class files Run proguard Compile class files to Dalvik Executable (DEX) Deploy on device 3. Dalvik VM Does not support .class bytecode format Can not use libraries which generate bytecode Devices have limited CPU and Memory Can not easily share libraries betweenapplications 4. Eclipse Has nice UI editors IDE is a build tool 5. Alternative Languages for Android 6. Jython Does not have enough community support Slow Dynamic 7. JRuby Ruboto framework Dynamic Need to use java code generation 8. Clojure Very cool :) Very slow start-up time Sometimes need to generate bytecode 9. PHPWho cares? 10. Rhino Nice language Can run with optimization turned off Completely interpreted Dynamic 11. Groovy ??? 12. Scala Statically typed Compiled to Java bytecode Has access to all classes in Android Fast but sometimes slow :) Has SBT Android Maven artifacts 13. How to build Scala apps for Android ANT Maven SBT + Android Plugin 14. Little nice things about Scala 15. Type Inference Improve readability Reduce number of imports Less typing 16. val stream = getAssets().open("areas.json")val stream: InputStream =getAssets().open("areas.json") 17. Import statementimport android.view.{Window, Menu, MenuItem, KeyEvent, View}import android.util.Log.{e =&gt; error} 18. Lazy valslazy val searchBox =findViewById([AutoCompleteTextView] 19. Mixin Inheritanceclass MainActivity extends MapActivity with Logging with DatabaseHelpers with MapHelpers 20. Collections Best collection library I ever used Sorry google-collections... Using arrays is easy @specialized 21. Collection examplesval suggestions = areas map { }val index = areas.indexWhere( == searchString)val index =areas.indexWhere(_.contains(coordinate))pointList.minBy( 22. First class functionsclass AreaOverlay(areas: Array[Area],selectListener: Int =&gt; Unit)new AreaOverlay(areas, this.onAreaSelected) 23. Convert listeners into functionsimplicit def fn2ClickListener(f: View =&gt; Unit): View.OnClickListener =new View.OnClickListener() { override def onClick(v: View) = f(view)}exitButton.setOnClickListener( this.onExitClick) 24. Files Directory structure is not forced to mirrorpackages Files can contain any amount of classes 25. Pattern matching No NPEs when using Option[T] Easy to work with data objects 26. Pattern matchinglastLocation match {case Some(c@Coordinate(lat, lon)) =&gt; None =&gt;...} 27. Implicitsimplicit def coordinate2geopoint(c: Coordinate): GeoPoint = new GeoPoint( * 1.0e6 toInt, c.lon * 1.0e6 toInt) 28. XMLfor (s</p>