Blogs

How to define a collection whose element are all of some base type (inheritance)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.6, “How to define a collection whose element are all of some base type.”

Problem

You want to specify that a class or method takes a type parameter, and that parameter is limited so it can only be a base type, or a subtype of that base type.

Solution

Define the class or method by specifying the type parameter with an upper bound. To demonstrate this, create a simple type hierarchy:

How (and why) to make mutable collections invariant in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.4, “How to make mutable collections invariant in Scala.”

Problem

You want to create a collection whose elements can be mutated, and want to know how to specify the generic type parameter for its elements.

Solution

When creating a collection of elements that can be changed (mutated), its generic type parameter should be declared as [A], making it invariant.

How to create Scala classes that use generic types (cookbook examples)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.1, “How to create Scala classes that use generic types (cookbook examples).”

Problem

You want to create a class (and associated methods) that uses a generic type.

Solution

As a library writer, creating a class (and methods) that takes a generic type is similar to Java. For instance, if Scala didn’t have a linked-list class and you wanted to write your own, you could write the basic functionality like this:

An introduction to Scala Types (from the Scala Cookbook)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is the introduction to Chapter 19, Types.

As you can tell from one look at the Scaladoc for the collections classes, Scala has a powerful type system. However, unless you’re the creator of a library, you can go a long way in Scala without having to go too far down into the depths of Scala types. But once you start creating collections-style APIs for other users, you will need to learn them.

How to configure SBT to work with Eclipse

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.7, “How to configure SBT to work with Eclipse.”

Problem

You want to use Eclipse with a project you’re managing with SBT.

Solution

Use the Scala IDE for Eclipse project so you can work on Scala projects in Eclipse, and use the “sbteclipse” plug-in to enable SBT to generate files for Eclipse.

How to create an SBT project with subprojects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.6, “How to create an SBT project with subprojects.”

Problem

You want to configure SBT to work with a main project that depends on other subprojects you’re developing.

Solution

Create your subproject as a regular SBT project, but without a project subdirectory. Then, in your main project, define a project/Build.scala file that defines the dependencies between the main project and subprojects.

SBT: How to control which version of a managed dependency is used

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.5, “SBT: How to control which version of a managed dependency is used.”

Problem

You want to make sure you always have the desired version of a managed dependency, including the latest integration release, milestone release, or other versions.