Several OO languages allow you to grab unapplied method references.
Some, such as Kotlin or Python, have them accessible from the class itself and just append self onto the front of the argument list.
class IntWrapper(private val x: Int) {
fun adding(other: IntWrapper) = IntWrapper(x + other.x)
}
val additionFunc: (IntWrapper, IntWrapper) -> IntWrapper = IntWrapper::adding
Excuse the terrible example, I can't think of any practical one off the top of my head.
JavaScript handles this notoriously poorly, due to the weird rules around this:
class NumberWrapper {
constructor(x) { this.x = x; }
adding(other) { return new NumberWrapper(this.x + other.x); }
}
const additionFunc = NumberWrapper.prototype.adding;
let fifteen = additionFunc.apply(new NumberWrapper(5), [new NumberWrapper(10)]);
// OR
const addFive = additionFunc.bind(new NumberWrapper(5));
fifteen = addFive(new NumberWrapper(10));
Swift handles this in a way I don't think I've seen before, modelling unapplied references as currying in a way that's not otherwise common in the language.
// Example method call, for comparison
let x = 1.0
_ = x.addingProduct(2.0, 3.0)
// Now, for the unapplied reference:
let fma: (Double) -> (Double, Double) -> Double = Double.addingProduct
// what the heck is that type signature?
Some languages, like C#, don't provide any mechanism for this, and you have to explicitly write out x => x.Foo().
What are the advantages and disadvantages to each approach? What other approaches exist?