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
Currently, for newtypes we generate a Coercible[O, N] instance where O is the original type and N is the newtype. While this is perfectly fine, we go a step further and generate a Coercible[F[O], F[N]] instance for anyF[_]. This is where we get into trouble. There are cases where this shouldn't be permitted due to the nature of the data structure we're dealing with. For example, let's look at how this would work with dogs.Set which relies on an implicit Order instance for its operations -
importcats.instances.int._importcats.Orderimportio.estatico.newtype.ops._importio.estatico.newtype.macros.newtype// Like Int except ordered in reverse.@newtype caseclassRevInt(value: Int)
objectRevInt {
implicitvalorder:Order[RevInt] =Order.from((x, y) =>-Order[Int].compare(x.value, y.value))
}
// Build a dogs.Set[Int]
println(dogs.Set(1, 2, 3))
// Set(1,2,3)// Build a dogs.Set[RevInt]
println(dogs.Set(RevInt(1), RevInt(2), RevInt(3)))
// Set(3,2,1)// Build a dogs.Set[Int], coerce it to dogs.Set[RevInt], and add an element
println(dogs.Set(1, 2).coerce[dogs.Set[RevInt]] +RevInt(3))
// Set(3,1,2)
One quick and dirty way to deal with this would be to introduce type roles via a type class.
macro? should be a little less quick, more dirty, but you're sweeping the dirt under the rug. although im not sure if it'll be a better (or worse i guess?) maid than what you have there. but it might upgrade you to an old/quite poor vacuum cleaner at least i.e.: it can generate some of that boilerplate you have if this or some variation is an acceptable encoding for this which it is as far as i can tell.
some thing to consider for posterities sake is how a higher kinded Coercible in general would play with partial unification related things, like if you have an Either you want to type lambda to a kind F[_] you'd have to probably use type projector? in which case, if you went down the compiler plugin road, itd might have to integrate with it, lets say. this is super far down the line concern though
Currently, for newtypes we generate a
Coercible[O, N]
instance whereO
is the original type andN
is the newtype. While this is perfectly fine, we go a step further and generate aCoercible[F[O], F[N]]
instance for anyF[_]
. This is where we get into trouble. There are cases where this shouldn't be permitted due to the nature of the data structure we're dealing with. For example, let's look at how this would work with dogs.Set which relies on an implicitOrder
instance for its operations -One quick and dirty way to deal with this would be to introduce type roles via a type class.
Then we'd define this Coercible instance based on the type role -
We then need to define type role instances -
See -
The text was updated successfully, but these errors were encountered: