Skip to main content
Bumped by Community user
Question Protected by gnat
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Edit correcting misunderstanding
Source Link
concat
  • 517
  • 3
  • 10

I was reading through the Hacklang docs on Collections, and came across this curious definition [paraphrased slightly]:

interface KeyedIterable<Tk, Tv> ...
interface ConstVector<+Tv> implements KeyedIterable<int, Tv>  ...

Of course, KeyedIterable must be read-only with respect to Tv, and indeed it is since its methods cannot modify its type. It could be specified to be covariant on at least Tv, but isn't.

I was wondering what impetus there could be to do this?


Edit: I read the definition wrong, KeyedIterable is actually defined as KeyedIterable<Tk, +Tv>, so this definition makes perfect sense. I'm just realizing now that it's not so much "should or should not", but rather "this is never possible".

I was reading through the Hacklang docs on Collections, and came across this curious definition [paraphrased slightly]:

interface KeyedIterable<Tk, Tv> ...
interface ConstVector<+Tv> implements KeyedIterable<int, Tv>  ...

Of course, KeyedIterable must be read-only with respect to Tv, and indeed it is since its methods cannot modify its type. It could be specified to be covariant on at least Tv, but isn't.

I was wondering what impetus there could be to do this?

I was reading through the Hacklang docs on Collections, and came across this curious definition [paraphrased slightly]:

interface KeyedIterable<Tk, Tv> ...
interface ConstVector<+Tv> implements KeyedIterable<int, Tv>  ...

Of course, KeyedIterable must be read-only with respect to Tv, and indeed it is since its methods cannot modify its type. It could be specified to be covariant on at least Tv, but isn't.

I was wondering what impetus there could be to do this?


Edit: I read the definition wrong, KeyedIterable is actually defined as KeyedIterable<Tk, +Tv>, so this definition makes perfect sense. I'm just realizing now that it's not so much "should or should not", but rather "this is never possible".

Tweeted twitter.com/StackProgrammer/status/730069372056838144
edited tags
Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683
Source Link
concat
  • 517
  • 3
  • 10

Should an interface covariant on T ever implement an interface invariant on it?

I was reading through the Hacklang docs on Collections, and came across this curious definition [paraphrased slightly]:

interface KeyedIterable<Tk, Tv> ...
interface ConstVector<+Tv> implements KeyedIterable<int, Tv>  ...

Of course, KeyedIterable must be read-only with respect to Tv, and indeed it is since its methods cannot modify its type. It could be specified to be covariant on at least Tv, but isn't.

I was wondering what impetus there could be to do this?