www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Printing an std.container.Array

reply "Bayan Rafeh" <bayan.rafeh92 gmail.com> writes:
Executing this code:

import std.container.array;
import std.stdio;


int main() {
	writeln(Array!int([1, 2]));
	return 0;
}

outputs the following:

Array!int(RefCounted!(Payload,
cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0)))


The strange thing is that this works fine:

import std.container.array;
import std.stdio;

int main() {
	writeln(Array!int([1, 2])[0..$]);
	return 0;
}

[1, 2]

How am I supposed to interpret this?
Apr 16 2015
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 16 Apr 2015 19:55:52 +0000
Bayan Rafeh via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Executing this code:
 
 import std.container.array;
 import std.stdio;
 
 
 int main() {
 	writeln(Array!int([1, 2]));
 	return 0;
 }
 
 outputs the following:
 
 Array!int(RefCounted!(Payload,
 cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0)))
 
 
 The strange thing is that this works fine:
 
 import std.container.array;
 import std.stdio;
 
 int main() {
 	writeln(Array!int([1, 2])[0..$]);
 	return 0;
 }
 
 [1, 2]
 
 How am I supposed to interpret this?
https://github.com/D-Programming-Language/phobos/pull/2875
Apr 16 2015
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Apr 16, 2015 at 07:55:52PM +0000, Bayan Rafeh via Digitalmars-d-learn
wrote:
 Executing this code:
 
 import std.container.array;
 import std.stdio;
 
 
 int main() {
 	writeln(Array!int([1, 2]));
 	return 0;
 }
 
 outputs the following:
 
 Array!int(RefCounted!(Payload,
 cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0)))
 
 
 The strange thing is that this works fine:
 
 import std.container.array;
 import std.stdio;
 
 int main() {
 	writeln(Array!int([1, 2])[0..$]);
 	return 0;
 }
 
 [1, 2]
 
 How am I supposed to interpret this?
Try slicing the Array before passing it to writeln? writeln(Array!int([1, 2])[]); Basically, there is a distinction between a container and a range that spans the items in a container. The conventional syntax for getting a range over a container's contents is the slicing operator []. T -- It said to install Windows 2000 or better, so I installed Linux instead.
Apr 16 2015
parent "Bayan Rafeh" <bayan.rafeh92 gmail.com> writes:
On Thursday, 16 April 2015 at 20:08:30 UTC, H. S. Teoh wrote:
 On Thu, Apr 16, 2015 at 07:55:52PM +0000, Bayan Rafeh via 
 Digitalmars-d-learn wrote:
 Executing this code:
 
 import std.container.array;
 import std.stdio;
 
 
 int main() {
 	writeln(Array!int([1, 2]));
 	return 0;
 }
 
 outputs the following:
 
 Array!int(RefCounted!(Payload,
 cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0)))
 
 
 The strange thing is that this works fine:
 
 import std.container.array;
 import std.stdio;
 
 int main() {
 	writeln(Array!int([1, 2])[0..$]);
 	return 0;
 }
 
 [1, 2]
 
 How am I supposed to interpret this?
Try slicing the Array before passing it to writeln? writeln(Array!int([1, 2])[]); Basically, there is a distinction between a container and a range that spans the items in a container. The conventional syntax for getting a range over a container's contents is the slicing operator []. T
Thanks that works great, though I still don't understand where the controversy is coming from. There was a mention of causing confusion between containers and ranges. How so?
Apr 17 2015
prev sibling next sibling parent "Panke" <tobias pankrath.net> writes:
On Thursday, 16 April 2015 at 19:55:53 UTC, Bayan Rafeh wrote:

 How am I supposed to interpret this?
The array contains two elements. The first equals one and the second equals two. What happens under the hood is that Array does no provide a toString method, instead a default is used. This results in your first output. For ranges - and the slice of the array is a range while the array is not - writeln prints the elements as a special case which leads to your second output.
Apr 16 2015
prev sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 16 Apr 2015 13:05:48 -0700
"H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> wrote:

 On Thu, Apr 16, 2015 at 07:55:52PM +0000, Bayan Rafeh via Digitalmars-d-learn
 wrote:
 Executing this code:
 
 import std.container.array;
 import std.stdio;
 
 
 int main() {
 	writeln(Array!int([1, 2]));
 	return 0;
 }
 
 outputs the following:
 
 Array!int(RefCounted!(Payload,
 cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0)))
 
 
 The strange thing is that this works fine:
 
 import std.container.array;
 import std.stdio;
 
 int main() {
 	writeln(Array!int([1, 2])[0..$]);
 	return 0;
 }
 
 [1, 2]
 
 How am I supposed to interpret this?
Try slicing the Array before passing it to writeln? writeln(Array!int([1, 2])[]); Basically, there is a distinction between a container and a range that spans the items in a container. The conventional syntax for getting a range over a container's contents is the slicing operator []. T
Yep, but problem is almost no one expect this, or know this. We definitely should do better.
Apr 16 2015
parent reply "Panke" <tobias pankrath.net> writes:
 Yep, but problem is almost no one expect this, or know this. We 
 definitely
 should do better.
How?
Apr 16 2015
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/16/15 4:18 PM, Panke wrote:
 Yep, but problem is almost no one expect this, or know this. We
 definitely
 should do better.
How?
By doing what is expected. Print the array contents. See my new comment in that PR. -Steve
Apr 16 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 16 April 2015 at 20:34:19 UTC, Steven Schveighoffer 
wrote:
 On 4/16/15 4:18 PM, Panke wrote:
 Yep, but problem is almost no one expect this, or know this. 
 We
 definitely
 should do better.
How?
By doing what is expected. Print the array contents. See my new comment in that PR. -Steve
I think that this action should print the contents of the container, not it's type, ie [1, 2, 3, 4]: import std.stdio : writeln; import std.container.rbtree : redBlackTree; void main() { auto a = redBlackTree(1, 2, 1, 3, 4, 3); writeln(a); // std.container.rbtree.RedBlackTree!(int, "a < b", false).RedBlackTree } Will it be modified in future versions of DMD?
Apr 16 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/16/15 5:18 PM, Dennis Ritchie wrote:
 On Thursday, 16 April 2015 at 20:34:19 UTC, Steven Schveighoffer wrote:
 On 4/16/15 4:18 PM, Panke wrote:
 Yep, but problem is almost no one expect this, or know this. We
 definitely
 should do better.
How?
By doing what is expected. Print the array contents. See my new comment in that PR. -Steve
I think that this action should print the contents of the container, not it's type, ie [1, 2, 3, 4]: import std.stdio : writeln; import std.container.rbtree : redBlackTree; void main() { auto a = redBlackTree(1, 2, 1, 3, 4, 3); writeln(a); // std.container.rbtree.RedBlackTree!(int, "a < b", false).RedBlackTree } Will it be modified in future versions of DMD?
Yes, that's what should happen. -Steve
Apr 17 2015
prev sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Thu, 16 Apr 2015 20:18:40 +0000
Panke via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Yep, but problem is almost no one expect this, or know this. We 
 definitely
 should do better.
How?
Improve doc at least. But it would be fine to have something like dump function (equivalent of php var_dump)
Apr 16 2015