You have declared the input type of that Function as Integer, so 123 is boxed from int to Integer, and it all runs nicely.jshell> Function<Integer, String> fun = s -> s.toString();
fun ==> $Lambda$17/1076496284@59ec2012
. . .
jshell> fun.apply(123)
$6 ==> "123"
You can see that fun2 no longer takes an int, not even boxed, but a String as its input, and you get "1232" returned.Function<String, String> fun2 = fun.andThen((String s) -> s + "2").compose((String s) -> Integer.parseInt(s)); // --yes, compiles
fun2 ==> java.util.function.Function$$Lambda$21/1887813102@1ce92674
. . .
jshell> fun2.apply(123)
| Error:
| incompatible types: int cannot be converted to java.lang.String
| fun2.apply(123)
| ^-^
jshell> fun2.apply("123")
$5 ==> "1232"
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
Aaaaaaaaaaaaaaaaaaaah! That explains it.Piet Souris wrote:. . . the compose function is applied first. . . .
Toby Eggitt wrote:Lots of interesting comments, and I admit I might have skimmed them a little too much, but did anyone mention that this compiles (but does not run )
on second look, I've a feeling I did skim it too quickly and missed that this was pretty much already there. Sorry!