class

A Java FIFO queue class

In my spare time lately I’ve been writing this Android football game, and for the game I needed a simple FIFO queue. I haven’t used Java much lately, and after a quick Google search showed things that were more complicated than what I had in mind, I wrote my own FIFO queue.

An anonymous class example in Scala

Sometimes I get away from writing Scala for a while, and when I come back to it I see a piece of code that looks like this following example and I wonder, “What is Foo, and how does this code work?”:

How to wrap Scala traits so they can be used from Java code

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.7, “How to write Scala traits so they can be used from Java code.”

Problem

You’ve written a Scala trait with implemented methods and need to be able to use those methods from a Java application.

Solution

You can’t use the implemented methods of a Scala trait from Java, so wrap the trait in a class.

How to disassemble and decompile Scala code (javap, scalac, jad)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.6, “How to disassemble and decompile Scala code.”

Back to top

Problem

In the process of learning Scala, or trying to understand a particular problem, you want to examine the bytecode the Scala compiler generates from your source code.

Back to top

Solution

You can use several different approaches to see how your Scala source code is translated:

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala: How to add Jar files and classes to the REPL Classpath

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.3, “How to add Jar files and classes to the REPL Classpath.”

Problem

You want to add individual classes or one or more JAR files to the REPL classpath so you can use them in a REPL session.

How to use serialization in Scala (Serializable trait)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.8, “How to use serialization in Scala (Serializable trait).”

Problem

You want to serialize a Scala class and save it as a file, or send it across a network.

Solution

The general approach is the same as Java, but the syntax to make a class serializable is different.

To make a Scala class serializable, extend the Serializable trait and add the @SerialVersionUID annotation to the class:

How to dynamically add a Scala trait to an object instance alvin June 14, 2015 - 4:16pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”

Problem

Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.

Solution

Add the trait to the object when you construct it. This is demonstrated in a simple example:

Scala: How to limit which classes can use a trait by inheritance

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.5, “How to limit which classes can use a trait by inheritance.”

Problem

You want to limit a trait so it can only be added to classes that extend a superclass or another trait.