I need to write a "decorator" function that takes ...
# arrow
d
I need to write a "decorator" function that takes in any function with arbitrary args (A,B,C,D,...) -> Z and returns a function of the same type that performs some decoration logic. Does arrow provide capabilities to do this?
We currently don’t have any functionality to do this, but we’ve thought about it many times. We’d love to support real N arity functions for applicative builders, parMapN, raceN, etc. Perhaps in the future but that would require a compiler plugin for sure. Kotlin does this internally in the compiler to create `Function`(N).
b
Well, if you have an upper arity limit it's possible to write extension functions similar to the tupling functions, too
e.g.
Copy code
fun <A, B, C> ((A, B) -> C).decorate(f: () -> Unit, g: () -> Unit): ((A, B) -> C) =
        { a, b -> f(); val c = this.invoke(a, b); g(); c }
which is a little specific and ugly but works
not as clever or as elegant as Shapeless or a compiler plugin, to be sure
d
Yeah, shapeless, exactly
Yeah, you could use tupled and untupled, but the compiler would force you to call it for every decoration.
decorate(f.tupled()).untupled()
😞
r
We are gonna support this transparently with compiler plugins
Both polyadic functions that generate automatically n arities based on calls and HList style derivation for product and coproduct type based on the presence of instances of tuples and hlists
But we are not there yet
We have currently proven we can implement higherkinds and pure function checks with compiler plugins and we are currently building support for keep 87 which is a preliminary step to auto derivation of instances
We plan on supporting derivation of instances automatically for product and coproduct types and based on structural discovery for any data type that exposes the main methods from which a type class can derive the rest. For example anything that has map can derive Functor. We attempt at deriving the law tests too
So you could test if your datatype is actually a lawful Functor
d
Nice