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
. . .
$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
. . .
| incompatible types: int cannot be converted to java.lang.String
$5 ==> "1232"
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!
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your databasehttps://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database