Is it possible to know if a default argument was supplied as an actual parameter value (when they are equal) in Scala? -
is possible know if default argument supplied actual parameter value? in other words, assume defined method:
def mymethod(x: int = 1) = x + 1
then possible distinguish between these 2 calls (which return identical results because parameter in second method call has same value default value in method definition) within method body:
mymethod() mymethod(1)
in other words, want know if there technique achieve effect similar -supplied-p feature in common lisp function definitions (see http://www.gigamonkeys.com/book/functions.html , http://www.psg.com/~dlamkins/sl/chapter21.html details , more context).
no, can't directly know this.
here's ugly workaround relies on fact default parameter can method call. comes many caveats, biggest of is not thread-safe.
private var useddefault = false private def default = { useddefault = true 1 } def mymethod(x: int = default) = { if (useddefault) { println("default") } else { println("supplied") } useddefault = false }
another more practical workaround this:
def mymethod(): unit = mymethod(1, useddefault = true) def mymethod(x: int): unit = mymethod(x, useddefault = false) private def mymethod(x: int, useddefault: boolean) = { if (useddefault) { println("default") } else { println("supplied") } }
Comments
Post a Comment