[scala] Soundness of contravariant bounds

View: New views
4 Messages — Rating Filter:   Alert me  

[scala] Soundness of contravariant bounds

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am curious why the following code is rejected by Scala compiler:

////////////////////////////
trait A[-T, S <: T]

////////////////////////////

Would it be possible to do smth non-typesafe, if it were not rejected? Can anybody provide an example?

Thanks,
Vladimir

[scala] Re: Soundness of contravariant bounds

by Adriaan Moors-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think this is done so that we don't have to check bounds when subsuming a type to a supertype:

looking only at variance, this holds: A[Any, String] <: A[Nothing, String] 
however, A[Nothing, String] is ill-formed, since String is not a subtype of Nothing of course

I assume you could construct something that goes wrong at run time this way, but haven't got time right now to construct an example.

cheers
adriaan

On Tue, Oct 27, 2009 at 4:47 PM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi,

I am curious why the following code is rejected by Scala compiler:

////////////////////////////
trait A[-T, S <: T]

////////////////////////////

Would it be possible to do smth non-typesafe, if it were not rejected? Can anybody provide an example?

Thanks,
Vladimir

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm for more information.


Re: [scala] Soundness of contravariant bounds

by Thomas Chust :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/27 Vladimir Reshetnikov <v.reshetnikov@...>:
> [...]
> I am curious why the following code is rejected by Scala compiler:
> ////////////////////////////
> trait A[-T, S <: T]
> ////////////////////////////
> [...]

Hello,

since T is contravariant and unbounded, you can always narrow its type
as you like but there is no guarantee that the type bound on S would
still be fulfilled then. The only possible solution would be to force
S = Nothing.

Depending on what you actually want to do, the solution could be as
simple as moving the type bound to the variant type parameter T, which
works just fine:

  trait A[-T >: S, S]

Ciao,
Thomas


--
When C++ is your hammer, every problem looks like your thumb.

[scala] Re: Soundness of contravariant bounds

by nikov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Contravariant type parameters are also introduced in C# 4.0, and C# compiler does allow this kind of declarations. I could not devise any example that would demonstrate that it is not type-safe. So I wonder maybe Scala community knows one. It is better to know this before C# 4.0 releases. But there can be difference between C# and Scala because C# allows variant type parameters only on interfaces that do not contain any executable code and on delegates.

Thanks,
Vladimir

On Tue, Oct 27, 2009 at 8:10 PM, Adriaan Moors <adriaan.moors@...> wrote:
I think this is done so that we don't have to check bounds when subsuming a type to a supertype:

looking only at variance, this holds: A[Any, String] <: A[Nothing, String] 
however, A[Nothing, String] is ill-formed, since String is not a subtype of Nothing of course

I assume you could construct something that goes wrong at run time this way, but haven't got time right now to construct an example.

cheers
adriaan

On Tue, Oct 27, 2009 at 4:47 PM, Vladimir Reshetnikov <v.reshetnikov@...> wrote:
Hi,

I am curious why the following code is rejected by Scala compiler:

////////////////////////////
trait A[-T, S <: T]

////////////////////////////

Would it be possible to do smth non-typesafe, if it were not rejected? Can anybody provide an example?

Thanks,
Vladimir

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm for more information.