Typeclass constraint in typeclass generic

前端 未结 1 1136
借酒劲吻你
借酒劲吻你 2021-01-15 01:58

Over the past week or so I\'ve been working on a typed, indexed array trait for Scala. I\'d like to supply the trait as a typeclass, and allow the library user to implement

相关标签:
1条回答
  • 2021-01-15 02:31

    I guess the solution is

    1. Implement the original IsA2dArray typeclass as an abstract class instead, which then allows me to use the Idx0: IsValidIndex syntax directly above (kindly suggested in the link above).

    This was my original thinking, but a) it is less user friendly, since it requires the user to wrap whatever type they are using in another class which then extends this abstract class.

    No, abstract class will not be extended*, it will be still a type class, just abstract-class type class and not trait type class.

    Can I just assume that trait and abstract class are interchangeable when defining typeclasses?

    Mostly.

    What is the advantage of using abstract classes instead of traits?

    https://www.geeksforgeeks.org/difference-between-traits-and-abstract-classes-in-scala/

    Unless you have hierarchy of type classes (like Functor, Applicative, Monad... in Cats). Trait or abstract class (a type class) can't extend several abstract classes (type classes) while it can extend several traits (type classes). But anyway inheritance of type classes is tricky

    https://typelevel.org/blog/2016/09/30/subtype-typeclasses.html


    * Well, when we write implicit def arr2dIsA2dArray[T, Idx0, Idx1] = new IsA2dArray[Arr2d[T, Idx0, Idx1], T, Idx0, Idx1] {... technically it's extending IsA2dArray but this is similar for IsA2dArray being a trait and abstract class.

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