0
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 = textInp...
Java
@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
      ...
Scala
override def onCreate(savedInstanceState:Bundle){
    import R.id._
    super.onCreate(savedInstanceState)
    setContentV...
Advantages
Closures
•   Strongly typed handlers   •   Forget one-method
                                  interfaces
•   Lexical scop...
Closures
          •   Strongly typed handlers           •   Forget one-method
                                           ...
Getters, Setters, Updates

mapImage.alpha = if (topLevelGroup.searchButton.clickable) BLENDED else OPAQUE

allTiles(select...
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)...
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 =
      ne...
Bag o’ Tricks
class ViewGroup(base:_root_.android.view.ViewGroup) extends View(base){
   object views{
     def +=(v:_root...
Bag o’ Tricks
class ViewGroup(base:_root_.android.view.ViewGroup) extends View(base){
   object views{
     def +=(v:_root...
Scala on Android

• More Correct
• More Concise
• Simpler
• Perfect Fit
Challenges
2500+ Classes
Hey, What About?
Scala on Your Phone
Scala on Your Phone
Scala on Your Phone
Scala on Your Phone
Scala on Your Phone
Scala on Your Phone
Upcoming SlideShare
Loading in...5
×

Scala on Your Phone

3,487

Published on

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

2 Comments
4 Likes
Statistics
Notes
  • 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
No Downloads
Views
Total Views
3,487
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
58
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide
  • Transcript of "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?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×