parameter

How do I set the Play Framework 2.6 port in production mode? alvin April 4, 2018 - 3:17pm

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) alvin March 7, 2018 - 8:14pm

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.

The Java 8 lambda Thread and Runnable syntax and examples alvin August 19, 2017 - 12:55pm

As a quick note, here are some examples of the Java 8 lambda Thread and Runnable syntax. As a little bonus I also show the Java lambda syntax in other situations, such as with an ActionListener, and several “handler” examples, including when a lambda has multiple parameters.

Scala: How to define a generic method parameter that must extend a base type alvin August 9, 2017 - 7:09pm

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) alvin June 21, 2015 - 1:29pm

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) alvin June 21, 2015 - 1:25pm

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 alvin June 21, 2015 - 1:20pm

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) alvin June 20, 2015 - 7:08pm

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 alvin June 19, 2015 - 4:58pm

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 alvin June 13, 2015 - 7:56pm

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: