Hi all. Well, I glad to see how young and brave web programmers trying to create a language suitable for system programming. It's looked like an alternative evolution. Not bad for me, may be we will find a new good ideas. But I am old enough and I want give some historic explanation about the reasons of the error handling in the old languages. Looked like the authors of GoLang and Rust is too young and don't know it. At very ancients times the program languages operated with addresses of commands. Look at example, I believe (but not sure) that it was able be runned on ZX Spectrum.
10 LET i=1
20 GOTO 40
30 PRINT ",";
40 PRINT i;
50 i=i+1
60 IF i<5 THEN 30
70 PRINT
BTW, with such paradigm special operators for "while loops" were not needed. Well, very ugly code and structural program languages were raised, where programmer operates with not addresses, but with blocks of code. Language C, for instance, was planned be a truly structural language, but it keep GOTO operator just for unknown cases. And, after of years of using C, revealed for what GOTO is still needed. And this still prevented C to be true structural language. GOTO was needed only for error handling. Look at very simple example on C.
#include <stdio.h>
int main(void)
{
int rc = puts("Hello World");
if (rc == EOF)
perror("fputs()"); // POSIX requires that errno is set
}
But what to do, if we want to write many different strings. Well, will be a lot of puts() functions, but we do not need to do error handling block separately for each of them. Error from the any fputs() function may be processed in the same manner. Thats why GOTO was needed.
#include <stdio.h>
int main(void)
{
int rc;
if (EOF==puts("Say: A"))
goto error_handling;
if (EOF==puts("Say: 'B'"))
goto error_handling;
if (EOF==puts("Say: 'C'"))
goto error_handling;
if (EOF==puts("Say: 'D'"))
goto error_handling;
if (EOF==puts("Say: 'E'"))
goto error_handling;
return 0;
error_handling:
perror("puts()"); // POSIX requires that errno is set
//to do some complex error handling
}
Thats why Exception was invented in almost all program languages and due to this GOTO was forgotten. And ancient languages became true structural. Well, was very funny for me to see that in GoLang exceptions were abscent. And now lets discuss what exists in Rust. https://doc.rust-lang.org/book/ch09-00-error-handling.html Well, exists macro panic!() and special return type Result<T, E>. The first looked like exit(1) in C, while the second in C like returning error code of function in the static variable errno. Nothing new. There is not "exceptions", but what about the code pattern, that I wrote above? Is in Rust something like (fantastic language): BEGIN puts("Say: 'A'") puts("Say: 'B'") puts("Say: 'C'") puts("Say: 'D'") puts("Say: 'E'") EXCEPTION perror("puts()"); // or other, more complex, routine END Well, something exists in the Recoverable Errors with Result - The Rust Programming Language To do something to simulate such pattern in Rust need to add "puts()" into the separate internal function, use operator ? at the end of all such functions and do exception handling in the external function. This is not fun at all and looked like ugliness. First of all, syntactic sugar operator ? can work with Result return type or with Option return type, but not with both. This is not good. Trivial example is the function, that searches substring in a string. What to do if such function don't find substring? Return None or Error? Of cause None, because this is not an error. Thats why I like Apple Swift and dislike Python. BTW, Rust is good in this too. But what about if we need search a file for a string? Such function must return None, if not found and Error if there will be errors in reading of the file. So both variants is needed. This can be easily made by encapsulate Option inside Ok of Result. But syntax sugar operator ? must support such use case too. Second, this is not fun to create unnecessary function only to emulate exception handling. I don't know real reason, why you didn't make "'exceptions". May be was good reason. But, at least there must be example in the Chapter " Error Handling" how to emulate the simple and transparent block of code with exception handling (example above) from other languages inside Rust. May be there is possible to create an anonymous function (closure) with external error handling to simulate the habitual block of code with section for the exception handling? Will be such example too complex or badly readable? Is this good for programmers and Rust?