“The lurking suspicion that something could be simplified is the world’s richest source of rewarding challenges.”
~ Edsger Dijkstra (as seen on twitter yesterday)
“The lurking suspicion that something could be simplified is the world’s richest source of rewarding challenges.”
~ Edsger Dijkstra (as seen on twitter yesterday)
Some people in Colorado crack me up. A recent conversation:
Me: Yada yada yada, I have an autoimmune disease.
Other: You should try marijuana.
When Mrs. Albert Einstein was asked if she understood her husband’s Theory of Relativity, she replied, “No ... but I know my husband, and he can be trusted.”
In today’s installation of “how to have fun with Scala,” if you want to (a) define a Scala method that takes an input parameter, and (b) that parameter has a generic type, and (c) you further want to further declare that the parameter must extend some base type, then (d) use this “bounds” syntax:
def getName[A <: RequiredBaseType](a: A) = ???
This example can be read as, “The parameter a has the generic type A, and A must be a subtype of RequiredBaseType.”
As a concrete example of how using bounds works, start with a simple base type, such as this Scala trait:
trait SentientBeing {
def name: String
}
Next, extend that base trait with a few more traits:
trait AnimalWithLegs extends SentientBeing
trait TwoLeggedAnimal extends AnimalWithLegs
trait FourLeggedAnimal extends AnimalWithLegs
Then extend those with some concrete case classes:
case class Dog(name: String) extends FourLeggedAnimal
case class Person(name: String, age: Int) extends TwoLeggedAnimal
case class Snake(name: String) extends SentientBeing
Notice that Snake extends SentientBeing, but not AnimalWithLegs.
Now that you have all the types you need, define a method that takes a parameter that has a generic type that must extend some base type. To see how everything works, define it this way the first time:
def getName[A <: SentientBeing](a: A): String = a.name
Because the base type (or “super type”) is SentientBeing, all of these calls work just fine:
getName(Person("Fred", 20))
getName(Dog("Rover"))
getName(Snake("Noodles"))
(Copy and paste those into the Scala REPL if you want to verify they work as advertised.)
Next, change getName so the generic type A must be a subtype of AnimalWithLegs:
def getName[A <: AnimalWithLegs](a: A): String = a.name
Now, when you run the same three method calls again, you’ll see that the Snake example fails because it doesn’t extend AnimalWithLegs:
getName(Person("Fred", 20))
getName(Dog("Rover"))
getName(Snake("Noodles")) //error
Here’s what the two sets of getName examples look like in the Scala REPL:

(Right-click that image and select “View image” to see it larger.)
As shown, the second Snake example results in this error message:
scala> getName(Snake("Noodles"))
<console>:15: error: inferred type arguments [Snake] do not conform to method getName's
type parameter bounds [A <: AnimalWithLegs]
getName(Snake("Noodles"))
^
<console>:15: error: type mismatch;
found : Snake
required: A
getName(Snake("Noodles"))
^
This is because the type Snake does not extend AnimalWithLegs. (At least not in my world.)
Technically what’s happening here is that I’m defining A with an “upper bound.” Bounds let you place restrictions on type parameters, and in this example I’m saying, “A must be a subtype of the type AnimalWithLegs”:
def getName[A <: AnimalWithLegs](a: A): String = a.name
More generally, this is how you say, “A must be a subtype of B”:
A <: B
I write more about this topic in, An introduction to Scala Types, so please see that article for a few more details.
If you’re interested in more information on this topic, see my free “Scala 3 Bounds” training video.
“When you blame others, you give up your power to change.”
~ Possibly said by Robert Anthony first
The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).
“I have already lost touch with a couple of people I used to be.”
~ Joan Didion (full quote here on Goodreads)
The Tower app website has a good description of git fetch vs pull. “You can never fetch often enough” is a helpful phrase.
Bill Wyman, bass guitarist for the Rolling Stones, ranks all 165 Pink Floyd songs from worst to best.
Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one Option instance contains one or more other Options, this article may be helpful.
There are times when you’re creating your domain model when it makes sense to use optional fields in your case classes. For instance, when you model an Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:
Here’s a Facebook post on how using AI has improved their language translation system by 11%.
“Between stimulus and response, there is a space. In that space is our power to choose our response. In our response lies our growth and our freedom.”
~ Viktor Frankl (author of Man’s Search for Meaning)
I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.
In general, you probably won’t want to run database queries using a Thread, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:
// works (pre-java8)
Thread t = new Thread() {
public void run() {
mAppDatabase.userDao().insert(u);
}
};
t.start();
Since that code runs in a separate thread, it runs fine even if I enable Android’s StrictMode.
Then I thought, “Hey, if I use the Android Studio 3 Canary Preview, I can use Android’s Java 8 features,” and when I did that I was able to use this Java 8 Thread syntax for the same purpose:
// works (java8)
new Thread(() -> {
mAppDatabase.userDao().insert(u);
}).start();
That also works, and it’s much easier to read than the old Java Thread syntax.
As a quick summary, if you want to use a Java 8 Thread with Android, I hope that last example is helpful. I don’t often interact with a database without calling back to update the UI, but when I’m writing a little test app, or testing new features like the Android Room persistence library, this is a simple approach to keep database I/O code off the main thread.
When I watched the movie Coffee Shop I knew that Laura Vandervoort wasn’t singing the songs in the movie — at least not live — and I assumed that the artist singing was Norah Jones, but it turns out that was wrong. The actual singer’s name is Mandi Mapes, and the two songs are called “Dance With Me” and “In Your Arms.” Videos for both of the Coffee Shop songs are shown below.
“Pure functional programming is programming with mathematical functions.”
~ Erik Meijer
On my list to listen to: A TED Radio Hour discussion, Why Does Time Exist?
“There could have been no two hearts so open, no tastes so similar, no feelings so in unison.”
Jane Austen, Persuasion
(As I learned from the movie, The Lake House)
I realy like this quote from baseball pitcher Jason Marquis, talking about Tony LaRussa, Manager of the St. Louis Cardinals:
“One thing Tony (La Russa) always preached over there was execution and minimizing mental mistakes. You don’t have to have the most talented team to do that, and it doesn’t take the most talented team to win.”
In baseball and in work I think this is true. It’s similar to this quote from Mike Ditka:
“Effort without talent is a depressing situation....but talent without effort is a tragedy.”
As a quick note, I was just looking into the state of Scala “lint” tools, and found ScalaStyle, WartRemover, and Scapegoat.
This 2014 underscore.io post states, “Those interested in FP purity in a Scala world, you’ll want WartRemover.” (Of course that recommendation may have changed by now.) The current ScalaStyle website states, “Scalastyle is used as part of the grading framework for the course Functional Programming Principles in Scala by Martin Odersky on Coursera.”