www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Two question about array

reply Alex <faint_u 163.com> writes:
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?

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?
Dec 30 2007
next sibling parent reply Derek Parnell <derek psych.ward> writes:
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
Dec 30 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:7zy1pvtteo79$.1ligslws4mjpl.dlg 40tude.net...

RRRRRRGH TIE 
Dec 30 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Alex" <faint_u 163.com> wrote in message 
news:fl88r1$1olc$1 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.
Dec 30 2007
parent reply Alex <faint_u 163.com> writes:
I'm trying to use Variant but got error message with the following code:
-------------------------------------------
import std.variant;

void main()
{
	Variant v;
	v = 4;
}

-----------------------------
error message:

E:\Tools\Lang\D>dmd test.d
e:\Tools\Lang\d\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 8.00.1
Copyright (C) Digital Mars 1989-2004  All rights reserved.
test.obj(test)
 Error 42: Symbol Undefined
_D3std7variant17__T8VariantNVk20Z8VariantN4typeMFZC8TypeInfo
test.obj(test)
 Error 42: Symbol Undefined _D3std7variant17__T8VariantNVk20Z8VariantN6__initZ
--- errorlevel 2

What's the problem?

Jarrett Billingsley Wrote:

 "Alex" <faint_u 163.com> wrote in message 
 news:fl88r1$1olc$1 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.
Dec 31 2007
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Alex" <faint_u 163.com> wrote in message 
news:flaqh4$14v7$1 digitalmars.com...
 I'm trying to use Variant but got error message with the following code:
 -------------------------------------------
 import std.variant;

 void main()
 {
 Variant v;
 v = 4;
 }

 -----------------------------
 error message:

 E:\Tools\Lang\D>dmd test.d
 e:\Tools\Lang\d\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 8.00.1
 Copyright (C) Digital Mars 1989-2004  All rights reserved.
 test.obj(test)
 Error 42: Symbol Undefined 
 _D3std7variant17__T8VariantNVk20Z8VariantN4typeMFZC8TypeInfo
 test.obj(test)
 Error 42: Symbol Undefined 
 _D3std7variant17__T8VariantNVk20Z8VariantN6__initZ
 --- errorlevel 2

 What's the problem?
I've never used Phobos' std.variant, but it looks like a typical issue with DMD, OPTLINK, precompiled libraries and templates (that is, DMD+OPTLINK usually fall flat on their face when trying to use templates from a library that's been precompiled). I don't know how to make it work :\
Dec 31 2007
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 31 Dec 2007 08:23:48 -0500, Alex wrote:

 I'm trying to use Variant but got error message with the following code:
 -------------------------------------------
 import std.variant;
 
 void main()
 {
 	Variant v;
 	v = 4;
 }
 
 -----------------------------
 error message:
 
 E:\Tools\Lang\D>dmd test.d
 e:\Tools\Lang\d\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 8.00.1
 Copyright (C) Digital Mars 1989-2004  All rights reserved.
 test.obj(test)
  Error 42: Symbol Undefined
_D3std7variant17__T8VariantNVk20Z8VariantN4typeMFZC8TypeInfo
 test.obj(test)
  Error 42: Symbol Undefined _D3std7variant17__T8VariantNVk20Z8VariantN6__initZ
 --- errorlevel 2
 
 What's the problem?
Which version of the DMD comiler are you using? When I tried your example with version 2.008 it worked... --------------- C:\temp>type test.d import std.stdio; import std.variant; void main() { Variant v; v = 4; writefln("%s", v); } C:\temp>dmd test.d c:\dmd\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi; C:\temp>test 4 C:\temp>dmd Digital Mars D Compiler v2.008 ---------------- -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 31 2007
parent reply Alex <faint_u 163.com> writes:
The error occured with dmd 2.007. 
I just tried 2.008 and it works now. BTW, where is the download page for latest
dmd compiler? The one in page http://www.digitalmars.com/d/dcompiler.html is
still version 2.007. I replaced the 2.007 to 2.008 in the URL to get the
correct version. But i think there should be a web page always listing the
latest version.

Derek Parnell Wrote:

 On Mon, 31 Dec 2007 08:23:48 -0500, Alex wrote:
 
 I'm trying to use Variant but got error message with the following code:
 -------------------------------------------
 import std.variant;
 
 void main()
 {
 	Variant v;
 	v = 4;
 }
 
 -----------------------------
 error message:
 
 E:\Tools\Lang\D>dmd test.d
 e:\Tools\Lang\d\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 8.00.1
 Copyright (C) Digital Mars 1989-2004  All rights reserved.
 test.obj(test)
  Error 42: Symbol Undefined
_D3std7variant17__T8VariantNVk20Z8VariantN4typeMFZC8TypeInfo
 test.obj(test)
  Error 42: Symbol Undefined _D3std7variant17__T8VariantNVk20Z8VariantN6__initZ
 --- errorlevel 2
 
 What's the problem?
Which version of the DMD comiler are you using? When I tried your example with version 2.008 it worked... --------------- C:\temp>type test.d import std.stdio; import std.variant; void main() { Variant v; v = 4; writefln("%s", v); } C:\temp>dmd test.d c:\dmd\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi; C:\temp>test 4 C:\temp>dmd Digital Mars D Compiler v2.008 ---------------- -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 31 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Alex" <faint_u 163.com> wrote in message 
news:flb58s$1r2o$1 digitalmars.com...
 The error occured with dmd 2.007.
 I just tried 2.008 and it works now. BTW, where is the download page for 
 latest dmd compiler? The one in page 
 http://www.digitalmars.com/d/dcompiler.html is still version 2.007. I 
 replaced the 2.007 to 2.008 in the URL to get the correct version. But i 
 think there should be a web page always listing the latest version.
That would be the change log. The newest version is always at the top of the changelog; click on the version number to get the latest version. I really wish Walter would stop putting older versions as "stable" versions because they never seem to be as good as the newest ones :P
Dec 31 2007