www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - immutable bug?

reply Era Scarecrow <rtcvb32 yahoo.com> writes:
 DMD 2.057 - Windows version

 I'm in the middle of converting a medium sized project from C to D. While I'm
doing that I have several static data structures. Here's where I find myself.
Can't probably duplicate the error message without the full sources.

 Since immutable is transitive, this should not cause an error. But I am
getting this one, which repeats 4 times for all four Notepart entries:

Error: cannot implicitly convert expression ("fQuality") of type string to
char[]

//for reference on types
const nLen = 4;
enum ValueType {}
struct Flags {}

alias NotePart NP;
alias ValueType VT;

struct NotePart {
	ValueType type;	
	char[] id;
	Flags flags;
	int _size;
}

struct SubRecordParts {
	char name[nLen];
	char requ[nLen];
	int size;
	NotePart[] notes;
	int identifyBy = -1;
}

//immutable should (i think) implicitly change char[] to immutable(char[])
immutable SubRecordParts subParts[] = [
	{"AADT", "", 16, [
		NP(VT.ranged_32, "IApparatus"),
		NP(VT.float_32, "fQuality"),
		NP(VT.float_32, "fWeight"),
		NP(VT.i_32, "iuses")]}
];
Jan 25 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 25 Jan 2012 10:46:57 -0500, Era Scarecrow <rtcvb32 yahoo.com>  
wrote:

  DMD 2.057 - Windows version

  I'm in the middle of converting a medium sized project from C to D.  
 While I'm doing that I have several static data structures. Here's where  
 I find myself. Can't probably duplicate the error message without the  
 full sources.

  Since immutable is transitive, this should not cause an error. But I am  
 getting this one, which repeats 4 times for all four Notepart entries:

 Error: cannot implicitly convert expression ("fQuality") of type string  
 to char[]

 //for reference on types
 const nLen = 4;
 enum ValueType {}
 struct Flags {}

 alias NotePart NP;
 alias ValueType VT;

 struct NotePart {
 	ValueType type;	
 	char[] id;
 	Flags flags;
 	int _size;
 }

 struct SubRecordParts {
 	char name[nLen];
 	char requ[nLen];
 	int size;
 	NotePart[] notes;
 	int identifyBy = -1;
 }

 //immutable should (i think) implicitly change char[] to  
 immutable(char[])
 immutable SubRecordParts subParts[] = [
 	{"AADT", "", 16, [
 		NP(VT.ranged_32, "IApparatus"),
 		NP(VT.float_32, "fQuality"),
 		NP(VT.float_32, "fWeight"),
 		NP(VT.i_32, "iuses")]}
 ];
Your issue is here, (I'm guessing). If do this (after slimming down to a compilable sample): alias immutable(NotePart) NP; then it compiles. Because the expression (guessing that you have NP aliased to NotePart) NP(...) is constructing a NotePart and not an *immutable* NotePart, it cannot resolve that part of the expression, even though the whole expression is treated as immutable after evaluation. Maybe there's an enhancement lurking in here... -Steve
Jan 25 2012
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 //immutable should (i think) implicitly change char[] to immutable(char[])
 Your issue is here, (I'm guessing).  If do this (after slimming down to a
 compilable sample):
 alias immutable(NotePart) NP;
 then it compiles.
 Because the expression (guessing that you have NP aliased to NotePart)
 NP(...) is constructing a NotePart and not an *immutable* NotePart, it
 cannot resolve that part of the expression, even though the whole
 expression is treated as immutable after evaluation.
 Maybe there's an enhancement lurking in here...
 -Steve
Perhaps that's it. I only aliased it to save on typing in this large block (some 200 entries). Easy work around since the structure is never used elsewhere (and instantiated immutable); So I converted my char[4] to immutable(char[4]) and the problem went away. But knowing it will be immutable later, it seems like it should figure it out. This is one of those lesser details they will get to later. I am not going to pester them, unless you think I should enter a bug report.
Jan 25 2012