class

Scala Option, Some, None syntax examples

Today I’m sharing some examples of the Scala Option/Some/None syntax. These examples will show how to use an Option for the var fields in a Scala class. Then I’ll show how to set those Option fields, and then get the values from the Option fields.

To get started, we’ll need a little case class to represent an Address:

Solution to the Scala jar class file is broken error

I just ran into the following Scala jar (class) is broken error:

A Scala case class copy method example

Scala case class FAQ: When you create a case class in Scala, a copy method is generated for your case class. What does this copy method do?

In short, it lets you make a copy of an object, where a "copy" is different than a clone, because with a copy you can change fields as desired during the copying process.

To demonstrate this, let's create an Employee class as a case class:

Scala List class examples - range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll try to share examples of the most common List operations (methods).

A Scala 2.10 implicit class example

Scala FAQ: Can you share an example of how to create an implicit class in Scala 2.10?

Sure. As your question implies, the implicit class functionality changed in Scala 2.10, so let's take a look at the new syntax.

Scala - How to add new methods to existing classes

Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.

A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.

Scala REPL - How to show more methods on a class/object in the REPL

When you're working in the Scala REPL and want to see what methods are available on a class/object, you can create an instance of an object, follow that with the "." character, and then press the [Tab] key. This process, known as "tab completion" in the REPL, gives you a preliminary list of methods that can be called on the object.

Here's what this looks like when we try it on an Int object:

How to show Scala String and StringOps methods in the REPL

Just a quick note here today on how to show methods from the String class in the Scala REPL, as well as methods from the StringOps class, which aren't seen as easily.

First, if you've used tab completion in the REPL, you may already know that you can show many String class methods in the REPL. If you hit the [Tab] key once you'll see a short list of String methods:

Converting a Scala class file to decompiled Java source code

As a Scala newbie, I'm curious about how the process of converting a Scala class back to Java source code works. What I really want to see is how my Scala source code is converted to Java source code. Besides plain old curiosity, I think that understanding more about how Scala works can also be very important to my understanding of Scala (such as the apply() method, and so on).

How to create multiple class constructors in Scala

Scala constructors FAQ: How do I create a Scala class with multiple constructors (secondary constructors)?

The Scala approach to defining multiple class constructors is a little different than Java, but somewhat similar. Rather than try to explain this in words, I just created some example source code to demonstrate how this works.

Here's some source code to demonstrate the Scala "multiple constructors" approach:

Syndicate content