×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Scala
Posted by: Pahay Ragh
Added: Oct 25, 2020 3:27 AM
Views: 4520
Tags: no tags
  1. //    Type conversions with implicit functions
  2.     //    When a compiler sees a type that is not expected in the evaluation context
  3.     //    then it will try to find an implicit function in the current scope
  4.     //    that can produce the expected type.
  5.  
  6.     implicit def int2Str(int: Int): String = s"$int is Implicit Converted to String"
  7.  
  8.     val x:String = 42.toUpperCase()
  9.     println(x)  // 42 IS IMPLICIT CONVERTED TO STRING
  10.  
  11.     def functionTakingString(str:String) = str
  12.  
  13.     val y:String = functionTakingString(42)
  14.     println(y)  // 42 is Implicit Converted to String
  15.  
  16.     //    The implicit function name is not that important —
  17.     //    only the function type signature, in our case its (Int) => (String).