• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Scala on Your Phone
 

Scala on Your Phone

on

  • 5,450 views

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

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

Statistics

Views

Total Views
5,450
Views on SlideShare
5,429
Embed Views
21

Actions

Likes
3
Downloads
54
Comments
2

3 Embeds 21

http://www.slideshare.net 17
http://www.linkedin.com 2
https://www.linkedin.com 2

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • 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 .
    Are you sure you want to
    Your message goes here
    Processing…
  • Amat menarik untuk dibaca. Saya suka presentation ini.

    Stulangperdana : Pemilik laman web www.mdamin76.com/
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scala on Your Phone Scala on Your Phone Presentation Transcript

  • Scala on Your Phone: Making Mobile Development Suck Less Michael Galpin, eBay @michaelg
  • Example: Event Handlers
  • Just a Click
  • iPhone
  • Android
  • Objective-C
  • @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]; }
  • Java
  • @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); } }); }
  • Scala
  • 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 }) }
  • Advantages
  • Closures • Strongly typed handlers • Forget one-method interfaces • Lexical scoping
  • 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) }
  • Getters, Setters, Updates mapImage.alpha = if (topLevelGroup.searchButton.clickable) BLENDED else OPAQUE allTiles(selectedTileIndex) = throbberImageView
  • Operators resultsLabelView += "Read more..." keywordInput -= eventListener mainMenu ++ fileMenuItems
  • Traits class SearchDetailsActivity extends Activty with JsonParser, Trackable
  • 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 _ => }
  • Trade Secrets
  • * Compiler Dex Compiler Compress Source Code Class files Dex File APK
  • 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 }
  • 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) } } ... }
  • 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
  • Scala on Android • More Correct • More Concise • Simpler • Perfect Fit
  • Challenges
  • 2500+ Classes
  • Hey, What About?