parameter

How do I set the Play Framework 2.6 port in production mode?

Play Framework FAQ: How do I set the Play Framework 2.6 port in production mode?

You set the port that your Play Framework application listens on by passing the http.port parameter to the “run” script for your application:

-Dhttp.port=5150

Here’s an example of a command I use to execute the run script for a Play Framework application named myapp:

How to define Scala methods that take complex functions as parameters (syntax)

Problem: You want to define a Scala method that takes a function as a parameter, and that function may have one or more input parameters, and may also return a value.

Solution: Following the approach described in the previous recipe, define a method that takes a function as a parameter. Specify the function signature you expect to receive, and then execute that function inside the body of the method.

Scala: How to define a generic method parameter that must extend a base type

In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:

def getName[A <: RequiredBaseType](a: A) = ???

That example says, “The parameter a has the generic type A, and A must be a subtype of RequiredBaseType.”

Examples of how to use types in your Scala classes (generics, call-by-name parameters)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.8, “Examples of how to use types in your Scala classes.”

To put what you’ve learned in this chapter to use, let’s create two examples. First, you’ll create a “timer” that looks like a control structure and works like the Unix time com‐ mand. Second, you’ll create another control structure that works like the Scala 2.10 Try/ Success/Failure classes.

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.”

Back to top

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.

Back to top

Solution

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

Table of Contents

  1. Problem
  2. Solution
Back to top

How to write a Scala method that takes a simple generic type

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 19.2, “How to write a Scala method that takes a simple generic type.”

Problem

You’re not concerned about type variance, and want to create a Scala method (or function) that takes a generic type, such as a method that accepts a Seq[A] parameter.

How to use Scala varargs fields from Java (varargs annotation)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 17.5, “How to annotate varargs methods in Scala.”

Problem

You’ve created a Scala method with a varargs field, and would like to be able to call that method from Java code.

Solution

When a Scala method has a field that takes a variable number of arguments, mark it with the @varargs annotation.

How to create a Scala/Akka Actor whose constructor requires arguments

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.2, “How to create a Scala/Akka Actor whose constructor requires arguments.”

Problem

You want to create an Akka actor, and you want your actor’s constructor to have one or more arguments.

Solution

Create the actor using the syntax shown here, where HelloActor takes one constructor parameter:

Scala: How to create methods that take variable-arguments (varargs) fields

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.7, “How to create methods that take variable-arguments (varargs) fields.”

Problem

To make a method more flexible, you want to define a method parameter that can take a variable number of arguments, i.e., a varargs field.

Solution

Define a varargs field in your method declaration by adding a * character after the field type:

How to use parameter names when calling a Scala method

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.4, “How to use parameter names when calling a Scala method.”

Problem

You prefer a coding style where you specify the method parameter names when calling a method.

Solution

The general syntax for calling a method with named parameters is this:

methodName(param1=value1, param2=value2, ...)

This is demonstrated in the following example. Given this definition of a Pizza class: