dmd 2.057 release
bearophile
bearophileHUGS at lycos.com
Wed Dec 14 03:13:21 PST 2011
Jacob Carlborg:
> What happened to arrays in this release:
They have closed a significant D2 hole.
As Walter has explained, this code used to compile fine and then crash at runtime:
class Foo {
void spam() {}
}
class Bar {}
void foo(Object[] a) {
a[0] = new Bar;
}
void main() {
Foo[] b = [new Foo];
foo(b);
b[0].spam(); // crash
}
Now instead it gives this at compile-time:
test.d(10): Error: function test.foo (Object[] a) is not callable using argument types (Foo[])
test.d(10): Error: cannot implicitly convert expression (b) of type Foo[] to Object[]
Now a modified foo is accepted if its 'a' argoment is constant, because you can't modify the array contents (unless you cast away const):
class Foo {
void spam() {}
}
void foo(const(Object[]) a) {
}
void main() {
Foo[] b = [new Foo];
foo(b);
}
This Java code compiles with no errors:
class Foo {
Foo() {}
void spam() {}
}
class Bar {
Bar() {}
}
class Test {
static void foo(Object[] a) {
a[0] = new Bar(); // line 10, runtime error here
}
public static void main(String[] args) {
Foo[] b = {new Foo()};
Test.foo(b);
b[0].spam(); // line 15
}
}
Instead of crashing at run-time at line 15, it raises a java.lang.ArrayStoreException at runtime at line 10, where at runtime it tests that in a[0] you put a Foo() instead of something else like a Bar() or Object(). So (in theory) the JavaVM has to perform a runtime test every time you perform an assignment of a class reference to an array cell.
Bye,
bearophile
More information about the Digitalmars-d-announce
mailing list