Re: Returning dynamic array from the function?

2018-09-08 Thread rjframe via Digitalmars-d-learn
On Sat, 08 Sep 2018 10:22:38 +, SuperPrower wrote: > Also, is this me and my bad English, or first comment in code in this > paragraph (linked above, not in the discussion) is supposed to be > something different? Shouldn't it be reference? Static arrays are value types, so the values are

Re: Returning dynamic array from the function?

2018-09-08 Thread SuperPrower via Digitalmars-d-learn
On Saturday, 8 September 2018 at 10:05:31 UTC, rikki cattermole wrote: onReceive: "The event handler that receives incoming data. Be sure to copy the incoming ubyte[] since it is not guaranteed to be valid after the callback returns." It could be a buffer on the stack, either way, .dup it

Re: Returning dynamic array from the function?

2018-09-08 Thread rikki cattermole via Digitalmars-d-learn
On 08/09/2018 9:46 PM, SuperPrower wrote: On Saturday, 8 September 2018 at 09:36:21 UTC, rikki cattermole wrote: We're going to need to see a minified version of the code to see what you're doing. Sure, here it is: ``` auto getBoards() { string[] boardList; auto url = baseUrl ~

Re: Returning dynamic array from the function?

2018-09-08 Thread SuperPrower via Digitalmars-d-learn
On Saturday, 8 September 2018 at 09:36:21 UTC, rikki cattermole wrote: We're going to need to see a minified version of the code to see what you're doing. Sure, here it is: ``` auto getBoards() { string[] boardList; auto url = baseUrl ~ "/api/v2/boards"; auto http =

Re: Returning dynamic array from the function?

2018-09-08 Thread rikki cattermole via Digitalmars-d-learn
On 08/09/2018 9:34 PM, SuperPrower wrote: I have a function that produces dynamic array of strings. I would like to return this array from this function. I understand that dynamic arrays are of reference type, and thus if I try to return array variable, I will actually return a pointer to the

Returning dynamic array from the function?

2018-09-08 Thread SuperPrower via Digitalmars-d-learn
I have a function that produces dynamic array of strings. I would like to return this array from this function. I understand that dynamic arrays are of reference type, and thus if I try to return array variable, I will actually return a pointer to the first element of the array on the heap.

Re: Returning dynamic array from the function

2014-06-14 Thread Marco Cosentino via Digitalmars-d-learn
Hi, I'm new to D and stumbled upon this very interesting discussion. My question now is: can you provide an example of how to return a collection of homogeneous elements whose size is not known at compile time (for wich you would normally use a dynamic array) from a function? Thanks, Marco

Re: Returning dynamic array from the function

2014-06-14 Thread via Digitalmars-d-learn
On Saturday, 14 June 2014 at 14:02:52 UTC, Marco Cosentino wrote: Hi, I'm new to D and stumbled upon this very interesting discussion. My question now is: can you provide an example of how to return a collection of homogeneous elements whose size is not known at compile time (for wich you

Re: Returning dynamic array from the function

2014-06-14 Thread Marco Cosentino via Digitalmars-d-learn
int[] data = [1,2,3,4];// create new array on the heap Thanks for the answer. This is the bit of information I was missing: how to create an array in the heap. Is also this a valid way to do so? int[] data = new int[0]; data ~= [4,2,3,1];

Re: Returning dynamic array from the function

2014-06-14 Thread monarch_dodra via Digitalmars-d-learn
On Saturday, 14 June 2014 at 21:37:51 UTC, Marco Cosentino wrote: int[] data = [1,2,3,4];// create new array on the heap Thanks for the answer. This is the bit of information I was missing: how to create an array in the heap. Is also this a valid way to do so? int[] data = new

Re: Returning dynamic array from the function

2013-01-29 Thread timewulf
On Wednesday, 17 October 2012 at 20:38:03 UTC, bearophile wrote: Jonathan M Davis: there's no way for the compiler to always catch it for you. I think there are type systems able to always catch this kind of bug (conservative region analysis, it means that if it can't demonstrate the

Returning dynamic array from the function

2012-10-17 Thread m0rph
I tryed to learn how arrays works and found another strange thing: import std.stdio; int[] create() { int[5] a1 = [ 10, 20, 30, 40, 50 ]; int[] b1 = a1; writeln(b1: , b1); return b1; } void main() { int[] a2 = create(); writeln(a2: , a2); }

Re: Returning dynamic array from the function

2012-10-17 Thread Simen Kjaeraas
On 2012-10-17, 21:17, m0rph wrote: I tryed to learn how arrays works and found another strange thing: import std.stdio; int[] create() { int[5] a1 = [ 10, 20, 30, 40, 50 ]; int[] b1 = a1; writeln(b1: , b1); return b1; } void main() { int[] a2 =

Re: Returning dynamic array from the function

2012-10-17 Thread sclytrack
On Wednesday, 17 October 2012 at 19:22:05 UTC, Simen Kjaeraas wrote: On 2012-10-17, 21:17, m0rph wrote: I tryed to learn how arrays works and found another strange thing: import std.stdio; int[] create() { int[5] a1 = [ 10, 20, 30, 40, 50 ]; int[] b1 = a1;

Re: Returning dynamic array from the function

2012-10-17 Thread m0rph
b1 points to the exact same data as does a1. This data is stack- allocated, and thus a2 points to an overwritten stack frame. Thanks for explanation, I thought contetns of a1 are copied to the heap when assignment operator executed.

Re: Returning dynamic array from the function

2012-10-17 Thread bearophile
sclytrack: It doesn't give an error when marking the function with safe. @safe int[] create() { } I think marking it @safe is not relevant. In theory a good type system should give an error message on similar code. I don't know if D is supposed to spot similar error situations. Bye,

Re: Returning dynamic array from the function

2012-10-17 Thread Jonathan M Davis
On Wednesday, October 17, 2012 21:46:50 bearophile wrote: sclytrack: It doesn't give an error when marking the function with safe. @safe int[] create() { } I think marking it @safe is not relevant. In theory a good type system should give an error message on similar code. I don't

Re: Returning dynamic array from the function

2012-10-17 Thread sclytrack
On Wednesday, 17 October 2012 at 19:46:51 UTC, bearophile wrote: sclytrack: It doesn't give an error when marking the function with safe. @safe int[] create() { } I think marking it @safe is not relevant. In theory a good type system should give an error message on similar code. I don't

Re: Returning dynamic array from the function

2012-10-17 Thread bearophile
Jonathan M Davis: there's no way for the compiler to always catch it for you. I think there are type systems able to always catch this kind of bug (conservative region analysis, it means that if it can't demonstrate the memory doesn't escape, it prudently refuses the code). D doesn't have

Re: Returning dynamic array from the function

2012-10-17 Thread Era Scarecrow
The only way that @safe could really be applicable would be if it became @system to take the address of a local variable or to slice a static array. And perhaps it should be, but that and catching the most obvious cases are all that the compiler could do to catch this for you. Hmmm, you

Re: Returning dynamic array from the function

2012-10-17 Thread Jonathan M Davis
On Wednesday, October 17, 2012 13:07:13 Jonathan M Davis wrote: The only way that @safe could really be applicable would be if it became @system to take the address of a local variable or to slice a static array. And perhaps it should be, but that and catching the most obvious cases are all

Re: Returning dynamic array from the function

2012-10-17 Thread Timon Gehr
On 10/17/2012 10:15 PM, sclytrack wrote: On Wednesday, 17 October 2012 at 19:46:51 UTC, bearophile wrote: sclytrack: It doesn't give an error when marking the function with safe. @safe int[] create() { } I think marking it @safe is not relevant. In theory a good type system should give an