Scala Language

Single Abstract Method Types (SAM Types)

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:

trait Runnable {
  def run(): Unit
}

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

The type can optionally have other non-abstract members:

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

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

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow