Skip to content

Commit

Permalink
Idiomatic scala 3 implicits
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Nov 16, 2023
1 parent 01e6a41 commit 1a0de7a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 31 deletions.
61 changes: 61 additions & 0 deletions cats/src/main/scala-2/magnolify/cats/CatsImplicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.cats

import cats.Show
import cats.kernel.*

trait CatsImplicits {
implicit def genBand(b: Band.type): magnolify.cats.BandDerivation.type =
magnolify.cats.BandDerivation

implicit def genCommutativeGroup(
cg: CommutativeGroup.type
): magnolify.cats.CommutativeGroupDerivation.type =
magnolify.cats.CommutativeGroupDerivation

implicit def genCommutativeMonoid(
cm: CommutativeMonoid.type
): magnolify.cats.CommutativeMonoidDerivation.type =
magnolify.cats.CommutativeMonoidDerivation

implicit def genCommutativeSemigroup(
cm: CommutativeSemigroup.type
): magnolify.cats.CommutativeSemigroupDerivation.type =
magnolify.cats.CommutativeSemigroupDerivation

implicit def genEq(eq: Eq.type): magnolify.cats.EqDerivation.type =
magnolify.cats.EqDerivation

implicit def genGroup(g: Group.type): magnolify.cats.GroupDerivation.type =
magnolify.cats.GroupDerivation

implicit def genHash(h: Hash.type): magnolify.cats.HashDerivation.type =
magnolify.cats.HashDerivation

implicit def genMonoid(m: Monoid.type): magnolify.cats.MonoidDerivation.type =
magnolify.cats.MonoidDerivation

implicit def genSemigroup(sg: Semigroup.type): magnolify.cats.SemigroupDerivation.type =
magnolify.cats.SemigroupDerivation

implicit def genShow(sg: Show.type): magnolify.cats.ShowDerivation.type =
magnolify.cats.ShowDerivation

}

object CatsImplicits
47 changes: 47 additions & 0 deletions cats/src/main/scala-3/magnolify/cats/CatsImplicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package magnolify.cats

import cats.Show
import cats.kernel.*

import scala.deriving.Mirror

trait CatsImplicits:

// format: off
extension (b: Band.type) inline def gen[T](using Mirror.Of[T]): Band[T] = BandDerivation.gen
extension (cg: CommutativeGroup.type) inline def gen[T](using Mirror.Of[T]): CommutativeGroup[T] = CommutativeGroupDerivation.gen

extension (cm: CommutativeMonoid.type) inline def gen[T](using Mirror.Of[T]): CommutativeMonoid[T] = CommutativeMonoidDerivation.gen

extension (cm: CommutativeSemigroup.type) inline def gen[T](using Mirror.Of[T]): CommutativeSemigroup[T] = CommutativeSemigroupDerivation.gen

extension (eq: Eq.type) inline def gen[T](using Mirror.Of[T]): Eq[T] = EqDerivation.gen

extension (g: Group.type) inline def gen[T](using Mirror.Of[T]): Group[T] = GroupDerivation.gen

extension (h: Hash.type) inline def gen[T](using Mirror.Of[T]): Hash[T] = HashDerivation.gen

extension (m: Monoid.type) inline def gen[T](using Mirror.Of[T]): Monoid[T] = MonoidDerivation.gen

extension (sg: Semigroup.type) inline def gen[T](using Mirror.Of[T]): Semigroup[T] = SemigroupDerivation.gen

extension (s: Show.type) inline def gen[T](using Mirror.Of[T]): Show[T] = ShowDerivation.gen
// format: on

object CatsImplicits
2 changes: 1 addition & 1 deletion cats/src/main/scala/magnolify/cats/auto/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

package magnolify.cats

package object auto extends AutoDerivation
package object auto extends CatsImplicits with AutoDerivation
32 changes: 2 additions & 30 deletions cats/src/main/scala/magnolify/cats/semiauto/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package magnolify.cats

import cats.Show
import cats.kernel.*

package object semiauto {
package object semiauto extends CatsImplicits {

@deprecated("Use Band.gen[T] instead", "0.7.0")
val BandDerivation = magnolify.cats.BandDerivation
Expand All @@ -39,30 +36,5 @@ package object semiauto {
val SemigroupDerivation = magnolify.cats.SemigroupDerivation
@deprecated("Use Show.gen[T] instead", "0.7.0")
val ShowDerivation = magnolify.cats.ShowDerivation
implicit def genBand(b: Band.type): magnolify.cats.BandDerivation.type =
magnolify.cats.BandDerivation
implicit def genCommutativeGroup(
cg: CommutativeGroup.type
): magnolify.cats.CommutativeGroupDerivation.type =
magnolify.cats.CommutativeGroupDerivation
implicit def genCommutativeMonoid(
cm: CommutativeMonoid.type
): magnolify.cats.CommutativeMonoidDerivation.type =
magnolify.cats.CommutativeMonoidDerivation
implicit def genCommutativeSemigroup(
cm: CommutativeSemigroup.type
): magnolify.cats.CommutativeSemigroupDerivation.type =
magnolify.cats.CommutativeSemigroupDerivation
implicit def genEq(eq: Eq.type): magnolify.cats.EqDerivation.type =
magnolify.cats.EqDerivation
implicit def genGroup(g: Group.type): magnolify.cats.GroupDerivation.type =
magnolify.cats.GroupDerivation
implicit def genHash(h: Hash.type): magnolify.cats.HashDerivation.type =
magnolify.cats.HashDerivation
implicit def genMonoid(m: Monoid.type): magnolify.cats.MonoidDerivation.type =
magnolify.cats.MonoidDerivation
implicit def genSemigroup(sg: Semigroup.type): magnolify.cats.SemigroupDerivation.type =
magnolify.cats.SemigroupDerivation
implicit def genShow(sg: Show.type): magnolify.cats.ShowDerivation.type =
magnolify.cats.ShowDerivation

}

0 comments on commit 1a0de7a

Please sign in to comment.