Skip to content

Commit

Permalink
Reformat with scalafmt 3.7.3
Browse files Browse the repository at this point in the history
Executed command: scalafmt --non-interactive
  • Loading branch information
scala-steward committed Mar 29, 2023
1 parent 079e5bd commit c800c94
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 93 deletions.
17 changes: 11 additions & 6 deletions src/core/impl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ object CaseClassDerivation:
IArray[Any](typeAnns[A]*)
):
def construct[PType: ClassTag](makeParam: Param => PType): A =
product.fromProduct(Tuple.fromArray(parameters.map(makeParam).to(Array)))
product.fromProduct(
Tuple.fromArray(parameters.map(makeParam).to(Array))
)

def rawConstruct(fieldValues: Seq[Any]): A =
product.fromProduct(Tuple.fromArray(fieldValues.to(Array)))
Expand All @@ -58,10 +60,11 @@ object CaseClassDerivation:
): M[A] = {
val m = summon[Monadic[M]]
m.map {
parameters.map(makeParam).foldLeft(m.point(Array())) { (accM, paramM) =>
m.flatMap(accM) { acc =>
m.map(paramM)(acc ++ List(_))
}
parameters.map(makeParam).foldLeft(m.point(Array())) {
(accM, paramM) =>
m.flatMap(accM) { acc =>
m.map(paramM)(acc ++ List(_))
}
}
} { params => product.fromProduct(Tuple.fromArray(params)) }
}
Expand Down Expand Up @@ -148,5 +151,7 @@ trait SealedTraitDerivation:
)
)
}
(sub ::: subtypesFromMirror[A, tail](m, idx + 1)).distinctBy(_.typeInfo).sortBy(_.typeInfo.full)
(sub ::: subtypesFromMirror[A, tail](m, idx + 1))
.distinctBy(_.typeInfo)
.sortBy(_.typeInfo.full)
end SealedTraitDerivation
92 changes: 49 additions & 43 deletions src/core/interface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ object CaseClass:

type PType

/**
* Gives the constructed typeclass for the parameter's type. Eg for a `case class Foo(bar: String, baz: Int)`,
* where this [[Param]] denotes 'baz', the `typeclass` field returns an instance of `Typeclass[Int]`.
*/
/** Gives the constructed typeclass for the parameter's type. Eg for a `case
* class Foo(bar: String, baz: Int)`, where this [[Param]] denotes 'baz',
* the `typeclass` field returns an instance of `Typeclass[Int]`.
*/
def typeclass: Typeclass[PType]

/**
* Get the value of this param out of the supplied instance of the case class.
*
* @param value an instance of the case class
* @return the value of this parameter in the case class
*/
/** Get the value of this param out of the supplied instance of the case
* class.
*
* @param value
* an instance of the case class
* @return
* the value of this parameter in the case class
*/
def deref(param: Type): PType

