object

SBT: How to pass command line arguments to ‘sbt run’ alvin August 17, 2017 - 11:30am

Question: How do I pass command-line parameters to my Scala application when I’m running the application with SBT?

Solution: There are two different possible scenarios here:

Scala for Java devs: Everything in Scala is an object alvin August 9, 2017 - 11:26am

The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).

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 parse JSON data into an array of Scala objects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.4, “How to parse JSON data into an array of Scala objects.”

Problem

You have a JSON string that represents an array of objects, and you need to deserialize it into objects you can use in your Scala application.

How to create a simple Scala object from a JSON String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.3, “How to create a simple Scala object from a JSON String.”

Problem

You need to convert a JSON string into a simple Scala object, such as a Scala case class that has no collections.

Solution

Use the Lift-JSON library to convert a JSON string to an instance of a case class. This is referred to as deserializing the string into an object.

How to create JSON strings from Scala classes that have collections fields

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.2, “How to create a JSON String from Scala classes that have collections.”

Problem

You want to generate a JSON representation of a Scala object that contains one or more collections, such as a Person class that has a list of friends or addresses.

How to create a JSON string from a Scala object

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.1, “How to create a JSON string from a Scala object.”

Problem

You’re working outside of a specific framework, and want to create a JSON string from a Scala object.

Solution

If you’re using the Play Framework, you can use its library to work with JSON, as shown in Recipes 15.14 and 15.15, but if you’re using JSON outside of Play, you can use the best libraries that are available for Scala and Java:

How to dynamically add a Scala trait to an object instance

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”

Problem

Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.

Solution

Add the trait to the object when you construct it. This is demonstrated in a simple example:

How to implement the Factory Method in Scala with ‘apply’

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.9, “How to implement the Factory Method in Scala with ‘apply.’”

Problem

To let subclasses declare which type of object should be created, and to keep the object creation point in one location, you want to implement the factory method in Scala.

How to create Scala object instances without using the “new” keyword (apply)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.8, “How to create object instances without using the 'new' keyword.”

Problem

You’ve seen that Scala code looks cleaner when you don’t always have to use the new keyword to create a new instance of a class, like this:

val a = Array(Person("John"), Person("Paul"))

So you want to know how to write your code to make your classes work like this.