www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Immutable static arrays

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Is this a bug?

	import std.stdio;
	void main() {
		immutable(int)[4] a;
		immutable(int[4]) b;
		writeln(typeid(a));
		writeln(typeid(b));
	}

Output:

	immutable(int[4])
	immutable(int[4])

So there's no tail-const type for static arrays?

More to the point, how should AA's with immutable static array keys be
implemented? The current implementation doesn't work at all because the
input static array can't be assigned to the Slot (the static array field
in Slot is completely immutable, even from the Slot ctor???).


T

-- 
It is widely believed that reinventing the wheel is a waste of time; but
I disagree: without wheel reinventers, we would be still be stuck with
wooden horse-cart wheels.
Mar 16 2012
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"H. S. Teoh" <hsteoh quickfur.ath.cx> wrote in message 
news:mailman.803.1331956296.4860.digitalmars-d puremagic.com...
 Is this a bug?

 import std.stdio;
 void main() {
 immutable(int)[4] a;
 immutable(int[4]) b;
 writeln(typeid(a));
 writeln(typeid(b));
 }

 Output:

 immutable(int[4])
 immutable(int[4])

 So there's no tail-const type for static arrays?

 More to the point, how should AA's with immutable static array keys be
 implemented? The current implementation doesn't work at all because the
 input static array can't be assigned to the Slot (the static array field
 in Slot is completely immutable, even from the Slot ctor???).


 T

 -- 
 It is widely believed that reinventing the wheel is a waste of time; but
 I disagree: without wheel reinventers, we would be still be stuck with
 wooden horse-cart wheels.
This is correct. It's the same as for primitive types. Because they implicitly convert to immutable, AAs should store a mutable copy. T[N] should be handled almost the same as T.
Mar 16 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Mar 17, 2012 at 02:59:55PM +1100, Daniel Murphy wrote:
[...]
 So there's no tail-const type for static arrays?

 More to the point, how should AA's with immutable static array keys
 be implemented? The current implementation doesn't work at all
 because the input static array can't be assigned to the Slot (the
 static array field in Slot is completely immutable, even from the
 Slot ctor???).
[...]
 This is correct.  It's the same as for primitive types.  Because they
 implicitly convert to immutable, AAs should store a mutable copy.
 T[N] should be handled almost the same as T. 
[...] So string[immutable int[4]] should store int[4] as key? I guess I just found it strange that assigning to a string (==immutable(char)[]) field is OK, but assigning to immutable(char[4]) is not. T -- Without outlines, life would be pointless.
Mar 16 2012
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"H. S. Teoh" <hsteoh quickfur.ath.cx> wrote in message 
news:mailman.805.1331958621.4860.digitalmars-d puremagic.com...
 On Sat, Mar 17, 2012 at 02:59:55PM +1100, Daniel Murphy wrote:
 [...]
 So there's no tail-const type for static arrays?

 More to the point, how should AA's with immutable static array keys
 be implemented? The current implementation doesn't work at all
 because the input static array can't be assigned to the Slot (the
 static array field in Slot is completely immutable, even from the
 Slot ctor???).
[...]
 This is correct.  It's the same as for primitive types.  Because they
 implicitly convert to immutable, AAs should store a mutable copy.
 T[N] should be handled almost the same as T.
[...] So string[immutable int[4]] should store int[4] as key? I guess I just found it strange that assigning to a string (==immutable(char)[]) field is OK, but assigning to immutable(char[4]) is not.
The difference is dynamic arrays contain an indirection, static arrays don't. Treat them the same as you do primitive types and they should work. (apart from compiler bugs)
Mar 16 2012