Domanda

I wonder, is there any difference between these two:

val a = 123
println(f"hello1 $a") // 1                         
println(s"hello1 $a") // 2
È stato utile?

Soluzione

According to the docs, f interpolation is typesafe. Also, it allows to add formatting right after the parameter, which s interpolation doesn't support.

Altri suggerimenti

The s interpolator allows you to plug in variables or expressions, while the f interpolator allows you to use formatting commands, à la C printf.

Note that contrary to C printf or Java String.format, the f interpolator will typecheck the arguments for you. This link has more information.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top