How to apply no args a TermMethod with a default argument?

I am trying to apply no arguments to a TermMethod that has a default argument.

It doesn’t look as though the Symbol has any knowledge of defaults so I was expecting to find two Symbols, one that has a parameter and one that does not, but there is only one.

In this particular case it is a constructor. The ClassSymbol only has the one <init> method which has a single parameter.

How do I do this?

The convention is currently that a “default arg” is supplied by a special method (belonging to the companion for class parameters). Those “default arg” methods are named after the method they supply args for, plus an index to mean which arg.

There is code here.

➜  scala -Vprint:typer
Welcome to Scala 3.2.2 (20.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> def f(x: Int = 42) = x
[[syntax trees at end of                     typer]] // rs$line$1
package <empty> {
  final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
  final module class rs$line$1() extends Object() { this: rs$line$1.type =>
    def f(x: Int): Int = x
    def f$default$1: Int @uncheckedVariance = 42
  }
}

def f(x: Int): Int

scala> class C(x: Int = 42)
[[syntax trees at end of                     typer]] // rs$line$2
package <empty> {
  final lazy module val rs$line$2: rs$line$2 = new rs$line$2()
  final module class rs$line$2() extends Object() { this: rs$line$2.type =>
    class C(x: Int) extends Object() {
      private[this] val x: Int
    }
    final lazy module val C: C = new C()
    final module class C() extends AnyRef() { this: C.type =>
      def $lessinit$greater$default$1: Int @uncheckedVariance = 42
    }
  }
}

// defined class C
2 Likes

Ah ok, thanks.

So does that mean that when an argument is not supplied then the compiler will search for these “default arg” methods? If I wanted to do that in a Macro then I would have to replicate this same logic myself? Seems easier to pass the same value as the default in that case.