January 11, 2002 Re: two questions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aaron | "Aaron" <arh14@cornell.edu> wrote in message news:3C3EF365.24975587@cornell.edu... > Well, I haven't checked out the D spec in a while, but the way Java does it, and apparently the way D also does it, is that arrays are actually mutable objects, so you can't just make them const. Making them const just means the *reference* cannot be modified, not the *contents* of the reference. There is no real way around this in Java except, as Walter says, you make the array private, and write your own getters, and simply don't provide setters. Sorry, forgot one important thing... I don't understand why arrays cannot be const? I just want to give a list of controls to user, so he may index it, get its length etc, but isn't able to add/delete/modify items. Of course this can be done with methods (these won't be settors), like: Control item(int n) { return m_items[n]; } Control itemCount() { return m_items.length; } ... for (int i = 0; i < window.itemCount(); i++) window.item(i).visible = false; However, it looks quite different from a typical D array. With const arrays this would be absolute transparent to user: const Control[] items() { return m_items; } ... for (int i = 0; i < window.items.length; i++) window.item[i].visible = true; There are some other properties that arrays have, for example "dup"... methods are required to implement them all, and it won't look like normal array. Well anyhow Walter explained why he didn't made it in D, so we have to live with it... |
Copyright © 1999-2021 by the D Language Foundation