News for hackers. Stuff that matters.
Re:From Rust to TypeScript by dilap on Saturday September 12, 2020 @12:06PM (#24453428)
Go does indeed use the tedious approach. Rust and Zig have nicer syntax for it.

But personally, I prefer even Go's "tedious" approach to (unchecked) exceptions, simply because it's too easy to miss an error case otherwise.

I also find that knowing where errors occur is important information, so I don't really mind having it add an extra line or two. (But the more minimal Rust/Zig syntax is nicer.)

(This attitude comes from having once worked on a python server application, which involved a lot, "oh shit, that throws" whackamole.)

Re:From Rust to TypeScript by hombre_fatal on Saturday September 12, 2020 @01:15PM (#24453881)
Go's approach is not great. You can leave off return assignment completely. You can't take a tuple as an argument. It has an amateur approach to how you can't shadow `err`, yet `:=` vs `=` change on whether `err` already exists or if your lhs variable is new. You have to do additional work to get a stack trace, errors are usually just strings. You may be forced to handle some errors (unless you trivially ignore them), but nobody is forced to make decent errors. Most libs don't even think about them, like being forced to handle `throw "something went wrong"`.

I like errors as return values (mainly Result<V, E>), but Go's is probably the worst impl I've come across. The reason why it's still productive despite this is because we, as developers, tend to not put much thought in errors anyways, so who cares if it sucks (until we need them).

Re:From Rust to TypeScript by JamesSwift on Saturday September 12, 2020 @02:20PM (#24454303)
The lack of a tuple type is the most frustrating for me, because it means you cant define channels as a type of tuple. You have to go through the boilerplate of defining an option type for every single T channel that you handle.
Re:From Rust to TypeScript by cle on Saturday September 12, 2020 @02:35PM (#24454404)
Couldn’t you just use a pointer?
Re:From Rust to TypeScript by JamesSwift on Saturday September 12, 2020 @03:38PM (#24454933)
When I say 'option type' I actually mean the idiomatic go multiple return `(res, err)`. You can define a function to return that but its not actually a first class T, so you cant define a channel that follows the same semantics. That means if you have a bunch of functions that use the `(res, err)` style and you later want to convert them to, e.g., batch processing steps using channels, you have to now add a lot of boilerplate type definitions to tie things together.
Re:From Rust to TypeScript by cle on Sunday September 13, 2020 @09:55PM (#24465957)
Ah got it. Tuples come with their own challenges and are quite similar to structs, so I’m not surprised they didn’t make the cut. I’ve had to define structs like you say occasionally, but it literally takes like 2 seconds so I don’t really care, and appreciate not having to decide when to use tuples vs structs and argue with teammates about it too.
Re:From Rust to TypeScript by JamesSwift on Monday September 14, 2020 @09:53AM (#24470132)
Youre right, it definitely isnt a large amount of work. Its the annoyance of the work and the leaky abstraction of "multiple returns" that _look_ (and sort of act) like its a tuple. In addition to channels, it also comes up if you want to store an array of return values.
Re:From Rust to TypeScript by zem on Saturday September 12, 2020 @08:56PM (#24457280)
even more annoyingly, you can define a function that returns it but not a function that accepts it, so you often have to jump through a lot of hoops to combine higher order functions with multiple return values.
Re:From Rust to TypeScript by cle on Sunday September 13, 2020 @09:59PM (#24465996)
Not sure what you mean, functions can take multiple return values as arguments just fine: https://play.golang.org/p/l7s7i-QyRnM
Re:From Rust to TypeScript by JamesSwift on Monday September 14, 2020 @09:56AM (#24470177)
_sort of_. Again, the leaky abstraction comes through when you try to add additional params in addition to the multiple return:

https://play.golang.org/p/lwMBkvnaVm0

Re:From Rust to TypeScript by untog on Saturday September 12, 2020 @12:16PM (#24453504)
Personally I find the Rust syntax is a nice middle ground. If the function returns a Result, it can throw an error. And unless you provide a From<> implementation you do need to handle the specific error that gets thrown. But if you want things to bubble up cleanly you provide From<> (or use a crate that provides it for you) and it all works wonderfully.
Re:From Rust to TypeScript by dehrmann on Saturday September 12, 2020 @02:27PM (#24454361)
> python server application, which involved a lot, "oh shit, that throws" whackamole.

This matches my experiences with Python, and docs, even official Python docs, are somewhat lacking when they describe what might get thrown.

Re:From Rust to TypeScript by flowerlad on Saturday September 12, 2020 @12:09PM (#24453459)
> "oh shit, that throws" whackamole

You don't have that problem in languages that support checked exceptions.

Re:From Rust to TypeScript by ufmace on Saturday September 12, 2020 @01:22PM (#24453929)
I've never seen a language that supported checked exceptions and actually used them consistently for everything.

But then that would be kind of awful, because IMO the checked exceptions cases for "I know this will never actually throw, leave me alone" and "This is just a quick script, go ahead and blow up the whole program if this fails" are poor, forcing you to write try catch blocks all over the place.

IMO, Rust has a nice approach for being stricter - unwrap. If it's a quick script, throw unwrap everywhere with reckless abandon. For real programs, grep for unwrap and make sure for every one, you really know for sure that it will never fail or that blowing up the whole program if it does is the right move. To convert from one to the other, grep unwrap and actually think about what you want to happen if that line is Err.

Re:From Rust to TypeScript by kibwen on Saturday September 12, 2020 @12:51PM (#24453719)
You may still have that problem if the language also supports unchecked exceptions.
Re:From Rust to TypeScript by dilap on Saturday September 12, 2020 @12:15PM (#24453498)
Agreed, mostly true.

(It's still not as explicit about where your function might exit, but you won't get surprise exceptions, which is way nicer.)

Re:From Rust to TypeScript by dehrmann on Saturday September 12, 2020 @02:25PM (#24454342)
You do and you don't. If you're using a Java library written by someone who doesn't believe in checked exceptions in Java, you might be lulled into thinking everything will be fine.