Posts in the “scala” category

Creating Play Framework template functions (examples)

When you want to create a Play Framework template function, you can find some examples on the Scala Templates page. There you'll find these first two examples.

First, assuming you have a Product model defined something like this:

case class Product(var name: String, var price: BigDecimal)

The first template looks like this:

The essence of Scala ~ Martin Odersky

Per this tweet, back on May 15 Martin Odersky shared a slide with these contents:

The essence of Scala: Fusion of functional and object-oriented programming in a typed setting:

- Functions for the logic
- Objects for the modularity

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:

How to get multiple, unique, random elements from a list of elements

One thing I never thought about before is that if you need to get multiple, unique, random elements from a list of elements, one solution to the problem is to shuffle the list and then take as many elements as you want/need. For instance, if you want three unique, random elements from a list of integers in Scala, you can do this:

scala> val list = List(1,2,3,4,5,1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

scala> val uniq = list.distinct
uniq: List[Int] = List(1, 2, 3, 4, 5)

scala> val shuffled = scala.util.Random.shuffle(uniq)
shuffled: List[Int] = List(1, 4, 5, 2, 3)

scala> val firstThree = shuffled.take(3)
firstThree: List[Int] = List(1, 4, 5)

As that solution shows, you start with a simple list; get the unique/distinct elements from the list; shuffle those elements to create a new list; then take the first three elements from the shuffled list. That’s probably not a great solution for huge lists, but for many simple lists it’s a way to get multiple random elements from the list.

How to run a Scala SBT-packaged jar file with Java (the `java` command)

If you want to run/execute a main method from a jar file you created with Scala and the sbt package command, this little tutorial shows how to do it. To make things a little more complicated, my Scala project depends on three external jar files, and the main method requires a command-line argument.

As noted in the Summary, you’ll probably want to use a tool like SBT-Assembly for larger projects.

How to run/execute a JAR file created with Dotty (Scala 3)

As a brief note to self, if you need to run a main method in a JAR file created with Dotty (Scala 3), and you want to run execute that application using the java command, the syntax is the same as before, you just have to figure out which JAR files to include in your classpath.

I just created an example using Scala futures and found that I could execute/run the example using this command:

java -cp "/path/to/dotty-library_0.27-0.27.0-RC1.jar:/path/to/scala-library-2.13.3.jar:target/scala-0.27/multiplefutures2_0.27-0.1.0.jar" futures.MultipleFutures2

In that command, multiplefutures2_0.27-0.1.0.jar is the name of my JAR file, and futures.MultipleFutures2 is the name of my package and main method, respectively.

If you want to execute a JAR file built with Dotty, I hope this example is helpful

How to run a Scala application in a jar file while setting the classpath

I don’t know why, but without digging into it more, all I can say right now is that I can’t use the Java Sound API from within SBT. Whenever I try running sbt run, I keep getting the following error message, even though I know that my app and sound file work fine when I package my Java application normally:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file

As part of the debugging process I created a little shell script named run.sh that contained these two lines:

Is the Scala Cookbook worth the price? (A brief note)

At $50 or $60, I know the Scala Cookbook, 2nd Edition — estimated at 700+ pages — is expensive. But if it helps to know it, I’ve been working on one recipe for three days. And this isn’t the only one. Asking questions on Gitter channels, the Contributors website, testing and reporting bugs for developers, digging through code. All so you don’t have to. ;)

How to use the Scala 3 nightly build as your REPL

As a brief note, if you want to use the Scala 3 nightly build as your REPL, one way to do that is with this project I just put on Github. Just:

  1. Clone that project
  2. cd into its directory
  3. Type sbt to start sbt
  4. Type console to start a Scala 3 REPL inside sbt
  5. Use the latest, greatest Scala syntax, as shown in this image:

How to use Scala 3 nightly build REPL with sbt

The hack that makes this work is in the build.sbt file, and shown here in bold:

lazy val root = project
  .in(file("."))
  .settings(
    name := "Scala3Nightly",
    description := "Lets me use the Scala 3 nightly build",
    version := "0.1.0",
    scalaVersion := dottyLatestNightlyBuild.get,
    useScala3doc := true,
  )

As shown in the build.sbt file, I found that scalaVersion hack on this page: github.com/scala/scala3-example-project.

Sorry, I have to run off to a meeting now, but if you want to use the latest Scala 3 nightly build as your REPL, I hope this is helpful.

(Also, as shown in this gitter.im/dotty link, you can use Coursier for this purpose, but in this case it’s much easier to use this sbt hack to create a Scala 3 REPL with the latest nightly build, imho.)

How to emit raw HTML with the Play Framework and Twirl template library

I’ll write more about this when I have some free time, but for the moment I’ll just say that if you ever need to emit raw HTML when using the Play Framework and Twirl template library, code like this will do the trick:

@play.twirl.api.HtmlFormat.raw(currentBookNode.bodyValue)

In this case the bodyValue field already contains HTML as a Scala String, and I don’t want Twirl to mess with that, so I use its HtmlFormat.raw method as shown.

How to create inline methods in Scala 3

In this article I’ll take a look at creating Scala 3 inline methods.

Background

First, from the Scala documentation: “inline is a new soft modifier that guarantees that a definition will be inlined at the point of use.” This is different than the Scala 2 inline annotation, which notes, “An annotation for methods that the optimizer should inline ... If inlining is not possible, for example because the method is not final, an optimizer warning will be issued.”

Scala shell scripts and the command line: Prompting the user, and reading input

A great thing about Scala is that not only is it scalable, it was also created to help you work on small tasks, including being useful in shell scripts. This includes small shell script tasks like prompting a user interactively from a shell script, and reading their input.

You can prompt users with print commands like println and print, and you can read their input with all of these methods that are available on the Scala Console class:

How to show the Scala version SBT is using

sbt FAQ: How do I show the Scala version that sbt is using?

Short answer

Issue the scalaVersion command at the sbt prompt.

Longer answer

I was having a problem getting a certain Scala feature to work when I compiled my code with sbt, and even though I knew I had set the Scala version in my build.sbt file, I wanted a way to double-check that sbt was indeed using that Scala version. That’s when I found I can issue the scalaVersion at the sbt prompt.

More info

Also, these sbt commands can point you to more information:

  • about
  • settings
  • help settings

And here are a few related links: