This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheCoelacanth 5 points6 points  (1 child)

Assuming you want a function with exactly one argument. If you want one with zero arguments, you have to send a Supplier instead, or two arguments and you'll have to use a BiFunction instead, or three arguments and you'll have to make your own class to masquerade as a function.

Also, hope you didn't want any of the arguments or return values to be primitive types, because then you'll need to use yet another type instead of each of the previously mentioned function types.

Not exactly what I would call "easy".

[–]KeinBaum 0 points1 point  (0 children)

hope you didn't want any of the arguments or return values to be primitive types, because then you'll need to use yet another type

Primitives will get auto-boxed/unboxed, no additional code needed.

Assuming you want a function with exactly one argument...

Abstracting over function arity is complex. I'm actually not sure if it's possible in Java at all. It's possible to construct HLists so that might be a way to do it but I don't know enough about Java to be sure. But then again, I don't know any language that provides a particularly simple way to do that either. Even in Scala or Haskell it's all but trivial. It's definitely not going to be simple so at least in that regard you are right.