February 25, 2016
On Thursday, 25 February 2016 at 12:18:07 UTC, mahdi wrote:
> On Thursday, 25 February 2016 at 11:50:02 UTC, sigod wrote:
>> On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote:
>>> Thanks.
>>>
>>> So when we define the function, we MUST specify the array size to be able to accept a static array?
>>> Can't we just define a function which can accept any static array with any size? (e.g. a function to calculate average of a static int array of any size)?
>>
>> Static array can be accepted in place of dynamic:
>>
>> 	void foo()
>> 	{
>> 		int[3] arr = [1, 2, 3];
>> 		
>> 		bar(arr);
>> 	}
>> 	
>> 	void bar(scope int[] arr)
>> 	{
>> 		import std.stdio : writeln;
>> 		writeln(arr); // [1, 2, 3]
>> 	}
>>
>> But be careful not to escape such variables. Demonstration: http://dpaste.dzfl.pl/613e04d4fe3f
>
> My question: If in your `bar` function, the code tries to add a new element to the dynamic array, it will be completely ok because array is dynamic. BUT if we pass a static array to this function, can this error be detected at compile time (and prevent a runtime error)? If so, how?

I'm not sure if this is an error at all. Append sees that array doesn't have any available space and allocates new array.

	writeln(arr.ptr); // 7FBFC45AA0
	arr ~= 1;
	writeln(arr.ptr); // 4002E000

I would say that you have poor code design if your function must be able to accept static arrays and append elements to it.

You might find this useful: http://dlang.org/phobos/std_array.html#.Appender
February 25, 2016
On Thursday, 25 February 2016 at 12:18:07 UTC, mahdi wrote:
> On Thursday, 25 February 2016 at 11:50:02 UTC, sigod wrote:
>> On Thursday, 25 February 2016 at 10:03:08 UTC, mahdi wrote:
>>> Thanks.
>>>
>>> So when we define the function, we MUST specify the array size to be able to accept a static array?
>>> Can't we just define a function which can accept any static array with any size? (e.g. a function to calculate average of a static int array of any size)?
>>
>> Static array can be accepted in place of dynamic:
>>
>> 	void foo()
>> 	{
>> 		int[3] arr = [1, 2, 3];
>> 		
>> 		bar(arr);
>> 	}
>> 	
>> 	void bar(scope int[] arr)
>> 	{
>> 		import std.stdio : writeln;
>> 		writeln(arr); // [1, 2, 3]
>> 	}
>>
>> But be careful not to escape such variables. Demonstration: http://dpaste.dzfl.pl/613e04d4fe3f
>
> My question: If in your `bar` function, the code tries to add a new element to the dynamic array, it will be completely ok because array is dynamic. BUT if we pass a static array to this function, can this error be detected at compile time (and prevent a runtime error)? If so, how?

Also, if you need to append elements to an array inside of a function, then you need to mark function arguments as `ref`:

	void bar(ref int[] arr)

Code wouldn't compile if you try to pass static array as `ref` argument.

	Error: function f436.bar (ref int[] arr) is not callable using argument types (int[3])
February 25, 2016
On Wednesday, 24 February 2016 at 21:48:14 UTC, mahdi wrote:
> Suppose we have a function like this:
>
> void diss(int[] array) ...
>
> How can we detect is `array` is static (fixed size) or dynamic, inside the function body?

I don't understand what I'm doing but got a proof of concept for you. This https://dlang.org/phobos/std_traits.html page helped.

import std.stdio;

void main() {
    int[]  a;
    int[1] b;
    writeln(is(typeof(a) == int[])); // true
    writeln(is(typeof(b) == int[])); // false
}

February 25, 2016
On Thu, 25 Feb 2016 02:08:18 +0000, Adam D. Ruppe wrote:

> On Thursday, 25 February 2016 at 01:31:17 UTC, Chris Wright wrote:
>> When you get to GC-allocated stuff, there's no way to tell.
> 
> The GC is easy, you can simply ask it:
> 
> http://dpldocs.info/experimental-docs/core.memory.GC.addrOf.1.html

I saw that method, and I saw that it returned the base address of a Pool. From the name, fields, and Gcx invocation, I guessed that Pool represented a bundle of multiple values. It's got about ten or twelve words of overhead, which would be a bit heavy to have one for each allocated object. (And that's on top of the bookkeeping malloc has to do, since the GC uses malloc under the hood.)

Or maybe it's just got that much overhead.
February 25, 2016
On Wednesday, 24 February 2016 at 21:48:14 UTC, mahdi wrote:
> Suppose we have a function like this:
>
> void diss(int[] array) ...
>
> How can we detect is `array` is static (fixed size) or dynamic, inside the function body?

I don't see that anyone has mentioned it but:

https://dlang.org/phobos/std_traits.html#isStaticArray
https://dlang.org/phobos/std_traits.html#isDynamicArray
February 25, 2016
On 2/24/16 9:08 PM, Adam D. Ruppe wrote:
> On Thursday, 25 February 2016 at 01:31:17 UTC, Chris Wright wrote:
>> When you get to GC-allocated stuff, there's no way to tell.
>
> The GC is easy, you can simply ask it:
>
> http://dpldocs.info/experimental-docs/core.memory.GC.addrOf.1.html
>
> "If p references memory not originally allocated by this garbage
> collector, if p is null, or if the garbage collector does not support
> this operation, null will be returned."
>
>
> The append operator uses this kind of logic to determine if it is safe
> to append. The `capacity` property on slices can query, though it is
> zero in some cases where the GC owns it, but it still needs reallocation
> to be appended to (e.g. when the array is already at the max length of
> the allocated block, or when the stomping protection kicks in. See:
> http://dlang.org/d-array-article.html )

Just a slight nit -- it will only return 0 if the array slice doesn't end at the end of valid data as defined by the metadata. If it ends exactly at the max length of the block, then arr.capacity == arr.length.

It will also return 0 if the memory block in question wasn't allocated via the array allocation routine (and therefore has no metadata).

The correct answer is what you said, to use the GC to look up whether it's part of the GC.

-Steve
February 25, 2016
On 02/25/2016 04:47 AM, sigod wrote:

>      void bar(ref int[] arr)
>
> Code wouldn't compile if you try to pass static array as `ref` argument.

To qualify further, static arrays cannot be passed as slice references because although there is an automatic slicing of static arrays, such slices are rvalues and rvalues cannot be bound to 'ref' parameters:

>      Error: function f436.bar (ref int[] arr) is not callable using
> argument types (int[3])

Ali

1 2
Next ›   Last »