Scaladroids
Ostap Andrusiv
About me
Agenda
What is android? What is scala?
Scala your android?
Why Scala?Why Scala?
Scala
Concise
Type-safe
Functional && OO
RSSReader
~400
~150
LOC
Expressions & Values
pif@pif:~/ $ scala
scala> 20 + 22
res1: Int = 42
scala> val fourtyTwo = 100 - 58
fourtyTwo: Int = 42
...
Functions
pif@pif:~/ $ scala
scala> def add42(a: Int) =
| a + 42
add42: (a: Int)Int
scala> add42(50 + 5*5*2)
res0: Int = 1...
Java Class
public class RSSEntry {
private String name;
private String description;
private String link;
public RSSEntry(S...
1
Scala class
class RSSEntry(name: String, description: String, link: String) {
// that's it! Nothing more!
case class RSSEn...
Basic Inheritance & Traits
class RSSEntry(name: String, desc: String) {}
// that's it! Nothing more!
class AuthoredRSSEntr...
Collections FTW!
Task: From all #ADD tweets,
find those programmers,
who know .NET and give them Coockies!!!
Java: // ~50 ...
Scaladroiding...
* -- S for Scala
Layout
<LinearLayout xmlns:android="http://schemas.android.com/ap...
android:layout_width... >
<TextView android:id="@+id/...
Layout Usage
// ... in Java:
TextView itemName = (TextView) findViewById(R.id.text_name);
TextView itemDesc = (TextView) f...
TypedResource TR.scala
object TR {
val itemName = TypedResource[android.widget.Button](R.id.text_n
// ...
}
// ==> ... in ...
Event Handlers & Listeners
// ... in Java:
Button b = (Button) findViewById(R.id.action_check);
b.setOnClickListener(new V...
Event Handlers & Listeners
// ... in Scala:
val b = findView(TR.buttonCheck)
b setOnClickListener(new View.OnClickListener...
Implicits
// ... in Scala:
class ShortString(s: String) {
def short =
if (s.length > 10)
s.substring(0,7) + “...”
else
s
}...
"Pimp My Library" Ninja!
Implicits
// ... in Scala:
class ClickableView(v: View) {
def onClick(u: Unit) {
v.setOnClickListener(new View.OnClickList...
Implicits
// ... in Java:
Toast.makeText(ctx, "Toast!", Toast.LENGTH_LONG).show();
// ... in Scala:
class ToastableString(...
// ... magic is in Scala:
trait Toasty {
class ToastString(s: String) {
def toast(implicit ctx: Context) =
Toast.makeText(...
Shared Preferences
// ... in Java:
SharedPreferences.Editor sp = ctx.getSharedPreferences(KEY, 0).ed
editor.putString("hel...
Menus
// ... in Java:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.entries, ...
Closable
// ... in Java:
try {
// do some tricky work...
finally {
tricky.close();
}
// ... in Scala:
trait Closers {
def ...
Async Tasks
// ... in Javish Scala:
new AsyncTask[String, Void, Object] {
override def doInBackground(params: String*) : O...
Async Tasks
// ... in Scala:
async {
// download data
}
// same applies to Handlers, post, threads, etc.
Scala Issues on Android
Maps API
wrapper or
v2 API
Solutions:
ProGuard
Magic behind
Magic behind
Let the Scala be with you!
? *Thanks!
Coursera
Twitter Scala School
Twitter Effective Scala
Odersky Book
stackoverflow
Scaladroids: Developing Android Apps with Scala
Upcoming SlideShare
Loading in …5
×

Scaladroids: Developing Android Apps with Scala

1,645 views

Published on

Android is a fast-growing mobile operating system with millions of users worldwide.
Scala is a modern programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Why not combine both of them?

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,645
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Scaladroids: Developing Android Apps with Scala

  1. 1. Scaladroids Ostap Andrusiv
  2. 2. About me
  3. 3. Agenda What is android? What is scala? Scala your android?
  4. 4. Why Scala?Why Scala?
  5. 5. Scala Concise Type-safe Functional && OO
  6. 6. RSSReader ~400 ~150 LOC
  7. 7. Expressions & Values pif@pif:~/ $ scala scala> 20 + 22 res1: Int = 42 scala> val fourtyTwo = 100 - 58 fourtyTwo: Int = 42 scala> fourtyTwo = -1 <console>:8: error: reassignment to val scala> var number = 42 number: Int = 42 scala> number = -1 number: Int = -1
  8. 8. Functions pif@pif:~/ $ scala scala> def add42(a: Int) = | a + 42 add42: (a: Int)Int scala> add42(50 + 5*5*2) res0: Int = 142 scala> def minusOne() = | -1 minusOne: ()Int scala> 43 + minusOne res1: Int = 42
  9. 9. Java Class public class RSSEntry { private String name; private String description; private String link; public RSSEntry(String name, String description, String link) { super(); this.name = name; this.description = description; this.link = link; } public String getName() { return name; } public void setName(String name) { this.name = name; } // ... 50 LINES more !!!
  10. 10. 1
  11. 11. Scala class class RSSEntry(name: String, description: String, link: String) { // that's it! Nothing more! case class RSSEntry(name: String, description: String, link: Stri // class + equals(), hashCode() & lots of other useful stuff object RSSEntry { def customize() = ... }
  12. 12. Basic Inheritance & Traits class RSSEntry(name: String, desc: String) {} // that's it! Nothing more! class AuthoredRSSEntry(name: String, desc: String, author: String extends RSSEntry(name, desc trait HtlmView { def asHTLMString() : String } trait PlainView { def asPlainText() : String } class NiceRSSEntry(name: String, desc: String) extends RSSEntry(name, desc with HtmlView with PlainView { // ... asHTLMString & asPlainText
  13. 13. Collections FTW! Task: From all #ADD tweets, find those programmers, who know .NET and give them Coockies!!! Java: // ~50 lines of mess and pain in the ass... Scala: tweets .filter (_ contains "#ADD13") .flatMap(_ split " " ) .filter (_ startsWith "@" ) .map (t => person(t)) .filter (_ langs contains (".net")) .foreach(giveCookies _) //import scala.collection.JavaConverters._
  14. 14. Scaladroiding... * -- S for Scala
  15. 15. Layout <LinearLayout xmlns:android="http://schemas.android.com/ap... android:layout_width... > <TextView android:id="@+id/text_name" android:textSize... /> <TextView android:id="@+id/text_descr" android:textSize... /> </LinearLayout> // ... in R.java: public final class R { // ... public static final class id { // ... public static final int text_descr=0x7f090003; public static final int text_name=0x7f090002; }
  16. 16. Layout Usage // ... in Java: TextView itemName = (TextView) findViewById(R.id.text_name); TextView itemDesc = (TextView) findViewById(R.id.text_descr); itemName.setText(item.getShortName()); itemDesc.setText(item.getShortDescription()); // ... in Scala: val itemName = findViewById(R.id.text_name).asInstanceOf[TextView val itemDesc = findViewById(R.id.text_descr).asInstanceOf[TextVie itemName.setText(item.getShortName) itemDesc.setText(item.getShortDescription)
  17. 17. TypedResource TR.scala object TR { val itemName = TypedResource[android.widget.Button](R.id.text_n // ... } // ==> ... in Scala: val itemName = findViewById(R.id.text_name).asInstanceOf[TextView val itemDesc = findViewById(R.id.text_descr).asInstanceOf[TextVie val itemName = findView(TR.itemName) val itemDesc = findView(TR.itemDesc)
  18. 18. Event Handlers & Listeners // ... in Java: Button b = (Button) findViewById(R.id.action_check); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ... make user happy :) } }); // ... in Scala: val b = findView(TR.buttonCheck) b setOnClickListener(new View.OnClickListener { override def onClick(v: View) { // ... make user happy :) } }) // hmmm... no benefit from this :( ?
  19. 19. Event Handlers & Listeners // ... in Scala: val b = findView(TR.buttonCheck) b setOnClickListener(new View.OnClickListener { override def onClick(v: View) { // ... make user happy :) } }) // hmmm... no benefit from this :( ? // this is java-like scala! Rewrite it: b onClick { /* make user happy */ } // b has no onClick method!!! Where is it from?
  20. 20. Implicits // ... in Scala: class ShortString(s: String) { def short = if (s.length > 10) s.substring(0,7) + “...” else s } new ShortString(“Very very long greeting”).short // Very ve... implicit def string2Shortstr(s: String) = new ShortString(s) “Very very long greeting”.short // Very ve...
  21. 21. "Pimp My Library" Ninja!
  22. 22. Implicits // ... in Scala: class ClickableView(v: View) { def onClick(u: Unit) { v.setOnClickListener(new View.OnClickListener() { override def onClick(v: View) = u }) } } implicit def view2ClickView(v: View) = new ClickableView(v) // ... later in Scala: b onClick { “make user happy”.toast } // oh... implicit ..toasts?
  23. 23. Implicits // ... in Java: Toast.makeText(ctx, "Toast!", Toast.LENGTH_LONG).show(); // ... in Scala: class ToastableString(s: String) { def toast(ctx: Context) = Toast.makeText(ctx, s, Toast.LENGTH_LONG).show } // ... later in Scala: “make user happy”.toast(getApplicationContext) // find 1 difference: “make user happy”.toast // wtf? Where can we get context?
  24. 24. // ... magic is in Scala: trait Toasty { class ToastString(s: String) { def toast(implicit ctx: Context) = Toast.makeText(ctx, s, Toast.LENGTH_LONG).show } implicit def string2ToastString(s: String) = new ToastString(s) } class NiceActivity extends Activity with Toasty { implicit val ctx = getApplicationContext “make users happy”.toast } Implicits
  25. 25. Shared Preferences // ... in Java: SharedPreferences.Editor sp = ctx.getSharedPreferences(KEY, 0).ed editor.putString("hello", "world"); editor.commit(); // ... in Scala: AppSharedPrefs.hello = "Julia" (AppSharedPrefs.hello + ", you look awesome!").toast
  26. 26. Menus // ... in Java: @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.entries, menu); return true; } // ... in Scala: trait Menus { override def onCreateOptionsMenu(menu: Menu) = { getMenuInflater.inflate(R.menu.entries, menu) true } } class EntriesActivity extends ListActivity with Menus
  27. 27. Closable // ... in Java: try { // do some tricky work... finally { tricky.close(); } // ... in Scala: trait Closers { def closeAfter[A <: {def close():Unit}, B](param:A)(f: A => B): try { f(param) } finally { param.close } } } // work with DB cursors: closeAfter(db.query(...params...)) { cu => // analyze cursor data }
  28. 28. Async Tasks // ... in Javish Scala: new AsyncTask[String, Void, Object] { override def doInBackground(params: String*) : Object = { // download data } }.execute()
  29. 29. Async Tasks // ... in Scala: async { // download data } // same applies to Handlers, post, threads, etc.
  30. 30. Scala Issues on Android Maps API wrapper or v2 API Solutions: ProGuard
  31. 31. Magic behind
  32. 32. Magic behind
  33. 33. Let the Scala be with you! ? *Thanks! Coursera Twitter Scala School Twitter Effective Scala Odersky Book stackoverflow

×