Hey !
So first of all and that might be source of confusion there are several kinds of implicits.
First one, is the horrible one: implicit conversion.
This is define as
implicit def [NAME](input: INPUT_TYPE): OUTPUT_TYPE = ???
And it allows you to automatically turn a value into an other type:
val a: INPUT_TYPE = ...
val b: OUTPUT_TYPE = a
in normal condition the second line above would break on compilation because a
and b
are two different type but with implicit conversion, the code will use the defined conversion method implicitly to convert it from INPUT_TYPE
to OUTPUT_TYPE
.
Read more at https://tourofscala.com/scala/implicit-conversion
Next one is implicit classes, that allows you to add a new method to an existing type:
implicit class NAME(value_name: TypeToOverride) {
def methodToAdd: OutputType = ???
}
So now when you use a value of type TypeToOverride
you will be able to call this new added method methodToAdd
.
Learn more at https://tourofscala.com/scala/implicit-class
Next one is implicit argument, which allows you to pass an argument to a method without having to it explicitly.
here: https://tourofscala.com/scala/implicit-val
in a method you would have
def method(arg1: A, arg2: B)(implicit arg3: C): OUT
and now you can call the method like you would normally do:
method(a,b)(c)
but if you have the implicit C
in scope you can call the method without it:
method(a,b)
assuming that somewhere you have
implicit val c: C
The last one is a bit more complex and is implicit proof. Ill just add the link to it: https://tourofscala.com/scala/implicit-proof .