implicit parameter VS default parameter value
implicit, scala
Solution
You should definitely prefer default parameter value.
- You should never create or use implicit parameters of general types like `Int` or `String`. See citation below.
- Default value is the simplest solution. In terms of language features complexity.
- Implicit parameters are for some kind of "context" for your method. If there is no context, just default value you can confuse other developers.
- Implicit value search will cost you some amount of compilation time.
- Implicit parameters should be specified manually only in rare cases.
See also: Programming In Scala 21.5 Implicit parameters/A style rule for implicit parameters:
As a style rule, it is best to use a custom named type in the types of implicit parameters.
Problem
There are, at least, two techniques in Scala to pass default value to a method 1) default parameter value ``` scala> def f(i: Int = 0) = i f: (i: Int)Int scala> f() res0: Int = 0 scala> f(1) res1: Int = 1 ``` 2) implicit parameter ``` scala> def g(implicit i: Int) = i g: (implicit i: Int)Int scala> implicit val default = 0 default: Int = 0 scala> g(1) res5: Int = 1 scala> g res7: Int = 0 ``` In which case do you choose one or another ? With the power of implicit, default values are they a usefull feature ?