Let’s assume this function:
def autoClosing(f: {def close();})(t: =>Unit) = {
t
f.close()
}
and this snippet:
val a = autoClosing(new X)(_)
a {
println("before close")
}
is it possible to curry the first part? Something like:
val a = autoClosing(_) { println("before close") }
so that I could send the objects on which close should be performed, and have the same block executed on them?
Yes, the snippet you have given works, as long as you give the type of the placeholder character.
Therefore, the code you are looking for is:
which compiles and works as expected :).
A couple of notes:
AnyReftype having aclosemethod, something liketype Closeable = AnyRef {def close()}, or an appropriate interface.autoClosing(_: Closeable){ ... }is actually equivalent to the following expanded anonymous function:c: Closeable => autoClosing(c){ ... }. The wildcard character is just shorthand for a partially applied function. You need to give the type of the_as the type inferer unfortunately cannot infer the type in this case.Hope it helps,
— Flaviu Cipcigan