Thread overview
immutable bug?
Jan 25, 2012
Era Scarecrow
Jan 25, 2012
Era Scarecrow
January 25, 2012
 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")]}
];
January 25, 2012
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
January 25, 2012
== 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.