class

How to Write a Scala Class That Can Be Used in a ‘for’ Expression alvin June 1, 2017 - 7:28pm

“You must unlearn what you have learned.”

Yoda, Star Wars

Introduction

As a reminder, the reason for the next several lessons on for expressions is so that you can get to a point where you can understand the following code:

A Java FIFO queue class alvin January 18, 2016 - 6:18pm

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.

Haskell lecture series (CIS 194, upenn.edu) alvin November 25, 2015 - 5:36pm

This upenn.edu link has online materials for a Haskell course. I don’t know if those notes will make any sense if you don’t know anything about Haskell, but if you know a little about Haskell the notes and assignments look useful. (I found this course via this learnhaskell Github project.)

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) alvin June 20, 2015 - 4:19pm

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: