Two question about array
Derek Parnell
derek at psych.ward
Sun Dec 30 06:31:10 PST 2007
On Sun, 30 Dec 2007 09:09:37 -0500, Alex wrote:
> 1. Is array a class derived from Object? If yes, why i get error message with the following code:
> int[] array;
> Object obj = array;
No, an array is not a class.
> If not, is there any general type can represent either an Integer, or an Array? Or a character string?
Have a look at the std.boxer module, that might be what you are looking
for.
> 2. simpleArray.length = simpleArray.length * 2 can pass the compile but simpleArray.length *= 2 can't. The compiler complains "simpleArray.length is not an lvalue", what's the problem?
This is a current restriction in D.
The reason is that the .length is a property (albeit a built-in one) and
properties are implemented as function calls. This means that ...
simpleArray.length *= 2
is equivalent to ...
get_length(simpleArray) *= 2
which as you can imagine is not going to do what you want. Whereas ...
simpleArray.length = simpleArray.length * 2
is equivalent to ...
set_length(simpleArray, get_length(simpleArray) * 2 )
Hopefully, this is one of the restrictions that can be removed soon.
This restriction applies to user defined properties too.
For example:
class Foo
{
float m_Value = 0;
int val() { return cast(int)m_Value; }
void val( int x) { m_Value = cast(float)x); }
}
Foo f = new Foo;
int a;
a = f.val; // okay
a += 2;
f.val = a; // okay;
f.val += 2; // Not okay.
--
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
More information about the Digitalmars-d-learn
mailing list