On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:
On 11/1/21 2:01 PM, Ali Çehreli wrote:
> The program is as incorrect as its C equivalent would be. ;)
I wrote a cool function to make it easy to disregard memory
safety:
import std.stdio;
auto assumedLength(S)(ref S slice) {
struct LengthSetter {
void opAssign(size_t length) {
// Nooo! :)
*cast(size_t*)(&slice) = length;
}
}
return LengthSetter();
}
void main() {
auto arr = [ 1, 2, 3 ];
arr.assumedLength = 10;
writeln(arr);
}
Joking aside, I liked the nested struct and its opAssign to
mimic internal `arr.length = 42` syntax. (I know it involves a
potentially expensive delegate but still...)
Ali
This function seems smart and flexible and higher than my current
level, I'll study it.