Dotty Documentation

0.11.0-bin-SNAPSHOT

class ExpandSAMs
extends MiniPhase

Expand SAM closures that cannot be represented by the JVM as lambdas to anonymous classes. These fall into five categories

  1. Partial function closures, we need to generate isDefinedAt and applyOrElse methods for these.
  2. Closures implementing non-trait classes
  3. Closures implementing classes that inherit from a class other than Object (a lambda cannot not be a run-time subtype of such a class)
  4. Closures that implement traits which run initialization code.
  5. Closures that get synthesized abstract methods in the transformation pipeline. These methods can be (1) superaccessors, (2) outer references, (3) accessors for fields.

However, implicit function types do not count as SAM types.

[-] Constructors

ExpandSAMs ( )

[-] Members

[+] private def checkRefinements ( tpe: Type , pos: Position ) ( implicit ctx: Context ) : Type
[+] def isPlatformSam ( cls: ClassSymbol ) ( implicit ctx: Context ) : Boolean

Is the SAMType cls also a SAM under the rules of the platform?

[+] override def phaseName : String

A name given to the Phase that can be used to debug the compiler. For instance, it is possible to print trees after a given phase using:

$ ./bin/dotc -Xprint:<phaseNameHere> sourceFile.scala
[+] private def toPartialFunction ( tree: Block , tpe: Type ) ( implicit ctx: Context ) : Tree

A partial function literal:

val x: PartialFunction[A, B] = { case C1 => E1; ...; case Cn => En }

which desugars to:

val x: PartialFunction[A, B] = {
  def $anonfun(x: A): B = x match { case C1 => E1; ...; case Cn => En }
  closure($anonfun: PartialFunction[A, B])
}

is expanded to an anomymous class:

val x: PartialFunction[A, B] = {
  class $anon extends AbstractPartialFunction[A, B] {
    final def isDefinedAt(x: A): Boolean = x match {
      case C1 => true
      ...
      case Cn => true
      case _  => false
    }

    final def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 = x match {
      case C1 => E1
      ...
      case Cn => En
      case _  => default(x)
    }
  }

  new $anon
}
[+] override def transformBlock ( tree: Block ) ( implicit ctx: Context ) : Tree