Created
September 6, 2024 09:03
-
-
Save Sedose/4c2860a4552cfea830c6a0d5e7751c0b to your computer and use it in GitHub Desktop.
Scala 3 implicit function parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@main def main(): Unit = | |
// Implicit parameter declaration | |
implicit val defaultMultiplier: Int = 2 | |
// Function taking implicit parameter | |
def multiplyBy(x: Int)(implicit multiplier: Int): Int = x * multiplier | |
// Function call without explicitly passed parameter | |
val result = multiplyBy(10) // compiler injects defaultMultiplier automatically | |
println(result) // 20, compiler used default multiplier == 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment