Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 16, 2015 Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
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? |
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bayan Rafeh | 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 |
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bayan Rafeh | 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. |
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bayan Rafeh | 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.
|
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
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. |
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | >
> Yep, but problem is almost no one expect this, or know this. We definitely
> should do better.
How?
|
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Panke | 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
|
April 16, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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? |
April 17, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Panke | 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) |
April 17, 2015 Re: Printing an std.container.Array | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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?
|
Copyright © 1999-2021 by the D Language Foundation