import

Using `puts` or `echo` instead of `println` in Scala

As my mind was wandering off earlier today, I started to wonder what it would take to create a Ruby puts or PHP echo statement in Scala. (For some reason my brain can never type “println,” and puts or echo are much easier to type.)

One simple way to mimic a puts or echo method is to use Scala's ability to rename things on import:

scala> import System.out.{println => echo}
import System.out.{println=>echo}

scala> import System.out.{println => puts}
import System.out.{println=>puts}

scala> echo("foo")
foo

scala> puts("foo")
foo

scala> puts(1 + 1)
2

Scala: How to add Jar files and classes to the REPL Classpath

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.3, “How to add Jar files and classes to the REPL Classpath.”

Problem

You want to add individual classes or one or more JAR files to the REPL classpath so you can use them in a REPL session.

How to use import statements anywhere (methods, blocks) in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 7.6, “How to use import statements anywhere (methods, blocks) in Scala.”

Problem

You want to use an import statement anywhere, generally to limit the scope of the import, to make the code more clear, or to organize your code.

How to use Scala imports like Java static imports

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 7.5, “How to use ‘static imports’ in Scala.”

Problem

You want to import members in a way similar to the Java static import approach, so you can refer to the member names directly, without having to prefix them with their class name.

Solution

Use this syntax to import all members of the Java Math class:

How to hide a class (or classes) with Scala import statements

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 7.4, “How to hide a class (or classes) with Scala import statements.”

Problem

You want to hide one or more classes while importing other members from the same package.

Solution

To hide a class during the import process, use the renaming syntax shown in Recipe 7.3, “Renaming Members on Import”, but point the class name to the _ wildcard character.

How to rename members on import in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 7.3, “How to rename members on import in Scala.”

Problem

You want to rename members when you import them to help avoid namespace collisions or confusion.

Solution

Give the class you’re importing a new name when you import it with this import syntax:

import java.util.{ArrayList => JavaList}

Then, within your code, refer to the class by the alias you’ve given it:

How to import multiple members in Scala (wildcard and curly braces syntax)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one the shorter recipes, Recipe 7.2, “How to import multiple members in Scala (wildcard and curly braces syntax).”

Problem

You want to import one or more members into the scope of your current program.

Solution

This is the syntax for importing one class:

import java.io.File

You can import multiple classes the Java way:

Android Studio - How to import a jar file (in a libs folder)

I just started using Android Studio 1.x and quickly ran into a problem where I needed to use a Jar file in my Android project. In short, this image shows the steps I followed to import the Jar file into my project. As an important note, I put the Jar file I needed in an app/libs folder, which I created in my project. As the image shows, this affects your Gradle build configuration. (I found this info at this SO link.)

How I significantly improved my iTunes song quality

It was driving me crazy that the quality of the songs I was streaming from a website known as Bandcamp sounded better than the quality of the songs I was playing from my iTunes collection. In my case, I recently bought a Marian Call CD named “Something Fierce”, and had imported the songs from that CD into iTunes. But the songs I streamed from Bandcamp sounded better than the songs I imported from the CD; how could this be?

Scala packaging and import examples

This article is a collection of Scala packaging and import examples. I created most of these examples when I was writing the Scala Cookbook. I share them here without much discussion, but for more examples and discussion, please check out the Cookbook.

Packages imported by default

By default, three packages are implicitly imported for you: