Replies: 6 comments 36 replies
-
Related to #3194. |
Beta Was this translation helpful? Give feedback.
-
see also it looks like in 16 Juan said that this will not happen, but many years have passed and gdscript is now rewritten to 2.0 :)) |
Beta Was this translation helpful? Give feedback.
-
well I think this can be already done with godot 4, since Callables are implemented you can create a |
Beta Was this translation helpful? Give feedback.
-
You can take advantage of the fact that AND and OR operations are lazy: func func1():
print(1)
return OK
func func2():
print(2)
return ERR_CANT_OPEN
func func3():
print(3)
return ERR_ALREADY_EXISTS
func _run():
func1() or func2() or func3()
# Prints:
# 1
# 2 A little longer, but in my opinion a more idiomatic version: func1() == OK and func2() == OK and func3() == OK There are also other options, for example, as written above, there is However, I don't see the problem in Example 1: repetition is not always bad. It is bad when large pieces of code are repeated in different parts of the application. When 1-2 lines are repeated next to each other (as in your example) - this is a common thing, there is nothing to fix here. Unless you can use the following option, it is shorter: var err = func1()
if not err:
err = func2()
if not err:
err = func3()
return err Personally, I really dislike exceptions, in my opinion they cause much more problems than a few extra lines of code. Exceptions are based on the vicious idea of deferring error handling until later, somewhere up the call stack. Exceptions break the order of program execution and require tracking of potentially the entire graph of application function calls. I prefer return codes or compound return results (with an error status). |
Beta Was this translation helpful? Give feedback.
-
I might sound rude, so apologies for that, but can someone in Godot team elaborate (or share the link if already elaborated) why they are against try/catch, except the "images folder" analogy (that's basically a faulty generalization) and weird takes like "better crash than handle bad code"? |
Beta Was this translation helpful? Give feedback.
-
I've stated at the top that "The title is a clickbait. I DO NOT propose adding try-catch to gdscript. I would merely like to discuss alternatives." If gdscript had type unions and a native Exception variant, we could write things such as: func get_someone()->Node|Exception:
var node:Node = get_node_or_null('../some/node')
if node != null: return node
return Exception.new('node not found')
func other_func()->void|Exception:
var node:Node|Exception = get_someone()
if node is Exception: return Exception.new('I needed some node that was not found')
node.do_something() Wich whould help a lot in implementing and using methods that may or may not suceed as expected. Type unions could be implemented with type erasure, so that they would only validate at compile time, but the runtime would use just Variant, wich wouldn't need any change in the runtime, only in the gdscript parser/compiler. There is already an older proposal with 98 positive reactions about type unions: |
Beta Was this translation helpful? Give feedback.
-
Describe the project you are working on
A 2d platformer
Describe the problem or limitation you are having in your project
none
Describe the feature / enhancement and how it helps to overcome the problem or limitation
The title is a clickbait. I DO NOT propose adding try-catch to gdscript. I would merely like to discuss alternatives.
It has already been stated that try-catch will never come into gdscript. I'm perfectly fine with that.
godotengine/godot#3516
However, I would like to discuss with other users if there is another way of making life easier for those that need to deal with a lot of error handling. This "making life easier" could or could not involve changes to gdscript.
Currently gdscript encourages usage of int error codes for error handling. So, a user that has a series of function calls that need to be checked can make it this way:
The ifs can become single liners
And let's not forget that gdscript supports semicolons
If I'm not mistaken, that's the limit with current gdscript.
Try-catch will never come. That's okay. If it did come, it could be like this
But it will never come, and that's totally fine.
So, what can be done to make the user experience more pleasant, similar to a try-catch?
Perl 5 has an interesting concept called "default variable". It is a local variable that is already declared in any context. Functions with apparently discarded return values instead have the return values assigned to the default variable. And single parameter functions called without the parameter actually use the default variable as argument.
If gdscript had a perl like default variable named _ (underscore), or any other name, like _default, we could rewrite example 3 like this:
The if without an expression uses the default variable, and the return without value uses the default variable also.
But whe could go further and create a new language construct, a pseudo-function called catch(), that would be replaced by the parser with "if _default: return _default". If this pseudo-function existed, we could rewrite example 5 like this
or like this
or maybe catch() could receive the function call as argument, so example 6 could be rewritten like this:
since the function return values are being stored in the default variable, the value can be reused in a chain of function calls
But since in perl a missing function parameter uses the default variable, writing the _default as parameter for func2() and func3() would be optional.
I know that a real exception handling system would be complex, since an exception could be thrown anywhere in a code block, like in a nested function call
But leveraging code replacement, we could implement a poor man's try-catch using the parser alone, with the limitation that it would only "catch" exceptions in "root" statements, not in nested ones.
So, a poor man's try-catch would be written like this
and would be transpiled to the manual error handling scheme
Without a catch block, without finally, just a try without catch.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
already described above
If this enhancement will not be used often, can it be worked around with a few lines of script?
already described above
Is there a reason why this should be core and not an add-on in the asset library?
already described above
Beta Was this translation helpful? Give feedback.
All reactions