Is it possible to know if a default argument was supplied as an actual parameter value? In other words, assume that I defined a method:
def myMethod(x: Int = 1) = x + 1
Then is it possible to distinguish between these two calls (which return identical results because the parameter in the second method call has the same value as the default value in method definition) within the method body:
myMethod()
myMethod(1)
In other words, I want to know if there is technique to achieve an effect that is similar to -supplied-p feature in Common Lisp function definitions (see http://www.gigamonkeys.com/book/functions.html and http://www.psg.com/~dlamkins/sl/chapter21.html for details and more context).
No, you can’t directly know this.
Here’s an ugly workaround that relies on the fact that the default parameter can be a method call. It comes with many caveats, the biggest of which is that it is not thread-safe.
Another more practical workaround is this: