Because you passed () => "empty"
as the first parameter, which is a function of type () => String
; so that's what the type B
is going to be. Note that the ifEmpty
parameter is a call-by-name parameter.
If you don't want B
to be () => String
, but just String
, then try this instead:
Some(1).fold("empty")(d => d.toString)
added after your edit: The main difference between your version 1 and version 2 demo is that in version 1, ifEmpty
is a call-by-name parameter, and in version 2, g
is not a call-by-name parameter, but a function of type A => B
.