Vavr: Proposal: Scala's "wunderbar" for functions

Created on 7 Feb 2021  路  1Comment  路  Source: vavr-io/vavr

Scala has the "wunderbar" which will let you apply a couple of arguments to a function while leaving others open.

The following would create a function that takes a single argument (the one we're "leaving open" with _):

addThree(1, _, 3)

We could implement something similar in Vavr by introducing a Placeholder class

public final class Placeholder {

  public static Placeholder __ = new Placeholder();

  private Placeholder() {
  }
}

We could then come up with a whole bunch of apply-overloads, like the following for Function3:

 default Function1<T2, R> apply(T1 first, Placeholder secondPlaceholder, T3 third) {
  return (second) -> apply(first, second, third);
 }

To be able to do the following in Vavr:

addThree.apply(1, __, 3)

This would require 2^args number of overloads (up until 256 for Function8). I've already written a simple script to generate all these overloads.

Would do you think?

question

Most helpful comment

Thanks for your suggestion! Unfortunately that is no option for us. My current strategy is to reduce the number of generated types in Vavr. Once we had a function and tuple arity of 26. That would be 2^26 overloads. Opening the Javadoc would blow up the browser. 馃槺

What is wrong with creating the function the direct way in places where you need it?

(n) -> addThree.apply(1, n, 3)

I will close this issue but the discussion is still open on any questions!

>All comments

Thanks for your suggestion! Unfortunately that is no option for us. My current strategy is to reduce the number of generated types in Vavr. Once we had a function and tuple arity of 26. That would be 2^26 overloads. Opening the Javadoc would blow up the browser. 馃槺

What is wrong with creating the function the direct way in places where you need it?

(n) -> addThree.apply(1, n, 3)

I will close this issue but the discussion is still open on any questions!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rumatoest picture rumatoest  路  5Comments

liviamoroianu picture liviamoroianu  路  3Comments

yarulan picture yarulan  路  5Comments

manu-m picture manu-m  路  6Comments

noorulhaq picture noorulhaq  路  5Comments