You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
This seems a bit surprising to me - the extension method combine magically came into scope when there was a Monoid[T] in scope, but the method unit still has to be invoked explicitly on the Monoid instance. It would make more sense if one had to do import summon[Monoid[T]]._ before being able to use combine that way. But that's annoying, so it would be even nicer if one could let the compiler know that Monoid and SemiGroup were typeclasses and make it so that whenever there an instance of Monoid is in scope, its members are also in scope. I was thinking something like this:
Despite perhaps complicating the language more, it feels more uniform - no special treatment for extension - and looks a little more like Haskell's typeclasses (not that that's something Dotty should necessarily aim for, but it's satisfying).
And of course, one would still be able to use summon[Monoid[T]].unit if there happened to be another unit method in scope.
The typeclass annotation could also generate an apply method in the companion object for convenience, as was suggested in this comment. So if there happened to be unit methods in scope, one would be able to do xs.foldLeft(Monoid[T].unit)(_.combine(_)) and avoid this boilerplate:
Currently, the Dotty documentation shows this example of implementing semigroups and monoids.
And shows this example of using it:
This seems a bit surprising to me - the extension method
combine
magically came into scope when there was aMonoid[T]
in scope, but the methodunit
still has to be invoked explicitly on theMonoid
instance. It would make more sense if one had to doimport summon[Monoid[T]]._
before being able to usecombine
that way. But that's annoying, so it would be even nicer if one could let the compiler know thatMonoid
andSemiGroup
were typeclasses and make it so that whenever there an instance ofMonoid
is in scope, its members are also in scope. I was thinking something like this:Despite perhaps complicating the language more, it feels more uniform - no special treatment for
extension
- and looks a little more like Haskell's typeclasses (not that that's something Dotty should necessarily aim for, but it's satisfying).And of course, one would still be able to use
summon[Monoid[T]].unit
if there happened to be anotherunit
method in scope.The
typeclass
annotation could also generate anapply
method in the companion object for convenience, as was suggested in this comment. So if there happened to beunit
methods in scope, one would be able to doxs.foldLeft(Monoid[T].unit)(_.combine(_))
and avoid this boilerplate:The text was updated successfully, but these errors were encountered: