-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
errors... #10
Comments
You could also ignore errors by the following func add(a, b int32) (int32, error) {
return errors.New("This function failed")
}
func main() error {
//
result := add(0, 1) catch () {}
log.Println("result:", result)
} This makes the code ugly if the developer does NOT handle the error (which would encourage error handling)... whereas currently golang discorages it thanks to |
I'd like us to tackle error handling, and I like the catch block idea, but I do like rusts func add(a, b int32) (int32, error) {
return errors.New("This function failed")
}
func main() error {
result := add(0, 1)?
log.Println("result:", result)
} I think the main reason is the fact that Zig's solution doesn't prevent the excessive x := []int32{
1,
2,
add(1, 2)?,
} or result, err := add(3, add(1, 2)?) WDYT? |
func main() error {
result := add(0, 1)?
log.Println("result:", result)
} From this code, what would be printed out if result was the error type? I just don't fully understand it tbh haha And in this example: x := []int32{
1,
2,
add(1, 2)?,
} what would be stored at I really love the conversing and thought provoking ideas going on here |
Looking back at what you said, I thought about always having to func main() error {
// if error caught then it gets returned immediately
result := add(0, 1) catch
log.Println("result:", result)
} WDYT? |
That's exactly the ? operator 👍 in case of an error return it immediately. func main() error {
result := add(0, 1)?
log.Println("result:", result)
} Is identical to func main() error {
result, err := add(0, 1)
if err != nil {
return err
}
log.Println("result:", result)
} In the other example x := []int32{
1,
2,
add(1, 2)?,
} I guess it transpiles down to this: temp, err := add(1, 2)
if err != nil {
return err
}
x := []int32{
1,
2,
temp,
} |
If you agree we can push it to the spec readme |
How would this work if I change the signature of the add() function
If the catch part ignored, or does it give a compile error? |
Compiler error for sure.
if var1, var2, ..., varN, err := callExpression(...); err != nil {
return zero1, zero2, ..., zeroM, err
} where |
Could the transpiler be "that intelligent" when there is no error in the signature, it will not generate that if statement? |
For sure, it's for us to implement so we can definitely go either way. I think it's mostly important during refactors and changes. For example, if i have the following code: func fetchAndPrint() error {
value := fetchData()?
fmt.Println(value)
}
func fetchData() (string, error) {
// do some IO operation
return "", nil
} And I want to refactor so that fetchData does not return error anymore: func fetchAndPrint() error {
value := fetchData()?
fmt.Println(value)
}
func fetchData() string {
// non-IO operation
return ""
} now func fetchAndPrint() {
fmt.Println(fetchData())
}
func fetchData() string {
// non-IO operation
return ""
} |
Imagine the following syntax for errors
Idea:
If a function returns an error you must have to "catch" it. No more
if err != nil
functions in C, C#, C++, Rust, Javascript, and Zig can only return 1 value. The problem was "how can you stick an error in there?" Go did the most obvious (and least creative thing) and just allows you to return multiple types. It's simple, and boring, just like go. I want to keep that spirit. It also forces "one way" of handling errors. Not sure of this syntax' implications but I like it.
It is not a "try catch" but uses the "catch" keyword which should be very familiar to developers.
Rust's approach is not pretty, but it works. It wraps errors and values in a
Result
type. I don't really like this as much as Zig's approach.The text was updated successfully, but these errors were encountered: