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.
If/when you need to know what keys are available in a Scala SBT build.sbt file, you can find them all listed in the Keys object. At the current moment, here’s the Scaladoc for the SBT Keys object.
Note that the first two SBT keys you’ll probably see are
I was just trying to modify one of my Drupal 8 template files — node.html.twig — and I couldn’t find any good documentation for what variables/values/fields are in the Drupal 8
Node class, so I dumped some output to my browser, and saw that these are the
If you want to list all of the MySQL database table column names (field names) as a simple list of names, with each column name listed on a separate line, just follow these steps.
First, start MySQL with the
-sN options, like this:
$ mysql -sN -u root -p
Then execute a query 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 Recipe 4.10, “How to handle constructor parameters when extending a Scala class.”
You want to extend a base Scala class, and need to work with the constructor parameters declared in the base class, as well as new parameters in the subclass.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.2, “How to control the visibility of Scala class constructor fields.”
You want to control the visibility of fields that are used as constructor parameters in a Scala class.
I just ran into one thing I wish I had included in the Scala Cookbook that I didn’t include: How to access a
var field in a Scala object from your Java code.
In short, if you have a field named
appName defined in a Scala object, like this: