covariant scala to invariant java

91 views
Skip to first unread message

Volodymyr Kyrychenko

unread,
Feb 28, 2011, 1:49:08 PM2/28/11
to scala-language
Hi, All

Is there the way to make the following work?

scala> trait X[+T] { def cls:Class[T] }
<console>:5: error: covariant type T occurs in invariant position in
type => Class[T] of method cls
trait X[+T] { def cls:Class[T]}
^

Kevin Wright

unread,
Feb 28, 2011, 1:59:48 PM2/28/11
to scala-l...@googlegroups.com

trait X[+T] { def cls[U >: T]: Class[U] }

Not easy typing that on a phone :)

Volodymyr Kyrychenko

unread,
Feb 28, 2011, 2:11:55 PM2/28/11
to scala-l...@googlegroups.com
On Mon, Feb 28, 2011 at 8:59 PM, Kevin Wright <kev.lee...@gmail.com> wrote:
> trait X[+T] { def cls[U >: T]: Class[U] }

And any luck with val instead of def?

> Not easy typing that on a phone :)

:)

--
Best Regards,
Volodymyr Kyrychenko

Jason Zaugg

unread,
Feb 28, 2011, 2:56:06 PM2/28/11
to scala-l...@googlegroups.com, Volodymyr Kyrychenko

scala> import annotation.unchecked._
import annotation.unchecked._

scala> trait X[+T] { def cls:Class[T @uncheckedVariance] }
defined trait X

http://stackoverflow.com/questions/2454281/when-is-uncheckedvariance-needed-in-scala-and-why-is-it-used-in-generictraversa

-jason

Reply all
Reply to author
Forward
0 new messages