gpt4 book ai didi

scala - 为更高种类的存在类型抑制 @unchecked 警告

转载 作者:行者123 更新时间:2023-12-04 20:39:54 25 4
gpt4 key购买 nike

在 Scala 2.10 中,给定 class Foo[F[_]] ,我不会写

scala> x.isInstanceOf[Foo[_]]
<console>:10: error: _$1 takes no type parameters, expected: one
x.isInstanceOf[Foo[_]]
^

或者
scala> x.isInstanceOf[Foo[_[_]]]
<console>:11: error: _$1 does not take type parameters
x.isInstanceOf[Foo[_[_]]]
^

我可以写 x.isInstanceOf[Foo[F] forSome { type F[_]] } ,它给出了一个未经检查的警告。我试过放置 @unchecked注释在不同的地方,但它们都不起作用:
scala> x.isInstanceOf[Foo[H] @unchecked forSome {type H[_]}]
<console>:11: warning: abstract type H in type Foo[H] @unchecked forSome { type H[_] <: Any } is unchecked since it is eliminated by erasure
x.isInstanceOf[Foo[H] @unchecked forSome {type H[_]}]
^

scala> x.isInstanceOf[Foo[H @unchecked] forSome {type H[_]}]
<console>:11: warning: abstract type H in type Foo[H @unchecked] is unchecked since it is eliminated by erasure
x.isInstanceOf[Foo[H @unchecked] forSome {type H[_]}]
^
<console>:11: error: kinds of the type arguments (? @unchecked) do not conform to the expected kinds of the type parameters (type F) in class Foo.
? @unchecked's type parameters do not match type F's expected parameters:
<none> has no type parameters, but type F has one
x.isInstanceOf[Foo[H @unchecked] forSome {type H[_]}]
^

scala> x.isInstanceOf[Foo[H] forSome {type H[_] @unchecked}]
<console>:1: error: `=', `>:', or `<:' expected
x.isInstanceOf[Foo[H] forSome {type H[_] @unchecked}]
^

有没有办法在没有警告的情况下编写这种存在类型?

最佳答案

一种猜测:

$ scala210 -language:_
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.7.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class Foo[F[_]]
defined class Foo

scala> (null: Any).isInstanceOf[(Foo[F] forSome { type F[_] }) @unchecked]
res0: Boolean = false

弹出窗口只是告诉我代码块不是很丰富。

哦,还有 s/guessing/experimenting .

关于scala - 为更高种类的存在类型抑制 @unchecked 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28176807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com