fields

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.

What are the Drupal 8 Node class fields (field names)? alvin May 17, 2016 - 3:42pm

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 Node fields:

How to list MySQL database table column names without the table formatting alvin May 7, 2016 - 7:45pm

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:

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 declare constructor parameters when extending a Scala class

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

Problem

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.

How to control the visibility of Scala constructor fields

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

Problem

You want to control the visibility of fields that are used as constructor parameters in a Scala class.

How to access Scala var and val fields from Java

I just ran into one thing I wish I had included in the Scala Cookbook that I didn’t include: How to access a val or 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: