Casting non-aliased mutable arrays to immutable in return values
Per Nordlöw via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 29 09:09:17 PDT 2017
Is it recommended to cast an unaliased array to `immutable` after
it has been initialized?
The reason for asking is that I would like the following function
void[] rawReadNullTerminated(string path)
@safe
{
import std.stdio : File;
auto file = File(path, `rb`);
import std.array : uninitializedArray;
ubyte[] data = uninitializedArray!(ubyte[])(file.size + 1);
file.rawRead(data);
data[file.size] = 0; // null terminator for sentinel
return data; // TODO can we cast this to
`immutable(void)[]`?
}
to have an immutable return type.
I'm aware of that I need to verify the contexts as UTF-8 if I
want to treat it as text.
More information about the Digitalmars-d-learn
mailing list