I was wondering what is the “best” was in odin to handle this error return case. I’ve not managed to find any discussion of exactly this.
Pretend I have a function “render_init()” that initialised a render system for a game. All my code really needs to know is “did this work” to determine if it can continue to run the application. So returning a bool “ok” is probably fine.
For debugging though it is useful to return more than that, perhaps a string that the higher level code can either log, or display in an error panel or something.
In standard C++ you might use an exception. If an exception is thrown of any kind, the operation failed. If you want to look into the exception to get a message you can.
My thoughts are to return both a bool and a string, but then it’s more ugly to handle in the caller if you only care about the bool, or perhaps I could just return a string and if it’s empty there is no error. But that seems ugly.
I did look maybe to return this :-
Err_Type :: union {
string
}
Then could return a string or a nil if there was no error . it easy for code to check for nil if you don’t care what the error was but the text is there if you want it. But then allocating and disposing of the string becomes a bit painful.
Is there any good way to do this? references to other discussions I’ve not managed to find would be welcome!