Skip to content

Commit fdc8b26

Browse files
committed
Add documentation for partial function transformation
1 parent 5e26f5d commit fdc8b26

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,47 @@ class ExpandSAMs extends MiniPhase {
5656
tree
5757
}
5858

59+
/** A partial function literal:
60+
*
61+
* ```
62+
* val x: PartialFunction[A, B] = { case C1 => E1; ...; case Cn => En }
63+
* ```
64+
*
65+
* which desugars to:
66+
*
67+
* ```
68+
* val x: PartialFunction[A, B] = {
69+
* def $anonfun(x: A): B = x match { case C1 => E1; ...; case Cn => En }
70+
* closure($anonfun: PartialFunction[A, B])
71+
* }
72+
* ```
73+
*
74+
* is expanded to an anomymous class:
75+
*
76+
* ```
77+
* val x: PartialFunction[A, B] = {
78+
* class $anon extends AbstractPartialFunction[A, B] {
79+
* final def isDefinedAt(x: A): Boolean = x match {
80+
* case C1 => true
81+
* ...
82+
* case Cn => true
83+
* case _ => false
84+
* }
85+
*
86+
* final def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 = x match {
87+
* case C1 => E1
88+
* ...
89+
* case Cn => En
90+
* case _ => default(x)
91+
* }
92+
* }
93+
*
94+
* new $anon
95+
* }
96+
* ```
97+
*/
5998
private def toPartialFunction(tree: Block, tpe: Type)(implicit ctx: Context): Tree = {
60-
// /** An extractor for match, either contained in a block or standalone. */
99+
/** An extractor for match, either contained in a block or standalone. */
61100
object PartialFunctionRHS {
62101
def unapply(tree: Tree): Option[Match] = tree match {
63102
case Block(Nil, expr) => unapply(expr)

0 commit comments

Comments
 (0)