As I remember, it was pointed out that rules for implicit conversions of arrays are formulated uniformly for complex and primitive types, and this report is exactly about those rules, and solution for classes works for primitive types.
Comment #6 by jason.james.house — 2009-02-18T22:06:34Z
I don't know if it's wise to mark bug 2544 as a duplicate of this bug. For example, the proposed solution (comment 3) shows a lack of understanding of this related issue.
Specifically, I believe the proposed solution in bug 2412 comment 3 says the following would be ok:
class base{}
class derived:base{}
derived[] x = new derived[2];
const(base)[] safe = x;
That really isn't true. Take the following code that should resize the arrays in place:
derived[] x = new derived[2];
derived.length = 1;
const(base)[] safe = x;
x ~= new derived(); // x[1] is now valid derived instance
safe ~= new base(); // just overwrote x[1] with a non-derived instance
Comment #7 by dfj1esp02 — 2009-02-19T03:01:02Z
isn't this bug 2093?
Comment #8 by dfj1esp02 — 2009-02-19T03:09:50Z
int[] x=[1,2];
x.length=1;
int[] y=x;
x~=3; // x=[1,3]
y~=4; // x=[1,4]
bug 2093 has little to do with covariance. Added as dependency.
*** Issue 3977 has been marked as a duplicate of this issue. ***
Comment #11 by bearophile_hugs — 2010-09-28T18:36:09Z
I think in this case runtime tests in nonrelease builds are better than nothing.
Comment #12 by smjg — 2010-09-29T02:47:57Z
(In reply to comment #11)
> I think in this case runtime tests in nonrelease builds are better than
> nothing.
In what case? And what would these runtime tests do?
Comment #13 by bearophile_hugs — 2010-11-16T17:04:34Z
(In reply to comment #11)
> I think in this case runtime tests in nonrelease builds are better than
> nothing.
I'm afraid, there's nothing to test at runtime, and I thought the solution was already chosen to disallow mutable covariance at compile time. I remember there was an attempt to implement the solution, but Steven says the code still compiles.
Comment #15 by bruno.do.medeiros+deebugz — 2010-11-17T12:24:40Z
For the record, the same problem also occurs with pointer types:
B* ba=[new B()].ptr;
A* aa=ba;
*aa=new A;
(*ba).methodB(); // (*ba) is expected to be B, but is A
Comment #16 by bearophile_hugs — 2010-11-17T15:13:10Z
(In reply to comment #14)
> I'm afraid, there's nothing to test at runtime,
Some runtime data info may be added, then. There is already some of it for classes and modules.
> and I thought the solution was
> already chosen to disallow mutable covariance at compile time.
I didn't know this.
Comment #17 by issues.dlang — 2010-11-17T15:27:51Z
It really should be stopped at compile time. There's not really a good reason to allow it. As much as it first looks like mixing A[] and B[] when B : A should work, it's a _really_ bad idea. Just because a container holds a type which is the base type of another type does not mean that a container which holds the derived type should be assignable/castable/convertable to one which holds the base type.
Really, the only question is whether you can get away with it with some form of const, and I believe that the consensus on it in the newsgroup last time that this was discussed was that you couldn't. I'd have to go digging through the archives though to find the exact thread.
This can and should be disallowed at compile time. It's a definite bug. It just hasn't been fixed yet.
Comment #18 by smjg — 2010-11-17T16:57:33Z
(In reply to comment #17)
> Really, the only question is whether you can get away with it with
> some form of const, and I believe that the consensus on it in the
> newsgroup last time that this was discussed was that you couldn't.
> I'd have to go digging through the archives though to find the
> exact thread.
I've no idea what discussion you're thinking of either. But I've studied it - see comment 4. But to summarise, the following implicit conversions should be allowed:
B[] to const(A)[]
const(B)[] to const(A)[]
immutable(B)[] to immutable(A)[]
immutable(B)[] to const(A)[]
> This can and should be disallowed at compile time. It's a definite
> bug. It just hasn't been fixed yet.
Yes, in the spec.
Comment #19 by bruno.do.medeiros+deebugz — 2010-11-18T11:10:49Z
I've looked at Stewart Gordon's proposal, and I agree that they are safe and sound (although it may need to be more detailed or cleaned-up a bit). I actually had prepared a post over a month ago detailing what is basically the same proposal and underlying conclusions as Stewart's proposal. I did it when I came across the code sample in bug #2544, but before I read Stewart's proposal (which I read only recently). I didn't actually post the text I prepared yet, since I was waiting to clear up my backlog of D newsgroups message. :S
In any case, the conclusions are the same, especially to the point of realizing the connection to Java's wildcard generics (which, BTW, are the only way to express this use case safely, but without further loss of type system functionality).
As a simple solution, I recommend we adopt Stewart's proposal, which is good enough I think.
The very best solution would be to have a concept like Java's wildcard's, but that is too complex in implementation to consider any time soon.
@bearophile:
"Some runtime data info may be added, then. There is already some of it for
classes and modules."
Are you out of your mind? Classes are not like arrays and pointers. These are supposed to be lightweight data types, it's out of place for D to have that extra runtime data in these lightweight data types (arrays and pointers). It worries me that you suggested this change without even considering an approach based on fixing/improving the (static) type system.
Comment #20 by bearophile_hugs — 2010-11-18T12:38:47Z
(In reply to comment #19)
> Classes are not like arrays and pointers. These are
> supposed to be lightweight data types, it's out of place for D to have that
> extra runtime data in these lightweight data types (arrays and pointers). It
> worries me that you suggested this change without even considering an approach
> based on fixing/improving the (static) type system.
A solution based on the type system (plus a bit of syntax) as in C#/Java seems better. But it's probably better to implement it after the current group of features is implemented.
Comment #21 by bruno.do.medeiros+deebugz — 2010-11-19T15:29:24Z
(In reply to comment #20)
> (In reply to comment #19)
>
> > Classes are not like arrays and pointers. These are
> > supposed to be lightweight data types, it's out of place for D to have that
> > extra runtime data in these lightweight data types (arrays and pointers). It
> > worries me that you suggested this change without even considering an approach
> > based on fixing/improving the (static) type system.
>
> A solution based on the type system (plus a bit of syntax) as in C#/Java seems
> better. But it's probably better to implement it after the current group of
> features is implemented.
Yes, like I mentioned, Steven's solution has some limitations. For example, if you have B[] the best you have is being allowed to cast it to const(A)[]. But that means you wont be able to call mutable methods on the elements of that array, whereas with a wildcard type such as:
<? extends A>[]
you would be able to call mutable methods of A. So this solution is not perfect, but it works with existing concepts in D (it only needs const), whereas we are far away from the point where can add such disrupting and complex features such as wildcard type. (D's type system seems to be struggling already as it is)
Comment #22 by bruno.do.medeiros+deebugz — 2010-11-19T15:31:03Z
> Yes, like I mentioned, Steven's solution has some limitations.
I meant Stewart (Gordon), not Steven.
Comment #23 by dfj1esp02 — 2010-11-20T14:30:34Z
> <? extends A>[]
Interesting. This type implies array is mutable, so you can put objects into it. Java will check at runtime for array type, but in D arrays don't have rtti.
Comment #24 by smjg — 2010-11-21T17:26:38Z
(In reply to comment #23)
> > <? extends A>[]
>
> Interesting. This type implies array is mutable, so you can put objects into
> it.
No, because you don't know which subclass of A the particular array is of. Java does exactly this with its generics - prevents you at compile-time from putting objects into the data structure for this reason. All Java generics do is compile-time type checking - at run-time, a List is a List. But arrays are different.
But each element of the array is mutable.
> Java will check at runtime for array type, but in D arrays don't have rtti.
Yes, because Java arrays don't use generics. Probably for backward compatibility, since they were invented long before generics were. They're just convertible to arrays of any class higher up the hierarchy. This has been claimed as one of the main weaknesses of Java's design.
Comment #25 by andrei — 2010-11-27T05:58:02Z
Elevating importance of this as it's a big hole in the type system. Please vote up.
Solution: Derived[] can be implicitly converted to const(Base)[] in all situation where pointer adjustment is not needed for converting Derived to Base. Conversion from Derived[] to Base[] must be rejected statically.
Comment #26 by jason.james.house — 2010-11-27T06:24:58Z
If derived[] is implicitly converted to const(base)[], what happens when I append another bade object to the array? Should it implicitly convert to const(base[])?
Comment #27 by andrei — 2010-11-27T06:32:03Z
(In reply to comment #26)
> If derived[] is implicitly converted to const(base)[], what happens when I
> append another bade object to the array? Should it implicitly convert to
> const(base[])?
You can't append a bad object to a const array.
Comment #28 by andrei — 2010-11-27T10:10:58Z
(In reply to comment #27)
> (In reply to comment #26)
> > If derived[] is implicitly converted to const(base)[], what happens when I
> > append another bade object to the array? Should it implicitly convert to
> > const(base[])?
>
> You can't append a bad object to a const array.
Ah, you mean "base" not "bad". Sorry! Conversion Derived[] -> const(Base)[] is fine because the resulting array is not an lvalue.
Comment #29 by jason.james.house — 2010-11-27T12:08:04Z
Doesn't the following make x an lvalue?
const(base)[] x = derived_array;
Comment #30 by smjg — 2010-11-27T12:20:15Z
(In reply to comment #29)
> Doesn't the following make x an lvalue?
>
> const(base)[] x = derived_array;
It does ... but appending to x won't alter derived_array. There was, however, previously a hole here that was issue 2093.