Gregg Hernandez <gregg@lucidchart.com>
golucid.co
class Thing {
private String name;
private int count;
Thing(String name, int count) {
this.name = name;
this.count = count;
}
String getName() {
return this.name;
}
int getCount() {
return this.count;
}
@Override
String toString() {
return "Thing(" + this.name + "," + this.count.toString + ")";
}
@Override
boolean equals(Object that) {
if (this == that) return true;
if (!(that isInstanceOf Thing)) return false;
Thing otherThing = (Thing)that;
return otherThing.name == this.name &&
otherThing.name == this.count;
}
@Override
int hashCode() {
...
}
case class Thing(name: String, count: Int)
class MainActivity extends Activity {
private SharedPreferences preferences = null;
@Override
void onCreate(Bundle state) {
super.onCreate(state);
preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
}
@Override
void onActivityResult(int request, int result, Intent data) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString("last_used", System.getCurrentTimeMillis());
editor.apply();
...
}
}
class MainActivity extends Activity {
private lazy val preferences =
getSharedPreferences("prefs", Context.MODE_PRIVATE)
override
def onActivityResult(request: Int, result: Int, data: Intent) {
SharedPreferences.Editor editor = preferences.edit()
editor.putString("last_used", System.getCurrentTimeMillis())
editor.apply()
...
}
}
JSONObject json = new JSONObject("{...}");
json.getString("key");
json.getInt("count");
json.getBoolean("yes");
JSONObject json = new JSONObject("{...}");
json.getUrl("url"); // doesn't work!
JSONObject json = new JSONObject("{...}");
new URL(json.getString("url"));
val json = new JSONObject("{...}")
json.get[URL]("url")
json.get[Int]("count")
json.get[WhateverYouWant]("hi")
// JSONObject
def get[A: JsonReader](key: String) = {
// omitted
}
// elsewhere
abstract class JsonReader[A] {
def parse(json: JSONObject, key: String): A
}
implicit val urlJsonReader = new JsonReader[URL] {
def parse(json: JSONObject, key: String) = {
/* code that parases a url from json at key */
}
}
implicit val whateverJsonReader =
new JsonReader[WhateverYouWant] { ... }
val json = new JSONObject("{...}")
json.get[URL]("url")
json.get[Int]("count")
json.get[WhateverYouWant]("hi")
def generateJsonReader[A](): JsonReader[A] = macro ???
def generateJsonReader[A](): JsonReader[A] = macro ???
case class Thing(a: String, b: Int, c: URL)
implicit val thingReader = generateJsonReader[Thing]()
def generateJsonReader[A](): JsonReader[A] = macro ???
case class Thing(a: String, b: Int, c: URL)
implicit val thingReader = new JsonReader[Thing] {
def parse(json: JSONObject, key: String): Thing = {
// parsing code
}
}
(ImageButton) findViewById(R.id.sendButton)
findView(TR.sendButton)
<LinearLayout>
<TextView android:id="@+id/text" />
<ImageView android:id="@+id/image" />
</LinearLayout>
override def onCreate(state: Bundle): Unit = {
super.onCreate(state)
val views =
TypedViewHolder.setContentView(this, TR.layout.main)
views.text.addTextchangedlistener(???)
views.image.setImageDrawable(TR.drawable.image.value)
}
lazy val views =
TypedViewHolder.setContentView(this, TR.layout.main)
override def onCreate(state: Bundle): Unit = {
super.onCreate()
views.text.addTextchangedlistener(???)
views.image.setImageDrawable(TR.drawable.image.value)
}
override def onResume(): Unit = {
super.onResume()
views.image.setImageDrawable(TR.drawable.whatever.value)
}
def longRunningOperation(): Long = ???
new Thread(new Runnable() {
public void run() {
longRunningOperation();
}
}).start();
class LongRunningTask extends AsyncTask<In, Integer, Out> {
protected Long doInBackground(In... ins) {
return longRunningTask();
}
protected void onPostExecute(Out result) {
showDialog("Got " + result);
}
}
val result = Future {
longRunningOperation();
}
val result = Future {
longRunningOperation();
}
def double(x: Long): Long = x * 2
val doubleResult: Future[Long] = result.map(double)
val result = Future {
longRunningOperation();
}
def takesForever(x: Long): Future[String] = ???
val doubleResult: Future[Long] =
result.flatMap(takesForever)
val result = for {
a <- firstLongRunner()
b <- secondLongRunnner(a)
c <- thirdLongRunner(b)
// etc
} yield (c * 2)
val result: Future[A] = ???
result.onComplete { maybeValue =>
// always runs when Future is done
}
result.onSuccess { value =>
// runs when ever no exceptions are thrown
}
result.onFailure { e =>
// runs when exceptions are thrown
}
sbt-android
github.com/scala-android/sbt-android
Neophytes Guide To Scala
danielwestheide.com/scala/neophytes.html
@gregghz
gregg@lucidchart.com
github.com/gregghz
golucid.co