I feel the dynamic array .sizeof property is kind of a bait and switch

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 26 11:12:10 PDT 2017


On Wednesday, 26 July 2017 at 02:32:07 UTC, Adam D. Ruppe wrote:
>
>> I've hand rolled a function which is working for me currently, 
>> but with my coding ability, I'd feel much safer with something 
>> official :)
>
> You could also do (cast(ubyte[]) array).length.
>

This was my (way over complicated) attempt at the same thing. 
I'll blame my verbosity because I was trying to teach myself 
about templates and constraints at the time :)

/+
There is a special type of array which acts as a wildcard that 
can hold arrays of any kind, declared as void[].  The .length of 
a void array is the length of the data in
bytes rather than the number of elements in its original type. "
+/

int arrayByteSize(T)(T someArray) if (isDynamicArray!(T))
{
     ubyte[] arr = cast(ubyte[]) someArray;
     return cast(int) arr.length;
}

>> It just seems like something this basic regarding dynamic 
>> arrays should just be built-in.
>
> What are you using it for?

glBufferData(GL_ARRAY_BUFFER, vertices.arrayByteSize, 
vertices.ptr, GL_STATIC_DRAW);

I find that openGL uses array buffers all over the place.

I just keep going back to the idea that such low level 
functionality should be inherent in either the language or 
Phobos.  If that is even possible.


More information about the Digitalmars-d-learn mailing list