Consider this code:
def withInt(unused: Boolean = false)(n: Int): Int = n + 1
def withInt(n: Int): Int = withInt()(n)
withInt(0) // OK
The idea is that the second function lets me write withInt(0)
instead of withInt()(0)
.
Now, the same thing doesn’t work with context functions:
def withContext(unused: Boolean = false)(code: Int ?=> Int): Int = code(using 42)
def withContext(code: Int ?=> Int): Int = withContext()(code)
withContext()(summon[Int]) // OK
withContext(summon[Int]) // rejected
It only works if the second function uses a different name, e.g., withContext2
.
I don’t understand why the two cases behave differently. Is that expected? (Probably not: why let me define a method that I cannot call?)
(Note also that withContext(summon[Int])
and withContext(0)
give me two different errors: one related to contexts and the other to overloaded names).