Thread overview
[phobos] returning void[] from std.file.read()
Sep 26, 2010
Jonathan M Davis
Sep 26, 2010
Michel Fortin
Sep 26, 2010
Robert Jacques
Sep 26, 2010
Jonathan M Davis
Sep 26, 2010
Robert Jacques
Sep 27, 2010
Denis
September 25, 2010
This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.

What are the pros and cons of returning void[] vs. ubyte[]?


Andrei
September 25, 2010
On Saturday 25 September 2010 14:17:58 Andrei Alexandrescu wrote:
> This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
> 
> What are the pros and cons of returning void[] vs. ubyte[]?

The biggest pro to returning ubyte[] is that it's not so confusing as to what on earth is being returned (what does void[] really mean anyway? It's pretty totally unusable unless you cast it). Also, ubyte[] would be properly indexable, and I don't think that void[] is, since void doesn't have a size by which to index. I'm not quite sure why you'd actually *want* void[], since ubyte[] is useable, void[] is not, and you're probably going to have to cast it to something else anyway (or use File's rawRead() instead of std.file.read(), as was suggested to me when I asked about this on D.Learn recently - though that could certainly use some enhancements).

- Jonathan M Davis
September 25, 2010
Le 2010-09-25 ? 17:17, Andrei Alexandrescu a ?crit :

> This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
> 
> What are the pros and cons of returning void[] vs. ubyte[]?

I see no reason to return void[]. The concept of a file is a sequence of bytes, not a sequence of voids. Yes, you might want to cast the file content's to various other data formats, but I fail to see how void[] is better than ubyte[] in that regard.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



September 25, 2010
On Sat, 25 Sep 2010 17:17:58 -0400, Andrei Alexandrescu <andrei at erdani.com> wrote:

> This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
>
> What are the pros and cons of returning void[] vs. ubyte[]?
>
>
> Andrei

Pro:
Well, the very first thing I always do with std.file.read is to cast the
data to the type I'm working with. So:

auto data = cast(float[])read("my_data.raw");

vs.

auto data = cast(float[])(cast(void[])read("my_data.raw"));

Having read return ubyte would mean adding an extra cast in several use cases

Con:
A void[] array is treated as having pointers in the GC, so extra false
pointers could be an issue.
September 25, 2010
On Saturday 25 September 2010 19:46:37 Robert Jacques wrote:
> On Sat, 25 Sep 2010 17:17:58 -0400, Andrei Alexandrescu
> 
> <andrei at erdani.com> wrote:
> > This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
> > 
> > What are the pros and cons of returning void[] vs. ubyte[]?
> > 
> > 
> > Andrei
> 
> Pro:
> Well, the very first thing I always do with std.file.read is to cast the
> data to the type I'm working with. So:
> 
> auto data = cast(float[])read("my_data.raw");
> 
> vs.
> 
> auto data = cast(float[])(cast(void[])read("my_data.raw"));
> 
> Having read return ubyte would mean adding an extra cast in several use cases

Why would you need two casts? I would expect that one would suffice without the need to cast to void[] in between.

- Jonathan M Davis
September 26, 2010
On Sun, 26 Sep 2010 00:32:41 -0400, Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> On Saturday 25 September 2010 19:46:37 Robert Jacques wrote:
>> On Sat, 25 Sep 2010 17:17:58 -0400, Andrei Alexandrescu
>>
>> <andrei at erdani.com> wrote:
>> > This has come up in
>> http://d.puremagic.com/issues/show_bug.cgi?id=1482.
>> > Should std.file.read() return void[] or ubyte[]? There's one matter
>> with
>> > assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate
>> type
>> > for describng raw bytes.
>> >
>> > What are the pros and cons of returning void[] vs. ubyte[]?
>> >
>> >
>> > Andrei
>>
>> Pro:
>> Well, the very first thing I always do with std.file.read is to cast the
>> data to the type I'm working with. So:
>>
>> auto data = cast(float[])read("my_data.raw");
>>
>> vs.
>>
>> auto data = cast(float[])(cast(void[])read("my_data.raw"));
>>
>> Having read return ubyte would mean adding an extra cast in several use cases
>
> Why would you need two casts? I would expect that one would suffice
> without the
> need to cast to void[] in between.
>
> - Jonathan M Davis

Sorry, you're right. Though I'm surprised at this behavior, since doing a similar reinterpretation cast between incompatible classes require going through void* first.
September 27, 2010
I think it should return ubyte[].

-Lars



On Sat, 2010-09-25 at 16:17 -0500, Andrei Alexandrescu wrote:
> This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
> 
> What are the pros and cons of returning void[] vs. ubyte[]?
> 
> 
> Andrei
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


September 27, 2010
I also think it should return ubyte[].

On Mon, Sep 27, 2010 at 10:56 AM, Lars Tandle Kyllingstad <lars at kyllingen.net> wrote:
> I think it should return ubyte[].
>
> -Lars
>
>
>
> On Sat, 2010-09-25 at 16:17 -0500, Andrei Alexandrescu wrote:
>> This has come up in http://d.puremagic.com/issues/show_bug.cgi?id=1482. Should std.file.read() return void[] or ubyte[]? There's one matter with assuming that void[] may contain pointers (which is not the case for something read from a file), so possibly ubyte[] is a more accurate type for describng raw bytes.
>>
>> What are the pros and cons of returning void[] vs. ubyte[]?
>>
>>
>> Andrei
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>
>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>