问题标签 [implicits]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
scala - 使用长的隐式参数列表清理签名
是否有一种优雅的解决方案可以以某种方式清理隐式参数列表,使签名更简洁?我有这样的代码:
而且我还想添加一些库功能所需的隐式参数(到 Pipeline.apply 方法),但签名已经很大了。我担心其他开发人员易于理解 - 是否有“最佳实践”方式来构建这些参数?
编辑:我的意思是隐式参数属于不同的类别。在此示例中:mapper
确保正确的内容类型,isCons
并cse
确保folder
对输入的顺序约束,并且我想添加表示业务逻辑“可行性”的隐含。它们应该如何分组,是否可以以可读的格式进行?
Edit2:是否有可能以某种方式提醒图书馆的用户,关于违反了哪个约束?例如,HList 中的类型是错误的,或者没有保持顺序约束,或者他缺乏适当的“业务逻辑”隐含?
scala - 模糊类级别和继承方法级别 ClassTag
我有一个需要带有 ClassTag 的函数的特征
现在我有一个需要扩展该特征的类,但它的 ClassTag 也用于其他方法,例如
当它被编译时,我收到一条错误消息:
有没有一种方法可以同时容纳类级别的 ClassTag 并覆盖父特征的方法?
scala - Scala 和隐式类实例化
我有以下两个 Scala 文件:
和:
}
我想知道的是,如果我必须多次调用该embelishString
函数,我会创建隐式StringWrapper
类的多个实例吗?
我担心这一点,因为当使用隐式时,它会传入一个字符串(str
)——它是我必须小心在embelishString
函数的不同调用之间共享的“状态”吗?
scala - 隐式参数中断类型推断或推断不足以解决它们?
在我添加隐式排序证据之前,类型推断在此示例中运行良好。类型推断规则(从左到右和跨参数列表)似乎得到了满足,但是关于隐式的某些东西会破坏它。
这可以编译并正常工作:
因此,任何一种类型推断都不足以进行隐式解析,或者隐式解析会破坏类型推断。
我猜想O
类型是推断出来的,但是当隐式解析发生时,它认为它是 Nothing,换句话说,它认为它好像我没有Int
在val result: Int
. 它是一个错误吗?
scala - 使用隐式限制具有抽象类型成员约束的特征方法?
我处于以下情况:
特征 Exp[T,C] 是 DSL 的基本特征和 AST,我想在这个特征中重载内置的 scala 运算符以允许在这个 dsl 上使用中缀符号,但是我想限制其中的一些方法在特征级别对类型 T 进行了限制,因此此处的相同操作 '&&' 具有不同的语义,具体取决于类型 T。
似乎莱布尼茨替代在这里不起作用/不能起作用(可能是因为它只为具有单个参数的函子 F[_] 定义):
这种限制特征 T 参数的方法是否有意义?有没有办法通过“隐藏”第二个参数 C 来使莱布尼茨在这种情况下工作:
如果这有道理吗?谢谢,
scala - Implicit resolution in descendants of associated types to avoid import tax
Note that it is not possible to create a companion object to third party type JavaRxObservable. And since my api
method must return JavaRxObservable
type because of its combinatory nature (MyObservable#map
would return JavaRxObservable
), it is a major drawback in API design because you are forcing people to read documentation that they gotta import stuff to use your api. Not even declaration in package object helps if it is meant to be API used by third parties.
function - scala隐式类方法类型不匹配在reduce与函数currying的非隐式方法中
问题:
关于隐式类的一些东西,使 reduce() 感到困惑。在隐式类中时,编译器会抱怨 reduce() 第二个参数。但是当相同的代码在非隐式方法中时,它可以编译并正常工作。
我对隐式类缺少什么?
代码:
scala - 在 Scala 中安排隐含优先级
考虑以下定义 DenseVector 和数组之间的双向隐式转换的(工作)片段:
让我们试一试:
现在另一个方向:
以下方法也有效:注意 darr。toSeq
但是,如果我们省略 .toSeq ,那么我们就有隐式冲突:
但是,我不希望明确放置“.toSeq”。
所以我的问题是:如何设置适当的优先级,以便Predef.toSeq是明显的赢家 - 总是在上面定义的自定义低优先级隐式之前尝试?
scala - 隐式搜索不指导类型推断以在 Scala 中查找超类型
在下面的代码中,我有证据R[A]
并且B
是 的子类型A
,所以我希望foo
推断出证据的类型A
并使用RA
证据。然而,scalac 拒绝这样做。
我试过这个:
但它仍然不起作用。
现在,如果我定义:
推理有效,但在我的实际代码中,我需要参考 TT。
编辑:现在我已将 A 的证据移至 A 伴生对象,此解决方案似乎不再适用。在现实环境中,证据将始终存在于伴随对象中,隐式搜索必须找到它。
另一个解决方案是让我的证据逆变,但这会给我带来很多麻烦,比如推断Nothing
和其他问题(我的实际代码比这个简化的例子更复杂)。
我怎样才能使它正常工作?
scala - Implicit resolution with covariance
Why does the following code work when Foo
is invariant, but not when it is covariant? The covariant version of Foo
produces a type error saying that in the call of useF1
, the argument has type Foo[T]
but F1
is required. A similar error is produced for useF2
.
If the variance annotation is removed from Foo
, the code works. The pattern match against F1
exposes the fact that T = Int
, so that x
has type Foo[Int]
. The implicit conversion function is used to convert Foo[Int]
to F1
in the argument of useF1
. Similarly for F2
. What part of this process is different when Foo is covariant, and why?
Although GADTs make subtyping more complicated in general, in this case the only two possible concrete types are Foo[Int]
and Foo[Unit]
, and no subtyping relationship holds between them, so subtyping shouldn't affect this example.