I know that in SCALA I can convert a type to another by define some implicit function, my question is , how can I know what conversions can I use after importing tons of packages?
For example, I have a string, and than how can I know what types can it convert to ?
Edit to clarify, I wanna do it in my scala compile plugin, so I may need to call a function on a reflect.api.tree type, and then get the implicits. I am looking some method to use the implicitly[] mentioned in the answer.
Implicit Type Conversion in C. C allows us to mix basic types in an expression. We have seen a glimpse of this behavior while discussing mixed mode arithmetic in chapter Arithmetic Operators in C. In such expressions, operand of one type is converted to another type. This process is known as Type Conversion.
Amount cannot be converted in to Money; therefore, we need explicit casting. So as to summarize, the implicit keyword should be used to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.
Performing an explicit conversion is also known as casting an expression to a given data type or object class.
There are two types of type conversion: Implicit type conversion. Explicit type conversion. In this chapter, we are discussing Implicit type conversion. This type of conversion is done by the compiler according to the following rules:
In the REPL you can invoke :implicits
to see all the in-scope implicits other than those available from the Predef. (Add -v
to see Predef implicits as well.)
You can also invoke the implicitly[]
function from anywhere in your code to test for particular implicits.
scala> implicitly[String => Seq[Char]]
res0: String => Seq[Char] = <function1>
scala> implicitly[String => Array[Char]]
<console>:12: error: No implicit view available from String => Array[Char].
implicitly[String => Array[Char]]
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With