[Issue 24166] strange errors returning references to void

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 9 15:13:38 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=24166

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
I don't think that void arrays where defined so you can return individual
elements. This opens a big hole in the type system. Consider the function
signature:

void foo(void[] b);

When you look at this signature you understand that the function does not
return anything. From that point of view, I was expecting that:

ref void foo(void[] b);

would yield a compilation error, but currently it does not (I would argue that
this needs to be fixed, the compiler probably just ignores the ref).

So, you do not expect that foo actually returns anything. But when void arrays
come into play:

void foo(void[] b)
{
    return b[0];
}

the signature of foo could mean 2 things: either you don't return nothing or
you return a void variable. How would you even store that thing, cause you
cannot declare a void variable and you cannot assign the value of a void
function.

So, my 2 cents: void arrays where designed just to be used inside the
boundaries
of a function: you store some data you receive from the network, you cast it to
the appropriate type and then you return it. If you want to escape an element
of the void array, you need to cast it. So the compiler is right to report the
deprecation on line "return a[i]" but I guess it could be more explicit: "void
functions cannot return elements of void arrays. If you want to return the
element, please cast it to the appropriate type."

--


More information about the Digitalmars-d-bugs mailing list