Scala implicit conversions is a powerful tool for Scala coding. It lets you decrease redundancy in your code and lets you make the code easier to read.
Scala implicit conversions feature enables you to do the following:
- Highlight methods in which implicit converions were used.
- Select from the available list of all applicable implicit conversions.
- Apply conversion either explicitly or in shorter version and still get the same result.
- Invoke implicit parameters.
Implicit Conversion highlighting
If Scala type-aware highlighting is enabled then IntelliJ IDEA highlights methods that are added via implicit conversions with a grey underline:
Invoking implicit conversion methods
Select an expression and pressCtrl+Shift+Q(Ctrl+Qfor Mac) to invoke the list of applicable implicit conversions. The list shows the regular scope displayed on the top and the expanded scope that is displayed on the bottom of the list.
You can make the implicit conversion method explicit. PressAlt+Enterand select Make explicit or Make explicit (Import method) :
If you select Make explicit then IntelliJ IDEA returns a method call with the class name. It might be helpful if you need to make sure that the compiler imports the particular implicit conversion method that you originally wanted:
If you select Make explicit (Import method) then the method is imported statically and IntelliJ IDEA returns just its call without the class name. Also, the next time you open the list of useful implicit conversions you will see this method in the regular scope:
Invoking implicit parameters
Place a cursor to the method where implict conversion was used and pressCtrl+Shift+P(Meta+Shift+Pfor Mac) to invoke implicit parameters. It might be helpful for code analizing when you want to find out what implicit parametrers were passed to the particular call. IntelliJ IDEA also lets you view the recursive implicit parameters.
If IntelliJ IDEA cannot find method calls where implicit paramerers were passed, it displays a pop-up message:
6 Comments
Anonymous
"lets you make the code easier to read."
typical scala style. It is indeed much easier to read -- but not for reader/maintainer
It is easier for the writer, much harder to reader.
Anonymous
Thank you, that was helpful. "It is indeed easier to read but not for the reader"
If you don't like scala don't use it, there's no point in making such comments.
Anonymous
I like scala, but I dont like implicits. shoudl i stop using scala just cuz i dont like one feature of it?
Anonymous
What is the "Meta" key on Mac? More specifically, what does this key sequence actually map to: (Meta+ Shift+ P for Mac)
Anonymous
It's the `command` key, marked as ⌘ on many keyboards.
So the combination you're looking for is ⌘⇧P
Anonymous
Is there a way to import conversions if you don't know what package it is in?