field

What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them)

Table of Contents1 - def field in trait2 - val field in trait (abstract)3 - val field in trait (concrete)4 - var field in trait (abstract)5 - var field in trait (concrete)6 - An abstract class in the middle7 - A trait in the middle8 - Summary

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 def, val, and 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.

How to find all MySQL database tables that have specific column names alvin May 8, 2016 - 9:56pm

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:

Scala: How to access the MongoDB document ‘_id’ field with Casbah

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.”

Problem

You want to get the ID field for a document you’ve inserted into a MongoDB collection.

Solution

Perform a query to get the document you want, and then call get("_ID") on the resulting MongoDBObject, like this:

How to create a simple Scala object from a JSON String

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.”

Problem

You need to convert a JSON string into a simple Scala object, such as a Scala case class that has no collections.

Solution

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.

How to create JSON strings from Scala classes that have collections fields

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.”

Problem

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.

How to create a JSON string from a Scala object

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.”

Problem

You’re working outside of a specific framework, and want to create a JSON string from a Scala object.

Solution

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:

How to use abstract and concrete fields in Scala traits

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.”

Problem

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.

How to create static members with Scala companion objects

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.”

Problem

You want to create a class that has instance methods and static methods, but unlike Java, Scala does not have a static keyword.