Single Abstract Method Types (SAM Types)

Other topics

Remarks:

Single Abstract Methods are types, introduced in Java 8, that have exactly one abstract member.

Lambda Syntax

NOTE: This is only available in Scala 2.12+ (and in recent 2.11.x versions with the -Xexperimental -Xfuture compiler flags)

A SAM type can be implemented using a lambda:

2.11.8
trait Runnable {
  def run(): Unit
}

val t: Runnable = () => println("foo")

The type can optionally have other non-abstract members:

2.11.8
trait Runnable {
  def run(): Unit
  def concrete: Int = 42
}

val t: Runnable = () => println("foo")

Contributors

Topic Id: 3664

Example Ids: 12607

This site is not affiliated with any of the contributors.