stuck on "Incorrect type in assignment" error
Steve Yegge <[email protected]> Sun, 6 Mar 2005 12:40:29 -0800
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
I'm trying to get a rather large Nice program written in a hurry,
and I keep hitting strange stumbling blocks. My approach so far
has been to gnash my teeth, wail, pull my hair, and try dozens of
different approaches until one gets past the compiler. But it's
going too slowly, so I'm going to start posting my questions here,
in the hopes that some Nice people will help.
First question, then: why doesn't this code work --
void main(String[] args) {
let int w = 10;
let int h = 5;
let Integer[][] grid = new Integer[w][h];
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
grid[i][j] = new Integer(i+j);
}
}
}
The compiler says:
c:\nice\hello\main.nice: line 5, column 19:
Incorrect type in assignment to grid
Found : ?java.lang.Integer[][]
Expected: java.lang.Integer[][]
compilation failed with 1 error
If I change the grid declaration to this:
let Integer[][] grid = new Integer[0][0];
it compiles, then of course throws an ArrayIndexOutOfBoundsException.
(Frankly, if Nice can't prevent run-time exceptions, then I don't believe it
should make you work so hard to prevent null-pointer exceptions. It's
really and truly not worth the hassle, as far as I can tell. The language has
a lot to offer, and right now this little issue just gets in your way. Optional
types should be, well... optional.)
I'm going to be returning this grid from a function, and
I don't want to declare it as an ?Integer[][], because I know
that neither the grid nor any of the elements will be null.
So I don't want the caller to have to mess with null checks.
How do I declare the grid and/or initialize it in a way that avoids
the need for the option-type?
Incidentally, is it complaining because it thinks the grid reference
could be null, or that one of its child elements could be null, or
one of the child-child-elements? There are three different types
here: the grid reference, its children, and their children. Which
one is the question-mark referring to? The User Manual could stand
some expansion of the Arrays section, to include information about
multi-dimensional arrays and arrays of non-primitive types.
Thanks,
Steve Yegge
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click