Comment #0 by default_357-line — 2024-06-10T09:48:16Z
object.destroy is, perhaps appropriately, the most destructive function ever written.
It does something useful (resetting the value and calling the destructor) for structs and struct derivatives, but something horrible (destroying the *class data*) for objects. So you're always tempted to use it, but you have to remember, every time, to disable the cases where it causes silent future crashes.
I forgot this time.
```
module test;
import std.algorithm;
class A {
int i;
int getI() => i;
this(int i) { this.i = i; }
}
void main() {
auto arr = [new A(2), new A(3)];
auto max = arr.maxElement!(a => a.getI);
// Due to maxElement calling extremum which uses Rebindable2
// which calls destroy
// the vtable pointer of arr[0] is now zeroed out.
assert(arr[0].getI == 2);
}
```