www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compile time static array issues.

reply Era Scarecrow <rtcvb32 yahoo.com> writes:
 I don't seem to have a good explanation why this isn't working. All i end up
getting is 'Error: cannot implicitly convert expression .... of type
string[][] to joined[]'. I also get 'incompatible types for (("AADT") ? (16)):
'string' and 'int''. I've tried it with casts, and full array qualifiers.

 I'm using DMD 2.053.

--

 struct joined {
	string name;
	string partof;
	string preReq;
}

struct subrecord_parts {
	string name;
	int size;
	string[] notes;
	int identifyBy;
	int[] partSize;
}

joined[] attached;
subrecord_parts[] parts;

static this() {
	attached = [
		["MAST", "DATA", "TES3"],
		["ANAM","INTV", "FACT"]];

	parts = [
		["AADT", 16, ["fWeight", "iValue", "iUses", "fQuality"], -1, [4,4,4,4]],
		["AODT", 24, ["iType", "fWeight", "iValue", "iHealth", "iEnchant Points",
"iArmour"],  -1, [4,4,4,4,4,4]]];
}

-- output
test.d(20): Error: cannot implicitly convert expression
([["MAST","DATA","TES3"],["ANAM","INTV","FACT"]]) of type string[][] to joined[]
Error: incompatible types for (("AADT") ? (16)): 'string' and 'int'
Error: incompatible types for (("AODT") ? (24)): 'string' and 'int'
test.d(26): Error: cannot implicitly convert expression
([[(__error),(__error),(__error),(__error),(__error)],[(__error),(__error),(__error),(__error),(__error)]])
of type _error_[][] to subrecord_parts[]
May 28 2011
next sibling parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 28/05/2011 23:57, Era Scarecrow wrote:
   I don't seem to have a good explanation why this isn't working. All i end up
 getting is 'Error: cannot implicitly convert expression .... of type
 string[][] to joined[]'. I also get 'incompatible types for (("AADT") ? (16)):
 'string' and 'int''. I've tried it with casts, and full array qualifiers.

   I'm using DMD 2.053.

 --

   struct joined {
 	string name;
 	string partof;
 	string preReq;
 }

 struct subrecord_parts {
 	string name;
 	int size;
 	string[] notes;
 	int identifyBy;
 	int[] partSize;
 }

 joined[] attached;
 subrecord_parts[] parts;

 static this() {
 	attached = [
 		["MAST", "DATA", "TES3"],
 		["ANAM","INTV", "FACT"]];

try attached = [joined("MAST", "DATA", "TES3"), joined("ANAM", "INTV", "FACT")];
 	parts = [
 		["AADT", 16, ["fWeight", "iValue", "iUses", "fQuality"], -1, [4,4,4,4]],
 		["AODT", 24, ["iType", "fWeight", "iValue", "iHealth", "iEnchant Points",
 "iArmour"],  -1, [4,4,4,4,4,4]]];

Similar here, parts = [ subrecord_parts("AADT", 16, ["fWeight", "iValue", "iUses", "fQuality"], -1, [4,4,4,4]), subrecord_parts(...)]; etc.
 }

 -- output
 test.d(20): Error: cannot implicitly convert expression
 ([["MAST","DATA","TES3"],["ANAM","INTV","FACT"]]) of type string[][] to
joined[]
 Error: incompatible types for (("AADT") ? (16)): 'string' and 'int'
 Error: incompatible types for (("AODT") ? (24)): 'string' and 'int'
 test.d(26): Error: cannot implicitly convert expression
 ([[(__error),(__error),(__error),(__error),(__error)],[(__error),(__error),(__error),(__error),(__error)]])
 of type _error_[][] to subrecord_parts[]

-- Robert http://octarineparrot.com/
May 28 2011
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
== Quote from Robert Clipsham (robert octarineparrot.com)'s article
 On 28/05/2011 23:57, Era Scarecrow wrote:
 try attached = [joined("MAST", "DATA", "TES3"), joined("ANAM", "INTV",
 "FACT")];

 Similar here, parts = [ subrecord_parts("AADT", 16, ["fWeight",
 "iValue", "iUses", "fQuality"], -1, [4,4,4,4]), subrecord_parts(...)]; etc.

