Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ambiguous Const instances and add missing instances #4458

Merged
merged 3 commits into from
Jul 10, 2023

Conversation

joroKr21
Copy link
Member

@joroKr21 joroKr21 commented Jun 11, 2023

  • Reorder instances in Const systematically
  • Fix ambiguous InvariantMonoidal instance
  • Add ContravariantSemigroupal instance
  • Deprecate unnecessary Functor instance
  • Add Hash instance and hash method
  • Add SemigroupK and MonoidK instances

@armanbilge armanbilge changed the title Fix ambiguous Const instances and add missing instances Fix ambiguous Const instances and add missing instances Jun 11, 2023
Comment on lines +44 to 46
@nowarn("cat=unused")
def traverse[F[_], C](f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a hint (feel free to disregard): with help of scalac-compat it can be addressed in a nicer way:

import org.typelevel.scalaccompat.annotation._

then

Suggested change
@nowarn("cat=unused")
def traverse[F[_], C](f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])
def traverse[F[_], C](@unused f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])

Copy link
Member Author

@joroKr21 joroKr21 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[error] ../typelevel/cats/core/src/main/scala/cats/data/Const.scala:26:12: object typelevel is not a member of package org
[error] import org.typelevel.scalaccompat.annotation._
[error]            ^
[error] ../typelevel/cats/core/src/main/scala/cats/data/Const.scala:46:26: not found: type unused
[error]   def traverse[F[_], C](@unused f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
[error]         

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess it should work better once these changes (typelevel/sbt-typelevel#518) will be available in the current version of the plugin 🤷

Meanwhile you could add it to build.sbt temporarily:

ThisBuild / libraryDependencies += "org.typelevel" %% "scalac-compat-annotation" % "0.1.0" % CompileTime

... or skip my initial comment about it entirely ...

Copy link
Member Author

@joroKr21 joroKr21 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a lot of unused (and other) warnings in the project. I guess it would be good to tackle them together.

@satorg
Copy link
Contributor

satorg commented Jun 12, 2023

@joroKr21 :

Fix ambiguous InvariantMonoidal / instance
Add InvariantSemigroupal instance

Cannot find those in your changes 🤔
Did you mean ContravariantMonoidal / ContravariantSemigroupal ?

@joroKr21
Copy link
Member Author

Cannot find those in your changes 🤔
Did you mean ContravariantMonoidal / ContravariantSemigroupal ?

Ah yes, I fixed the ambiguity of InvariantMonoidal and added ContravariantSemigroupal

@joroKr21 joroKr21 force-pushed the const branch 2 times, most recently from 7161da3 to feb708e Compare June 13, 2023 07:13
satorg
satorg previously approved these changes Jun 14, 2023
Copy link
Contributor

@satorg satorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you!

satorg
satorg previously approved these changes Jun 14, 2023

implicit def catsDataFunctorForConst[C]: Functor[Const[C, *]] =
@deprecated("Use catsDataTraverseForConst instead", "2.10.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to override map in catsDataTraverseForConst, for Performance:tm:. Or, so long as we are keeping ConstFunctor, we can extend from it to inherit the specialized implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it final override because it was also overridden in ConstApplicative

 - Reorder instances in `Const` systematically
 - Fix ambiguous `InvariantMonoidal` instance
 - Add `ContravariantSemigroupal` instance
 - Deprecate unnecessary `Functor` instance
 - Add `Hash` instance and `hash` method
 - Add `SemigroupK` and `MonoidK` instances
 - Both `Traverse` and `Applicative` have a default `map` implementation
 - Trait linearization determines which `map` method is called
 - Make `map` in `ConstFunctor` final override to make sure
@satorg satorg self-requested a review July 3, 2023 08:58
@johnynek johnynek merged commit 6daf274 into typelevel:main Jul 10, 2023
@joroKr21 joroKr21 deleted the const branch July 11, 2023 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants