2

In the following snippet you can see that the string parameter has some default value

def a(i: Int, s: String = "please autocomplete this param") = i
def b = (a _).tupled.andThen(println(_))

b(2) //compiler complains: not enough parameters

But as you can see, compiler looses this information and now want both parameters

Wat do (extra points if someone sneaks shapeless in here somehow)

EDIT for posterity: there is no way to do this

JSelser
  • 3,510
  • 1
  • 19
  • 40

1 Answers1

4

According to eptx

Functions don't support parameter defaults. Methods do. Converting from a method to a function loses parameter defaults. (Scala 2.8.1)

Printing -Xprint:typer compilation phase of

object Foo {
  def a(i: Int, s: String = "please autocomplete this param") = i
}

gives something like

def a(i: Int, s: String = "please autocomplete this param"): Int = i;
<synthetic> def a$default$2: String = "please autocomplete this param"

where we see default argument is available as a$default$2. So we could do something we should definitely NOT do and combine implicit conversion with compiler-implementation-dependent detail

object Foo {
  def a(i: Int, s: String = "please autocomplete this param") = i
  def b = (a _).tupled.andThen(println(_))
  implicit def xToTupleWithDefault(x: Int): (Int, String) = (x, a$default$2)
}

import Foo._
b(2)   // expands to b(xToTupleWithDefault(2))
Mario Galic
  • 47,285
  • 6
  • 56
  • 98