Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created September 6, 2024 09:03
Show Gist options
  • Save Sedose/4c2860a4552cfea830c6a0d5e7751c0b to your computer and use it in GitHub Desktop.
Save Sedose/4c2860a4552cfea830c6a0d5e7751c0b to your computer and use it in GitHub Desktop.
Scala 3 implicit function parameter
@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