Scala: reconciling type classes with dependency injection

前端 未结 3 1441
野的像风
野的像风 2021-01-31 20:06

There seems to be a lot of enthusiasm among Scala bloggers lately for the type classes pattern, in which a simple class has functionality added to it by an addi

3条回答
  •  孤街浪徒
    2021-01-31 20:49

    The argument against type classes as dependency injection here is that with type classes the "precise type of the items is known at compile time" whereas with dependency injection, they are not. You might be interested in this Scala project rewrite effort where I moved from the cake pattern to type classes for dependency injection. Take a look at this file where the implicit declarations are made. Notice how the use of environment variables determines the precise type? That is how you can reconcile the compile time requirements of type classes with the run time needs of dependency injection.

提交回复
热议问题