为什么指令上的 `map` 有时会将结果包装在元组中?

Why does `map` on a Directive sometimes wrap the result in a tuple?

下面的 example from the the Akka HTTP docucumentation 表明您可以在 Directives 上使用 map 方法来像您期望的那样转换它们的值。

val textParam: Directive1[String] =
  parameter("text".as[String])

val lengthDirective: Directive1[Int] =
  textParam.map(text => text.length)

但是,当我尝试在自己的示例中使用它时,我得到了预期类型的​​ Tuple1 指令,而不是预期类型本身的指令。例如:

val stringOpsDirective: Directive[Tuple1[StringOps]] =
  textParam.map(text => augmentString(text))

我看到 map 有一个隐含的 Tupler 参数,但查看实现可以找到该特征,我不明白为什么所有内容都没有包含在 Tuple 中。

在您使用 map 时,您声明了一个与示例中使用的声明类型略有不同但等效的 return 类型。示例 return 键入 'Directive1',您 return 键入 'Directive'。

如果您使用 Directive1,您的示例采用与文档中相同的形式:

val stringOpsDirective: Directive1[StringOps] =
  textParam.map(text => augmentString(text))

之所以有效,是因为 Directive1[_]Directive[Tuple1[_]] 的别名,定义如下:

type Directive1[T] = Directive[Tuple1[T]]

换句话说,你是对的,每个 Directive 都被参数类型化为某种 Tuple。但是,在 Tuple1.

的情况下,您可以使用类型 Directive1 在某种程度上隐藏该事实