Search code examples
d

What is void[]?


I've seen the type void[] used in a few places in the D documentation and function signatures. What does it mean?

I've searched the array section in the documentation as well as Google, but didn't turn up anything conclusive. All I found is that all arrays are implicitly convertible to the void[] type. Is it just an untyped array?


Solution

  • void[] is just an array of anything. in void[] is a convenient signature for a function that just writes bytes to a file or something because it will accept any kind of array - void meaning the type isn't important and in meaning you aren't meaning to modify or store it, thus allowing you to accept const or immutable arrays too.

    To use a void[], you must first cast it to something else, typically, ubyte[], so the elements have a concrete size allowing you to index it. void[].length is given as the total number of bytes in the array, so if you are just passing a pointer and length of data to a function (like an operating system level file write, e.g. Linux's write syscall), passing arr.ptr, arr.length will get those bytes written without caring about what they represent.