/** Requires compilation with `-Yretain-trees` on.
Expand Down Expand Up @@ -93,15 +95,16 @@ object CaseClass:
end Param
end CaseClass


/**
* In the terminology of Algebraic Data Types (ADTs), case classes are known as 'product types'.
*
* @param parameters an array giving information about the parameters of the case class. Each [[Param]] element
* has a very useful [[CaseClass.Param.typeclass]] field giving the constructed typeclass for the
* parameter's type. Eg for a `case class Foo(bar: String, baz: Int)`, you can
* obtain `Typeclass[String]`, `Typeclass[Int]`.
*/
/** In the terminology of Algebraic Data Types (ADTs), case classes are known as
* 'product types'.
*
* @param parameters
* an array giving information about the parameters of the case class. Each
* [[Param]] element has a very useful [[CaseClass.Param.typeclass]] field
* giving the constructed typeclass for the parameter's type. Eg for a `case
* class Foo(bar: String, baz: Int)`, you can obtain `Typeclass[String]`,
* `Typeclass[Int]`.
*/
abstract class CaseClass[Typeclass[_], Type](
val typeInfo: TypeInfo,
val isObject: Boolean,
Expand Down Expand Up @@ -191,12 +194,11 @@ abstract class CaseClass[Typeclass[_], Type](

end CaseClass

/**
* Represents a Sealed-Trait or a Scala 3 Enum.
*
* In the terminology of Algebraic Data Types (ADTs), sealed-traits/enums are termed
* 'sum types'.
*/
/** Represents a Sealed-Trait or a Scala 3 Enum.
*
* In the terminology of Algebraic Data Types (ADTs), sealed-traits/enums are
* termed 'sum types'.
*/
case class SealedTrait[Typeclass[_], Type](
typeInfo: TypeInfo,
subtypes: IArray[SealedTrait.Subtype[Typeclass, Type, _]],
Expand Down Expand Up @@ -243,17 +245,20 @@ case class SealedTrait[Typeclass[_], Type](
override def toString: String =
s"SealedTrait($typeInfo, IArray[${subtypes.mkString(",")}])"

/**
* Provides a way to recieve the type info for the explicit subtype that
* 'value' is an instance of. So if 'Type' is a Sealed Trait or Scala 3
* Enum like 'Suit', the 'handle' function will be supplied with the
* type info for the specific subtype of 'value', eg 'Diamonds'.
*
* @param value must be instance of a subtype of typeInfo
* @param handle function that will be passed the Subtype of 'value'
* @tparam Return whatever type the 'handle' function wants to return
* @return whatever the 'handle' function returned!
*/
/** Provides a way to recieve the type info for the explicit subtype that
* 'value' is an instance of. So if 'Type' is a Sealed Trait or Scala 3 Enum
* like 'Suit', the 'handle' function will be supplied with the type info for
* the specific subtype of 'value', eg 'Diamonds'.
*
* @param value
* must be instance of a subtype of typeInfo
* @param handle
* function that will be passed the Subtype of 'value'
* @tparam Return
* whatever type the 'handle' function wants to return
* @return
* whatever the 'handle' function returned!
*/
def choose[Return](value: Type)(handle: Subtype[_] => Return): Return =
@tailrec def rec(ix: Int): Return =
if ix < subtypes.length then
Expand Down Expand Up @@ -288,10 +293,11 @@ object SealedTrait:
IArray.empty[Any]
)

/**
* @tparam Type the type of the Sealed Trait or Scala 3 Enum, eg 'Suit'
* @tparam SType the type of the subtype, eg 'Diamonds' or 'Clubs'
*/
/** @tparam Type
* the type of the Sealed Trait or Scala 3 Enum, eg 'Suit'
* @tparam SType
* the type of the subtype, eg 'Diamonds' or 'Clubs'
*/
class Subtype[Typeclass[_], Type, SType](
val typeInfo: TypeInfo,
val annotations: IArray[Any],
Expand Down Expand Up @@ -327,9 +333,9 @@ object SealedTrait:
asType
)

/**
* @return the already-constructed typeclass instance for this subtype
*/
/** @return
* the already-constructed typeclass instance for this subtype
*/
def typeclass: Typeclass[SType & Type] =
callByNeed.value.asInstanceOf[Typeclass[SType & Type]]
def cast: PartialFunction[Type, SType & Type] = this
Expand Down
57 changes: 28 additions & 29 deletions src/core/magnolia.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import Macro.*
trait CommonDerivation[TypeClass[_]]:
type Typeclass[T] = TypeClass[T]

/**
* Must be implemented by the user of Magnolia to construct a
* typeclass for case class `T` using the provided type info.
* E.g. if we are deriving `Show[T]` typeclasses, and `T`
* is a case class `Foo(...)`, we need to constuct `Show[Foo]`.
*
* This method is called 'join' because typically it will _join_
* together the typeclasses for all the parameters of the case class,
* into a single typeclass for the case class itself. The field
* [[CaseClass.params]] can provide useful information for doing this.
*
* @param caseClass information about the case class `T`, its parameters,
* and _their_ typeclasses
*/
/** Must be implemented by the user of Magnolia to construct a typeclass for
* case class `T` using the provided type info. E.g. if we are deriving
* `Show[T]` typeclasses, and `T` is a case class `Foo(...)`, we need to
* constuct `Show[Foo]`.
*
* This method is called 'join' because typically it will _join_ together the
* typeclasses for all the parameters of the case class, into a single
* typeclass for the case class itself. The field [[CaseClass.params]] can
* provide useful information for doing this.
*
* @param caseClass
* information about the case class `T`, its parameters, and _their_
* typeclasses
*/
def join[T](caseClass: CaseClass[Typeclass, T]): Typeclass[T]

inline def derivedMirrorProduct[A](
Expand Down Expand Up @@ -77,21 +77,20 @@ trait Derivation[TypeClass[_]]
extends CommonDerivation[TypeClass]
with SealedTraitDerivation:

/**
* This must be implemented by the user of Magnolia to construct a Typeclass
* for 'T', where 'T' is a Sealed Trait or Scala 3 Enum, using the provided
* type info. E.g. if we are deriving 'Show[T]' typeclasses, and T
* is an enum 'Suit' (eg with values Diamonds, Clubs, etc), we need to
* constuct 'Show[Suit]'.
*
* This method is called 'split' because it will ''split'' the different
* possible types of the SealedTrait, and handle each one to finally
* produce a typeclass capable of handling any possible subtype of the trait.
*
* A useful function for implementing this method is [[SealedTrait#choose]],
* which can take a value instance and provide information on the specific
* subtype of the sealedTrait which that value is.
*/
/** This must be implemented by the user of Magnolia to construct a Typeclass
* for 'T', where 'T' is a Sealed Trait or Scala 3 Enum, using the provided
* type info. E.g. if we are deriving 'Show[T]' typeclasses, and T is an enum
* 'Suit' (eg with values Diamonds, Clubs, etc), we need to constuct
* 'Show[Suit]'.
*
* This method is called 'split' because it will ''split'' the different
* possible types of the SealedTrait, and handle each one to finally produce
* a typeclass capable of handling any possible subtype of the trait.
*
* A useful function for implementing this method is [[SealedTrait#choose]],
* which can take a value instance and provide information on the specific
* subtype of the sealedTrait which that value is.
*/
def split[T](sealedTrait: SealedTrait[Typeclass, T]): Typeclass[T]

transparent inline def subtypes[T, SubtypeTuple <: Tuple](
Expand Down
29 changes: 14 additions & 15 deletions src/test/SumsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ object SumsTests:
enum Size:
case S, M, L


sealed trait Sport
case object Boxing extends Sport
case class Soccer(players: Int) extends Sport
Expand Down Expand Up @@ -201,22 +200,22 @@ class SumsTests extends munit.FunSuite:
assertEquals(res, Lefty())
}

test("derive all subtypes in complex hierarchy") {
val res = Passthrough.derived[Complex].ctx.get.toOption.get
test("derive all subtypes in complex hierarchy") {
val res = Passthrough.derived[Complex].ctx.get.toOption.get

val pkg = "magnolia1.tests.SumsTests.Complex"
val expected = List(
s"$pkg.ClassH",
s"$pkg.Object",
s"$pkg.ObjectC",
s"$pkg.ObjectD",
s"$pkg.ObjectE",
s"$pkg.ObjectF",
s"$pkg.Scoped.Object",
)
val pkg = "magnolia1.tests.SumsTests.Complex"
val expected = List(
s"$pkg.ClassH",
s"$pkg.Object",
s"$pkg.ObjectC",
s"$pkg.ObjectD",
s"$pkg.ObjectE",
s"$pkg.ObjectF",
s"$pkg.Scoped.Object"
)

assertEquals(res.subtypes.map(_.typeInfo.full).toList, expected)
}
assertEquals(res.subtypes.map(_.typeInfo.full).toList, expected)
}

test("support split without join") {
val res = summon[NoCombine[Halfy]].nameOf(Righty())
Expand Down

0 comments on commit c800c94

Please sign in to comment.