www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array Ambiguity

reply Your Name <your.email example.org> writes:
Static + Dynamic arrays exhibit some really weird behavior.

https://run.dlang.io/is/3RfmAb

https://run.dlang.io/is/EP0SPw
Dec 20 2018
next sibling parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 20 December 2018 at 22:59:31 UTC, Your Name wrote:
 Static + Dynamic arrays exhibit some really weird behavior.

 https://run.dlang.io/is/3RfmAb

 https://run.dlang.io/is/EP0SPw
No weird behavior. The parameter as "int[] a" doesn't actually mean dynamic array. It means slice. It takes a slice of the static array. In the first example it picks static array because the compiler can make the array static since it's not mutated in the function. Like if you do the following: https://run.dlang.io/is/fwvd59 Then it'll pick dynamic array.
Dec 20 2018
next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Thursday, 20 December 2018 at 23:18:45 UTC, bauss wrote:
 On Thursday, 20 December 2018 at 22:59:31 UTC, Your Name wrote:
 Static + Dynamic arrays exhibit some really weird behavior.

 https://run.dlang.io/is/3RfmAb

 https://run.dlang.io/is/EP0SPw
No weird behavior. The parameter as "int[] a" doesn't actually mean dynamic array. It means slice. It takes a slice of the static array. In the first example it picks static array because the compiler can make the array static since it's not mutated in the function.
I think fixed length arrays are passed by value, and so can be mutated: https://run.dlang.io/is/zs5w6b
 Like if you do the following:
 https://run.dlang.io/is/fwvd59

 Then it'll pick dynamic array.
The reason here is because of auto, not of ~=. Bastiaan.
Dec 20 2018
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Thursday, December 20, 2018 4:18:45 PM MST bauss via Digitalmars-d wrote:
 On Thursday, 20 December 2018 at 22:59:31 UTC, Your Name wrote:
 Static + Dynamic arrays exhibit some really weird behavior.

 https://run.dlang.io/is/3RfmAb

 https://run.dlang.io/is/EP0SPw
No weird behavior. The parameter as "int[] a" doesn't actually mean dynamic array. It means slice. It takes a slice of the static array.
Per the official language terminolgy, int[] is a dynamic array regardless of what it's a slice of. And aside from lifetime issues, there really isn't a semantic difference between a dynamic array that's a slice of GC-allocated memory or a slice of something else (like a static array). It acts exactly the same, including the abilility to be appended to (it's just that if it's not GC-allocated, it's guaranteed to not be able to grow in place, whereas with a GC-allocated array, it might be able to). - Jonathan M Davis
Dec 20 2018
prev sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Thursday, 20 December 2018 at 22:59:31 UTC, Your Name wrote:
 Static + Dynamic arrays exhibit some really weird behavior.

 https://run.dlang.io/is/3RfmAb
I haven’t checked the rules, but it seems logical to me. Since the lengths match, the fixed length array argument is a more specific match than the slice argument.
 https://run.dlang.io/is/EP0SPw
A slice argument can take a slice of a fixed length array. The learn forum would probably have been the right place for this :) Bastiaan.
Dec 20 2018