问题
In Scala 2.10, given class Foo[F[_]]
, I can't write
scala> x.isInstanceOf[Foo[_]]
<console>:10: error: _$1 takes no type parameters, expected: one
x.isInstanceOf[Foo[_]]
^
or
scala> x.isInstanceOf[Foo[_[_]]]
<console>:11: error: _$1 does not take type parameters
x.isInstanceOf[Foo[_[_]]]
^
I can write x.isInstanceOf[Foo[F] forSome { type F[_]] }
, which gives an unchecked warning. I've tried placing @unchecked
annotation in different places, but none of them work:
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}]
^
Is there any way to write this existential type without a warning?
回答1:
With pattern matching you can keep the warnings away:
x match {case _: Foo[_] => ???}
It is also a bit less verbose in my opinion. In case you name the case
variable (starting with lower case letter or escaped with back quotes, ie. not _
as in the above example before :
), you already have an asInstanceOf
.
回答2:
Kind of guessing:
$ 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
The pop-up just told me code blocks are not very informative.
Oh, and s/guessing/experimenting
.
来源:https://stackoverflow.com/questions/28176807/suppressing-unchecked-warning-for-a-higher-kinded-existential-type