Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala on Your Phone

3,878 views

Published on

See how you can develop Android applications using the Scala programming language. Presented at Silicon Valley Code Camp 2009.

  • Thanks for this slide (Scala on Your Phone). Congrats, you've shared information clearly and concisely. It's one of my favorites. Seek your permission to embed it to my site at www.sprintringtones.org/ .

    Anita Gomez .
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Amat menarik untuk dibaca. Saya suka presentation ini.

    Stulangperdana : Pemilik laman web www.mdamin76.com/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Scala on Your Phone

  1. 1. Scala on Your Phone: Making Mobile Development Suck Less Michael Galpin, eBay @michaelg
  2. 2. Example: Event Handlers
  3. 3. Just a Click
  4. 4. iPhone
  5. 5. Android
  6. 6. Objective-C
  7. 7. @synthesize textInput; @synthesize label; @synthesize name; - (IBAction)changeGreeting:(id)sender { self.name = textInput.text; NSString *nameString = name; if([nameString length] == 0) { nameString = @"Code Camp"; } NSString *greeting = [[NSString alloc] initWithFormat:@"Hello %@!", nameString]; label.text = greeting; [greeting release]; } - (BOOL)textFieldShouldReturn:(UITextField *)theTextField { if(theTextField == textInput) { [textInput resignFirstResponder]; } return YES; } - (void)dealloc { [textInput release]; [label release]; [name release]; [super dealloc]; }
  8. 8. Java
  9. 9. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button sayButton = (Button) findViewById(R.id.say_button); sayButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { EditText nameBox = (EditText) findViewById(R.id.name); String name = nameBox.getText().toString(); TextView greeting = (TextView) findViewById(R.id.greeting); greeting.setText("Hello " + name); } }); }
  10. 10. Scala
  11. 11. override def onCreate(savedInstanceState:Bundle){ import R.id._ super.onCreate(savedInstanceState) setContentView(R.layout.main) val sayButton = findViewById(say_button).asInstanceOf[Button] sayButton.setOnClickListener( () => { val user = findViewById(name).asInstanceOf[EditText].text findViewById(greeting).asInstanceOf[TextView].text = "Hello " + user }) }
  12. 12. Advantages
  13. 13. Closures • Strongly typed handlers • Forget one-method interfaces • Lexical scoping
  14. 14. Closures • Strongly typed handlers • Forget one-method interfaces • Lexical scoping val computedValue = parseInt(findViewById(foo).asInstanceOf[EditText].text) this.post( () => { val url = new URL("http://foo.com?qs=" + computedValue) Source.fromStream(url.openStream).getLines.foreach(findViewById(results).text += _) }) mainMenu.onClick = (item:MenuItem) => { startActivity(item.activity) }
  15. 15. Getters, Setters, Updates mapImage.alpha = if (topLevelGroup.searchButton.clickable) BLENDED else OPAQUE allTiles(selectedTileIndex) = throbberImageView
  16. 16. Operators resultsLabelView += "Read more..." keywordInput -= eventListener mainMenu ++ fileMenuItems
  17. 17. Traits class SearchDetailsActivity extends Activty with JsonParser, Trackable
  18. 18. XML val results = for (suggested <- responseXml"Suggested"){ yield Term(sugested"Term" text, suggested"Priority" text) } nextNode match { case <code>{txt}</code> => this.status = parseInt(txt) case <state>{txt}</state> => this.state = txt case _ => }
  19. 19. Trade Secrets
  20. 20. * Compiler Dex Compiler Compress Source Code Class files Dex File APK
  21. 21. Implicits object Activity{ implicit def funcToClicker1(f:_root_.android.view.View => Unit):OnClickListener = new OnClickListener(){ def onClick(v:_root_.android.view.View)=f.apply(v)} implicit def funcToClicker0(f:() => Unit):OnClickListener = new OnClickListener() { def onClick(v:_root_.android.view.View)=f.apply} implicit def funcToLongClicker0(f:() => Boolean):OnLongClickListener = new OnLongClickListener() { def onLongClick(v:_root_.android.view.View) = f.apply} implicit def funcToLongClicker1(f:_root_.android.view.View => Boolean):OnLongClickListener = new OnLongClickListener() { def onLongClick(v:_root_.android.view.View) = f.apply(v)} implicit def viewToRichView(v:_root_.android.view.View):scala.android.view.View = new scala.android.view.View(v) implicit def richViewToView(view:scala.android.view.View):_root_.android.view.View = view.base }
  22. 22. Bag o’ Tricks class ViewGroup(base:_root_.android.view.ViewGroup) extends View(base){ object views{ def +=(v:_root_.android.view.View){ base.addView(v) } def apply(index:Int) = new { def update(v:_root_.android.view.View) = base.addView(index,v) } } ... }
  23. 23. Bag o’ Tricks class ViewGroup(base:_root_.android.view.ViewGroup) extends View(base){ object views{ def +=(v:_root_.android.view.View){ base.addView(v) } def apply(index:Int) = new { def update(v:_root_.android.view.View) = base.addView(index,v) } } ... } myGroup.views += someOtherView myGroup.views(3) = yetAnotherView
  24. 24. Scala on Android • More Correct • More Concise • Simpler • Perfect Fit
  25. 25. Challenges
  26. 26. 2500+ Classes
  27. 27. Hey, What About?

×