>>53
How is this purported to be safe
It's not purported to be safe!
You are not understanding what I am saying here. I don't care if it's provably safe; I will reason about whether it is correct myself.
The problem is, if it isn't PROVABLY safe, then the compiler can't AUTOMATICALLY resolve it for you. This means you have to either manage the memory manually or use a garbage collector. A garbage collector will check *at runtime* whether an object needs to be freed, which is the only way to automatically and safely handle memory; that is, as
>>51 was wondering, why most modern languages use them.
Remember, you originally said that if all I want is annotations for memory management, then a compiler could just annotate it automatically. I said no, because it couldn't do it *safely*; it couldn't prove that its optimizations were correct. Halting problem.
I was pointing out that there exists provably safe memory management which is equivalent to the manual case.
There is certainly provably safe memory management in restricted subsets of the language. "All allocations are scoped on the stack, and you cannot make a reference that doesn't live on the stack. Therefore all references go out of scope before whatever they point to." Provably safe memory management -- except this sort of limited case is not actually useful for writing real-world programs. I'm not sure if this is what you meant or not.
There are some nuances in there, and it's not just syntax.
Yeah, yeah. They are *extremely* semantically similar. I'm sure you could come up with some minor difference, so fine, they aren't identical. (For the record, not having to indent code is not a semantic difference.)
Many years ago I wrote a macro in C++ that did exactly what defer does in Go. Here, I just rewrote it. Give it a try:
#include <stdio.h>
#define defer3(f, line) struct DEFER##line {~DEFER##line() {f;}} defer##line
#define defer2(f, line) defer3(f, line)
#define defer(f) defer2(f, __LINE__)
int main() {
printf("a\n");
defer(printf("e\n"));
printf("b\n");
defer(printf("d\n"));
printf("c\n");
}
Of course Go reinvents this as a built-in, since they refuse to support any form of RAII, or something like Python's with statement (among many other things.)
I'm very sorry if I'm ignorant of Go's exceptions, but they did announce the feature a whole twelve days ago.