Scala implicitly vs implicit arguments

后端 未结 1 1744
名媛妹妹
名媛妹妹 2021-02-07 14:24

I am new to Scala, and when I look at different projects, I see two styles for dealing with implicit arguments

scala]]>def sum[A](xs:List[A])(implicit m:Monoi         


        
相关标签:
1条回答
  • 2021-02-07 15:21

    In some circumstances, the implicit formal parameter is not directly used in the body of the method that takes it as an argument. Rather, it simply becomes an implicit val to be passed on to another method that requires an implicit parameter of the same (or a compatible) type. In that case, not having the overt implicit parameter list is convenient.

    In other cases, the context bound notation, which is strictly syntactic sugar for an overt implicit parameter, is considered aesthetically desirable and even though the actual parameter is needed and hence the implicitly method must be used to get it is considered preferable.

    Given that there is no semantic difference between the two, the choice is predicated on fairly subjective criteria.

    Do whichever you like. Lastly note that changing from one to the other will not break any code nor would require recompilation (though I don't know if SBT is discriminting enough to forgo re-compiling code that can see the changed definition).

    0 讨论(0)
提交回复
热议问题