Skip to content

Evaluation order of default/named args changed #11571

@som-snytt

Description

@som-snytt

Compiler version

Scala compiler version 3.0.0-RC2-bin-SNAPSHOT-git-6e175cf -- Copyright 2002-2021, LAMP/EPFL

Minimized code

  ./bin/scala
scala> import scala.util.chaining._

scala> def x = 42.tap(println(_))
def x: Int

scala> def y = 27.tap(println(_))
def y: Int

scala> def z = 17.tap(println(_))
def z: Int

scala> def f(i: Int = x, j: Int = y) = i + j
def f(i: Int, j: Int): Int
def f$default$1: Int
def f$default$2: Int

scala> f(j = z)
42
17
val res1: Int = 59

Output

Default arg is evaluated before supplied arg.

Expectation

Actually supplied arg evaluated first, per spec and Scala 2 behavior.

  skala
Welcome to Scala 2.13.6-20210301-034900-3af0b2f (OpenJDK 64-Bit Server VM, Java 15).
Type in expressions for evaluation. Or try :help.

scala> import scala.util.chaining._
import scala.util.chaining._

scala> def x = 42.tap(println(_))
def x: Int

scala> def y = 27.tap(println(_))
def y: Int

scala> def z = 17.tap(println(_))
def z: Int

scala> def f(i: Int = x, j: Int = y) = i + j
def f(i: Int, j: Int): Int

scala> f(j = z)
17
42
val res0: Int = 59

scala>

Alternatively, document the change if it is not already documented. Simplified rewriting has some benefits.

Noticed at #11561 (comment)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions