Created
October 25, 2020 03:25
-
-
Save rupeshtr78/d74ddd58ea6711890c366e11885cdeb5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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