Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why implicitConversions is required for implicit defs but not classes?

As far as I understand, implicit conversions can result in potentially hard to understand code, or code suffering from other problems (perhaps even bugs?), which is why they require explicit enabling in order to be used in code without getting warnings.

However, given that implicit conversions are in big part (if not most of the time) used for wrapping an object with an object of another type, and so are implicit classes—I'd appreciate you correcting me if I'm wrong—, why do the former require the import of scala.language.implicitConversions but the latter do not?

object Main extends App {   implicit class StringFoo(x: String) {     def fooWithImplicitClass(): Unit =       println("foo with implicit class")   }   // => silence.    "asd".fooWithImplicitClass()    /************************/    class Foo(x: String) {     def fooWithImplicitDef(): Unit =       println("foo with implicit def")   }   implicit def string2Foo(x: String) = new Foo(x)   // => warning: implicit conversion method string2Foo should be enabled    "asd".fooWithImplicitDef() } 
like image 602
Erik Kaplun Avatar asked Sep 20 '13 10:09

Erik Kaplun


People also ask

What is the use of implicit class in Scala?

Scala 2.10 introduced a new feature called implicit classes. An implicit class is a class marked with the implicit keyword. This keyword makes the class's primary constructor available for implicit conversions when the class is in scope. Implicit classes were proposed in SIP-13.

How do you use implicit conversions?

An implicit conversion from type S to type T is defined by an implicit value which has function type S => T , or by an implicit method convertible to a value of that type. Implicit conversions are applied in two situations: If an expression e is of type S , and S does not conform to the expression's expected type T .

What are Scala implicit conversions?

Implicit conversions in Scala are the set of methods that are apply when an object of wrong type is used. It allows the compiler to automatically convert of one type to another. Implicit conversions are applied in two conditions: First, if an expression of type A and S does not match to the expected expression type B.

What is implicit class in Java?

The implicit parameter in Java is the object that the method belongs to. It's passed by specifying the reference or variable of the object before the name of the method. An implicit parameter is opposite to an explicit parameter, which is passed when specifying the parameter in the parenthesis of a method call.


1 Answers

Implicit classes effectively only add new methods (or traits), and they are only ever used when these added methods are called (or the implicit class is used explicitly, but this rarely happens in practice). Implicit conversions to existing types, on the other hand, can be invoked with less visibility to the programmer.

like image 184
Alexey Romanov Avatar answered Oct 16 '22 16:10

Alexey Romanov