Replies: 8 comments 2 replies
-
I'd consider using union types for errors. Especially since Typescript has such nice support for them. |
Beta Was this translation helpful? Give feedback.
-
I use a lot of But by default I think it makes it too easy to silence errors, and makes the code too verbose.
Rust's question mark is actually really nice, although there is not a nice TS counterpart for it. |
Beta Was this translation helpful? Give feedback.
-
Personally, I like exceptions only as a means of handling "exceptional" errors. For example, in something like a request router or a middleware stack, being able to put an exception handler at the top makes a lot of sense, if you don't want the entire service to shut down because of an error in one controller or middleware component. On the other hand, when developers throw exceptions for completely normal and expected conditions, is where exception handling as such begins to fall apart. For example, there is nothing exceptional about trying to open a file that doesn't exist, or attempting to open a port that isn't available - these are completely normal and expected conditions, and handling them really shouldn't be optional. I think I'd like to see a language with two mechanisms for this: one for "exceptional conditions you should handle", and another for "errors you probably only want to handle in an error-handler". Interestingly, the exception class in JS is actually called One answer for "exceptional conditions you should handle" of course is If we're going to have A few options come to mind: 🤔 Separate base types for 🤔 Something like Java's 😀💡 Some means of explicitly throwing exceptions "directly at" the caller. I actually don't know if any language has this? But some simple annotation on the The latter could optionally come with some kind of shorthand alternative to the wordy |
Beta Was this translation helpful? Give feedback.
-
Have you considered using something similar to Rust's Option/Result? Sum types force a user to handle the error, and in my opinion, are more ergonomic than Java-style checked exceptions as they are "built-in" to the return type. They're similar to using null, and could be implemented in a more simple manner than Rust enums, but are safe. |
Beta Was this translation helpful? Give feedback.
-
I like having an official But it's also a little ugly. One thing I find cumbersome in Rust is constantly unwrapping results. The ergonomics are also difficult when you think about how |
Beta Was this translation helpful? Give feedback.
-
I like how |
Beta Was this translation helpful? Give feedback.
-
So I think I have settled on basing error handling on simple type elimination from union types. type Result<Value, Error> = { value: Value } | { error: Error } The compiler can eliminate possible type signatures through static code analysis and fail to compile in situations where the value could be either option. const result = readFile("/path/to/file.txt") // Result<string, FileReadError>
if ('error' in result) { // Compiler determines that this if statement can only execute if
console.log(result.error) // the error type exists which also means the value option cannot exist
// Trying to access result.value here will result in a compiler error
process.exit(1) // By exiting here, the compiler knows that the error option
// is not possible beyond this point
}
console.log(result.value) // By process of elimination, the compiler knows that result.value
// is the only possibility at this point The advantage of this is:
I imagine we will still need a panic mechanism, but that will not be the primary method of error handling |
Beta Was this translation helpful? Give feedback.
-
Ok I take this back, I feel that BorrowScript should have a Exceptions lose type information and tuple return types are cumbersome/repetitive to handle. function foo(): Result<string, IError> {
const value = willError()? // returns Result<string, IError>
}
function willError(): Result<string, IError> {
Err(new Error("Hello world"))
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Exceptions are great ergonomically. They might be expensive as there is additional runtime code that needs to be shipped into the binary to support them.
In the first version, I will recommend using Go style tuples with a nullable error as the second type.
Later, once we have a working compiler, if the overhead of adding exceptions isn't too great, we will have a discussion on their introduction.
Beta Was this translation helpful? Give feedback.
All reactions