在Scala中,我需要创建一个&表示复合值的产品类型,例如:
val and: String & Int & User & ... = ???
Run Code Online (Sandbox Code Playgroud)
即and应该有一个String零件,Int零件和User零件.这类似于Scala with关键字:
val and: String with Int with User with ... = ???
Run Code Online (Sandbox Code Playgroud)
拥有这样的产品类型我需要一种方法,有一个功能A => A,将它应用于某些产品价值,并通过A部分改变来恢复该产品.这意味着产品中的每种类型都必须是唯一的 - 这是可以接受的.
一个重要的限制是,当将一个功能A => A应用于产品时,我只知道产品在A某个内部,但没有关于它所包含的其他类型的信息.但作为函数的调用者,我将其传递给具有完整类型信息的产品,并希望将此完整类型作为函数签名的一部分返回.
在伪代码中:
def update[A, Rest](product: A & Rest, f: A => A): A & Rest
Run Code Online (Sandbox Code Playgroud)
使用Shapeless或其他深奥的东西对我来说没问题.我尝试使用HLists但它们是有序的,而像异构集这样的东西在这里更适合代表A & Rest部分.
更新:
以下代码解决了我从RégisJean-Gilles回答的用例,下面添加了阅读支持,一些评论和改进的类型安全:
object product {
/** Product of `left` and `right` values. */
case class &[L, R](left: L, right: R)
implicit class AndPimp[L](val left: L) extends AnyVal {
/** Make a product of `this` (as left) and `right`. */
def &[R](right: R): L & R = new &(left, right)
}
/* Updater. */
/** Product updater able to update value of type `A`. */
trait ProductUpdater[P, A] {
/** Update product value of type `A`.
* @return updated product */
def update(product: P, f: A ? A): P
}
trait LowPriorityProductUpdater {
/** Non-product value updater. */
implicit def valueUpdater[A]: ProductUpdater[A, A] = new ProductUpdater[A, A] {
override def update(product: A, f: A ? A): A = f(product)
}
}
object ProductUpdater extends LowPriorityProductUpdater {
/** Left-biased product value updater. */
implicit def leftProductUpdater[L, R, A](implicit leftUpdater: ProductUpdater[L, A]): ProductUpdater[L & R, A] =
new ProductUpdater[L & R, A] {
override def update(product: L & R, f: A ? A): L & R =
leftUpdater.update(product.left, f) & product.right
}
/** Right-biased product value updater. */
implicit def rightProductUpdater[L, R, A](implicit rightUpdater: ProductUpdater[R, A]): ProductUpdater[L & R, A] =
new ProductUpdater[L & R, A] {
override def update(product: L & R, f: A ? A): L & R =
product.left & rightUpdater.update(product.right, f)
}
}
/** Update product value of type `A` with function `f`.
* Won't compile if product contains multiple `A` values.
* @return updated product */
def update[P, A](product: P)(f: A ? A)(implicit updater: ProductUpdater[P, A]): P =
updater.update(product, f)
/* Reader. */
/** Product reader able to read value of type `A`. */
trait ProductReader[P, A] {
/** Read product value of type `A`. */
def read(product: P): A
}
trait LowPriorityProductReader {
/** Non-product value reader. */
implicit def valueReader[A]: ProductReader[A, A] = new ProductReader[A, A] {
override def read(product: A): A = product
}
}
object ProductReader extends LowPriorityProductReader {
/** Left-biased product value reader. */
implicit def leftProductReader[L, R, A](implicit leftReader: ProductReader[L, A]): ProductReader[L & R, A] =
new ProductReader[L & R, A] {
override def read(product: L & R): A =
leftReader.read(product.left)
}
/** Right-biased product value reader. */
implicit def rightProductReader[L, R, A](implicit rightReader: ProductReader[R, A]): ProductReader[L & R, A] =
new ProductReader[L & R, A] {
override def read(product: L & R): A =
rightReader.read(product.right)
}
}
/** Read product value of type `A`.
* Won't compile if product contains multiple `A` values.
* @return value of type `A` */
def read[P, A](product: P)(implicit productReader: ProductReader[P, A]): A =
productReader.read(product)
// let's test it
val p = 1 & 2.0 & "three"
read[Int & Double & String, Int](p) // 1
read[Int & Double & String, Double](p) // 2.0
read[Int & Double & String, String](p) // three
update[Int & Double & String, Int](p)(_ * 2) // 2 & 2.0 & three
update[Int & Double & String, Double](p)(_ * 2) // 1 & 4.0 & three
update[Int & Double & String, String](p)(_ * 2) // 1 & 2.0 & threethree
}
Run Code Online (Sandbox Code Playgroud)
这是一个仅使用纯 scala 的解决方案,不需要任何库。它依赖于使用相当标准方法的类型类:
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class &[L,R](left: L, right: R)
implicit class AndOp[L](val left: L) {
def &[R](right: R): L & R = new &(left, right)
}
trait ProductUpdater[P,A] {
def apply(p: P, f: A => A): P
}
trait LowPriorityProductUpdater {
implicit def noopValueUpdater[P,A]: ProductUpdater[P,A] = {
new ProductUpdater[P,A] {
def apply(p: P, f: A => A): P = p // keep as is
}
}
}
object ProductUpdater extends LowPriorityProductUpdater {
implicit def simpleValueUpdater[A]: ProductUpdater[A,A] = {
new ProductUpdater[A,A] {
def apply(p: A, f: A => A): A = f(p)
}
}
implicit def productUpdater[L, R, A](
implicit leftUpdater: ProductUpdater[L, A], rightUpdater: ProductUpdater[R, A]
): ProductUpdater[L & R, A] = {
new ProductUpdater[L & R, A] {
def apply(p: L & R, f: A => A): L & R = &(leftUpdater(p.left, f), rightUpdater(p.right, f))
}
}
}
def update[A,P](product: P)(f: A => A)(implicit updater: ProductUpdater[P,A]): P = updater(product, f)
// Exiting paste mode, now interpreting.
Run Code Online (Sandbox Code Playgroud)
我们来测试一下:
scala> case class User(name: String, age: Int)
defined class User
scala> val p: String & Int & User & String = "hello" & 123 & User("Elwood", 25) & "bye"
p: &[&[&[String,Int],User],String] = &(&(&(hello,123),User(Elwood,25)),bye)
scala> update(p){ i: Int => i + 1 }
res0: &[&[&[String,Int],User],String] = &(&(&(hello,124),User(Elwood,25)),bye)
scala> update(p){ s: String => s.toUpperCase }
res1: &[&[&[String,Int],User],String] = &(&(&(HELLO,123),User(Elwood,25)),BYE)
scala> update(p){ user: User =>
| user.copy(name = user.name.toUpperCase, age = user.age*2)
| }
res2: &[&[&[String,Int],User],String] = &(&(&(hello,123),User(ELWOOD,50)),bye)
Run Code Online (Sandbox Code Playgroud)
更新:回应:
当产品不包含要更新的值时,是否可以使其不编译
是的,这绝对是可能的。我们可以更改ProductUpdater类型类,但在这种情况下,我发现引入一个单独的类型类作为给定产品至少包含一个类型元素的ProductContainsType证据要容易得多:PA
scala> :paste
// Entering paste mode (ctrl-D to finish)
@annotation.implicitNotFound("Product ${P} does not contain type ${A}")
abstract sealed class ProductContainsType[P,A]
trait LowPriorityProductContainsType {
implicit def compositeProductContainsTypeInRightPart[L, R, A](
implicit rightContainsType: ProductContainsType[R, A]
): ProductContainsType[L & R, A] = null
}
object ProductContainsType extends LowPriorityProductContainsType {
implicit def simpleProductContainsType[A]: ProductContainsType[A,A] = null
implicit def compositeProductContainsTypeInLeftPart[L, R, A](
implicit leftContainsType: ProductContainsType[L, A]
): ProductContainsType[L & R, A] = null
}
// Exiting paste mode, now interpreting.
Run Code Online (Sandbox Code Playgroud)
现在我们可以定义更严格的update方法:
def strictUpdate[A,P](product: P)(f: A => A)(
implicit
updater: ProductUpdater[P,A],
containsType: ProductContainsType[P,A]
): P = updater(product, f)
Run Code Online (Sandbox Code Playgroud)
让我们来看看:
scala> strictUpdate(p){ s: String => s.toUpperCase }
res21: &[&[&[String,Int],User],String] = &(&(&(HELLO,123),User(Elwood,25)),BYE)
scala> strictUpdate(p){ s: Symbol => Symbol(s.name.toUpperCase) }
<console>:19: error: Product &[&[&[String,Int],User],String] does not contain type Symbol
strictUpdate(p){ s: Symbol => Symbol(s.name.toUpperCase) }
Run Code Online (Sandbox Code Playgroud)