I was certain I tried that already. But it compiles now. Perhaps I just didn't do enough of them to confirm if it was working. Thanks
May 28 2011
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Era Scarecrow:

  I don't seem to have a good explanation why this isn't working.

Also try: struct Joined { string name, partof, preReq; } struct SubrecordParts { string name; int size; string[] notes; int identifyBy; int[] partSize; } Joined[] attached = [ {"MAST", "DATA", "TES3"}, {"ANAM","INTV", "FACT"} ]; SubrecordParts[] parts = [ {"AADT", 16, ["fWeight", "iValue", "iUses", "fQuality"], -1, [4,4,4,4]}, {"AODT", 24, ["iType", "fWeight", "iValue", "iHealth", "iEnchant Points", "iArmour"], -1, [4,4,4,4,4,4]} ]; void main() {} In D struct names are (usually) CamelCase. Bye, bearophile
May 28 2011
next sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
== Quote from bearophile (bearophileHUGS lycos.com)'s article
 Also try:
 struct Joined {
     string name, partof, preReq;
 }
 Joined[] attached = [
     {"MAST", "DATA", "TES3"},
     {"ANAM","INTV",  "FACT"}
 ];

Coming from C, this is exactly what I first tried. However since I don't recall the {}'s being used in D it ended up using []'s, which sounds right and yet doesn't at the same time. Like this (Which is broken). Joined[] attached = [ ["MAST", "DATA", "TES3"], ["ANAM","INTV", "FACT"] ]; Doing a search I got the impression I had to use 'static this()'. Thanks bearophile. Let's hope I don't need much more help later :P
May 29 2011
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-05-29 03:56, bearophile wrote:
 Era Scarecrow:

   I don't seem to have a good explanation why this isn't working.

Also try: struct Joined { string name, partof, preReq; } struct SubrecordParts { string name; int size; string[] notes; int identifyBy; int[] partSize; } Joined[] attached = [ {"MAST", "DATA", "TES3"}, {"ANAM","INTV", "FACT"} ]; SubrecordParts[] parts = [ {"AADT", 16, ["fWeight", "iValue", "iUses", "fQuality"], -1, [4,4,4,4]}, {"AODT", 24, ["iType", "fWeight", "iValue", "iHealth", "iEnchant Points", "iArmour"], -1, [4,4,4,4,4,4]} ]; void main() {} In D struct names are (usually) CamelCase. Bye, bearophile

Isn't that struct initialization syntax deprecated or to be deprecated? -- /Jacob Carlborg
May 29 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jacob Carlborg:

 Isn't that struct initialization syntax deprecated or to be deprecated?

You may be right. Sometimes I read about deprecations, and then after a while I forget it because the alternative (like using the complex numbers of Phobos) is too much ugly by comparison :-) I think we need an official deprecation list page, that includes a good rationale for each of the deprecated things. Bye, bearophile
May 29 2011
parent reply Jacob Carlborg <doob me.com> writes:
On 2011-05-29 15:01, bearophile wrote:
 Jacob Carlborg:

 Isn't that struct initialization syntax deprecated or to be deprecated?

You may be right. Sometimes I read about deprecations, and then after a while I forget it because the alternative (like using the complex numbers of Phobos) is too much ugly by comparison :-) I think we need an official deprecation list page, that includes a good rationale for each of the deprecated things. Bye, bearophile

The would be very helpful. There are too many messages like the one from above from me. -- /Jacob Carlborg
May 29 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 C struct initialization in D is also buggy, it doesn't take into
 account field initializers. It's best to get used to the D way of
 initializing structs.

I'd like it to be debugged instead :-) Bye, bearophile
May 29 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
C struct initialization in D is also buggy, it doesn't take into
account field initializers. It's best to get used to the D way of
initializing structs.
May 29 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 5/29/11, bearophile <bearophileHUGS lycos.com> wrote:
 Andrej Mitrovic:

 C struct initialization in D is also buggy, it doesn't take into
 account field initializers. It's best to get used to the D way of
 initializing structs.

I'd like it to be debugged instead :-)

Well if it has already been decided that they're going away, no point in debugging them! :) Although I have to say, porting C code with struct initializers to D is a pain in the ass. Maybe I need a Vim script to do that or something, heh..
May 29 2011