def getString: String = {
"I am a value to be resolved"
}
def wrapper1(auth)(thunk: => String): String = {
if(auth)
thunk
}
def wrapper2(auth)(valueAlreadyResolved: String): String = {
if(auth)
valueAlreadyResolved. // Auth check did nothing, code already ran!!!
}
wrapper1(auth)(getString)
wrapper2(auth)(getString)
These two method signatures/behavior are different but the syntax to call them is identical! One executes "getString" only when the parameter is referenced (v1), the other (v2) executes it before the wrapper is even entered.
This sort of flexibility can be handy, but it can also shoot you in the foot. Some languages like I think Java make you do something like "::getString" to pass a function pointer. That redundancy makes it explicit and prevents the mistake above which could have serious implications.
This sort of flexibility can be handy, but it can also shoot you in the foot. Some languages like I think Java make you do something like "::getString" to pass a function pointer. That redundancy makes it explicit and prevents the mistake above which could have serious implications.