Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created October 25, 2020 03:25
Show Gist options
  • Save rupeshtr78/d74ddd58ea6711890c366e11885cdeb5 to your computer and use it in GitHub Desktop.
Save rupeshtr78/d74ddd58ea6711890c366e11885cdeb5 to your computer and use it in GitHub Desktop.
// Type conversions with implicit functions
// When a compiler sees a type that is not expected in the evaluation context
// then it will try to find an implicit function in the current scope
// that can produce the expected type.
implicit def int2Str(int: Int): String = s"$int is Implicit Converted to String"
val x:String = 42.toUpperCase()
println(x) // 42 IS IMPLICIT CONVERTED TO STRING
def functionTakingString(str:String) = str
val y:String = functionTakingString(42)
println(y) // 42 is Implicit Converted to String
// The implicit function name is not that important —
// only the function type signature, in our case its (Int) => (String).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment