A couple of questions about arrays and slices
Cecil Ward
cecil at cecilward.com
Sat Jun 24 01:02:12 UTC 2023
On Thursday, 22 June 2023 at 05:21:52 UTC, Cecil Ward wrote:
> On Thursday, 22 June 2023 at 01:44:22 UTC, Jonathan M Davis
> wrote:
>> On Wednesday, June 21, 2023 7:05:28 PM MDT Paul Backus via
>> Digitalmars-d-learn wrote:
>>> [...]
>>
>> To add to that, it _has_ to know the element type, because
>> aside from anything related to a type's size, it bit-blits the
>> type's init value onto the new elements when it increases the
>> length of the dynamic array.
>>
>> You'd probably be dealing with bytes if you were explicitly
>> asking for memory and the like (e.g. with malloc), but a
>> dynamic array is properly typed, and everything you do with it
>> in @safe code is going to deal with it as properly typed. For
>> it to be otherwise would require @system casts.
>>
>> - Jonathan M Davis
>
> Thankyou Jonathan!
I just had a fight with LDC over the following code when I tried
out reserve. I have an associative array that maps strings to
‘ordinals’ ie uints that are unique, and the compiler hates the
call to reserve.
==
struct decls_t
{
uint n_entries = 0;
uint[ dstring ] ordinals; // Associative array maps variable
names to ordinals
}
static decls_t Decls;
enum NPreAllocEntries = 32;
Decls.ordinals.reserve( NPreAllocEntries );
source>(82): Error: none of the overloads of template
`object.reserve` are callable using argument types
`!()(uint[dstring], ulong)`
/opt/compiler-explorer/ldc1.32.1/ldc2-1.32.1-linux-x86_64/bin/../import/object.d(3983): Candidate is: `reserve(T)(ref T[] arr, size_t newcapacity)`
Compiler returned: 1
More information about the Digitalmars-d-learn
mailing list