scala

The Scala community build project

Scala community build is an interesting project, which is described like this:

“This repository contains configuration files that enable us to build and test a corpus of Scala open source projects together using Lightbend's `dbuild`. How big is it? It’s 3.2 million lines of Scala code, total, from 185 projects (as of January, 2019), and takes about 15 hours to run.”

“Why do this? The main goal is to guard against regressions in new versions of Scala (language, standard library, and modules). It’s also a service to the open source community, providing early notice of issues and incompatibilities.”

ScalaCheck custom generator examples

Table of Contents1 - Custom generators2 - Built-in ScalaCheck generators3 - How to use ScalaCheck generators4 - More ScalaCheck generators

Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.

Back to top

Custom generators

This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:

Scala: Generating a sequence/list of all ASCII printable characters

I ran into a couple of interesting things today when trying to generate random alphanumeric strings in Scala, which can be summarized like this. I won’t get into the “random” stuff I was working on, but here are a couple of examples of how to generate lists of alphanumeric/ASCII characters in Scala:

scala> val chars = ('a' to 'Z').toList
chars: List[Char] = List()

scala> val chars = ('A' to 'z').toList
chars: List[Char] = 
List(A, B, C, D, E, F, G, H, I, J, K, L, 
     M, N, O, P, Q, R, S, T, U, V, W, X, 
     Y, Z, [, \, ], ^, _, `, a, b, c, d, 
     e, f, g, h, i, j, k, l, m, n, o, p, 
     q, r, s, t, u, v, w, x, y, z)

scala> val chars = (' ' to 'z').toList
chars: List[Char] = 
List( , !, ", #, $, %, &, ', (, ), *, +, 
     ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 
     8, 9, :, ;, <, =, >, ?, @, A, B, C, 
     D, E, F, G, H, I, J, K, L, M, N, O, 
     P, Q, R, S, T, U, V, W, X, Y, Z, [, 
     \, ], ^, _, `, a, b, c, d, e, f, g, 
     h, i, j, k, l, m, n, o, p, q, r, s, 
     t, u, v, w, x, y, z)

Implicit methods/functions in Scala 2 and 3 (Dotty extension methods)

Table of Contents1 - Scala 2: Create the method in an implicit class2 - Scala 3 (Dotty): Adding methods to closed classes with extension methods

Scala lets you add new methods to existing classes that you don’t have the source code for, i.e., classes like String, Int, etc. For instance, you can add a method named hello to the String class so you can write code like this:

"joe".hello

which yields output like this:

"Hello, Joe"

Admittedly that’s not the most exciting method in the world, but it demonstrates the end result: You can add methods to a closed class like String. Properly (tastefully) used, you can create some really nice APIs.

In this article I’ll show how you can create implicit methods (also known as extension methods) in Scala 2 and Scala 3 (Dotty).