What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them)
I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using
def. But there are still a few things I wonder about.
Today I had a few free moments and I decided to look at what happens under the covers when you use
var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with
scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.
I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.
A farmer plowing a field in Palmer, Alaska on May 20, 2015.
At 10:30 pm.
Here’s a photo I took on the drive back from Golden today of some cattle on the right and wrong side of the fence, a big field, and some mountains. (Click the small image to see the full size image.)
MySQL FAQ: How can I find all MySQL database tables that have specific column names?
I found the solution on this SO page. Here’s my take on it.
First, assuming that you want to copy and paste those column names after you get them, I recommend starting the MySQL command line client like this:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 16.7, “How to access the MongoDB document 'ID' field (_id) with Casbah.”
You want to get the
ID field for a document you’ve inserted into a MongoDB collection.
Perform a query to get the document you want, and then call
get("_ID") on the resulting
MongoDBObject, like this:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.3, “How to create a simple Scala object from a JSON String.”
You need to convert a JSON string into a simple Scala object, such as a Scala
case class that has no collections.
Use the Lift-JSON library to convert a JSON string to an instance of a
case class. This is referred to as deserializing the string into an object.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.2, “How to create a JSON String from Scala classes that have collections.”
You want to generate a JSON representation of a Scala object that contains one or more collections, such as a
Person class that has a list of friends or addresses.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.1, “How to create a JSON string from a Scala object.”
You’re working outside of a specific framework, and want to create a JSON string from a Scala object.
If you’re using the Play Framework, you can use its library to work with JSON, as shown in Recipes 15.14 and 15.15, but if you’re using JSON outside of Play, you can use the best libraries that are available for Scala and Java:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.2, “How to use abstract and concrete fields in Scala traits.”
You want to put abstract or concrete fields in your traits so they are declared in one place and available to all types that implement the trait.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.6, “How to create static members with Scala companion objects.”
You want to create a class that has instance methods and static methods, but unlike Java, Scala does not have a