Two question about array
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sun Dec 30 06:32:16 PST 2007
"Alex" <faint_u at 163.com> wrote in message
news:fl88r1$1olc$1 at digitalmars.com...
> 1. Is array a class derived from Object? If yes, why i get error message
> with the following code:
> int[] array;
> Object obj = array;
> If not, is there any general type can represent either an Integer, or an
> Array? Or a character string?
Arrays are not objects. They are arrays :) They are their own type and are
not related to any other type.
If you need an integer or an array you need some kind of variant type. If
you're using Tango, there's tango.core.Variant; if you're using Phobos with
D1, there's std.boxer; and Phobos with D2 has deprecated std.boxer in favor
of std.variant.
> 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?
It's because these are really syntactic sugar for function calls.
"simpleArray.length = 5" is the same as "simpleArray.length(5)". If you try
to do "simpleArray.length *= 2" you're basically writing
"simpleArray.length() *= 2", which obviously makes no sense. The same thing
would happen with class properties.
It's dumb. I know.
More information about the Digitalmars-d-learn
mailing list