News for hackers. Stuff that matters.
Re:Rust is a hard way to make a web API by mc10 on Friday January 15, 2021 @07:52PM (#25798755)
I’m curious what your alternative is, given that Go famously also lacks sum types.
Re:Rust is a hard way to make a web API by damnyou on Friday January 15, 2021 @07:53PM (#25798761)
Yes, Go is a fundamentally broken language too. As is C++, as is C. Sum types are the absolute bare minimum a language must have for me to not consider it broken beyond hope.

Go in particular is really unfortunate for a language so new. The language's weaknesses have dropped my estimation of its creators by several notches.

Re:Rust is a hard way to make a web API by Guthur on Friday January 15, 2021 @07:58PM (#25798781)
"I'm broken and can not use a language without Sum Types"

There, I fixed it for you.

Re:Rust is a hard way to make a web API by damnyou on Friday January 15, 2021 @07:59PM (#25798793)
Yes, that's possible. I would rather tear my eyes out than use a language without sum types. The benefits of sum types in being able to model arbitrarily complex domains are extraordinarily massive.

Every language has product types. Every programmer understands how useful product types are. Why in the world would the same not apply to sum types?

Re:Rust is a hard way to make a web API by AnimalMuppet on Friday January 15, 2021 @08:11PM (#25798877)
You sound like you have a hammer, and so every problem looks like a nail.

Sum types can be useful. Other aspects of languages can be useful, too. Why fixate on that one? And if you're going to say that it's a minimum bar, well, some of the other features are missing in most languages that have sum types.

Pick the language that has the total set of features that makes it easiest to write whatever program you're trying to write. Don't get locked in to focusing on only one feature.

Re:Rust is a hard way to make a web API by Twisol on Friday January 15, 2021 @09:29PM (#25799400)
I work with Java in my day job; it's by no means my favorite language, but I can be quite productive in it.

I haven't kept a tally of my frustrations with Java, but I can assure you that the most frequent is its lack of first-class sum types. Every time I need to encode (encode!) such a basic concept as "it could be this or that" using subclasses and some Design Pattern to Manage the Variants (usually Visitor, sometimes State), I metaphorically weep for my soul.

There are plenty of other minor gripes I have with the language -- there have been multiple instances where generic generics (HKTs, `class Foo<F<_>>`) would have made things much clearer -- but sum types are certainly the most prominent.

Re:Rust is a hard way to make a web API by damnyou on Friday January 15, 2021 @08:37PM (#25799056)
Yes that's why I write Rust.

Our field has a terrible reputation for quality. In my estimation roughly 70-80% of that is directly or indirectly because of the lack of sum types.

Directly: people don't have the right tools to make invalid states in their business logic unrepresentable.

Indirectly: the lack of sum types results in broken null and error handling.

Re:Rust is a hard way to make a web API by AnimalMuppet on Sunday January 17, 2021 @10:16AM (#25811310)
Late reply; I hope you see it. My reply is late because I had to think for a day first. So, my sincere congratulations - a post that makes me think for a day is much rarer than a post I agree with.

To me, sum types for error handling are isomorphic to checked exceptions. They both let you do dual-track programming - separating the normal path from the error path. Both have compiler support for enforcement. But checked exceptions are no longer considered to be the answer. What went wrong?

The problem turned out to be the programmers. They did at least two things that subverted checked exceptions.

First, they silently ate exceptions (that is, had an empty catch block just to make the exception go away. This is the equivalent of having a sum type that is either an integer or Nothing, and a function. Rather than return the sum type, the function returns an integer. In the Nothing case, the function just returns 0. That's about the same as the empty catch block to not have to declare the exception in the function's return type.

The second way checked exceptions went wrong was the opposite. When a function could throw Exception1, Exception2, Exception3, and Exception4, it was tempting to just declare it as throwing Exception (the base class). In the same way, a function that gets SumType1 back from one function, SumType2 back from another function, and SumType3 from a third function may return the sum of the sum types. It becomes an Everything type.

In both cases, the problem was that programmers were lazy. But here we are 20 years later, and programmers are still lazy. Until the programmers change, sum types won't fix things any more than checked exceptions did.

Re:Rust is a hard way to make a web API by damnyou on Sunday January 17, 2021 @08:08PM (#25817086)
Thanks for the thoughtful response. I spent a bit of time thinking about it, and thank you for that as well.

So, I think you're right about a result type being isomorphic to checked exceptions, for the reasons you laid out. If you compare it to Rust, what ends up happening is similar -- many libraries return an error type that's a union of all their dependencies' error types, and binaries end up using an "everything" anyhow::Error type in the end. However, I think where sum types in general end up working and checked exceptions don't are:

1. Sum types don't encourage a mix of checked and unchecked exceptions the way Java does. Translating from Rust, all exceptions that are meant to be handled by regular users are checked. The only exceptions that are unchecked are broken invariants (panics), which usually end up being handled either through aborting the program or through some sort of top-level restart logic. You could write Java in that style but it's not the ecosystem's convention.

2. Sum types are more general than checked exceptions: they can be used to express nulls and business logic as well. I suspect checked exceptions would have worked better if Java also had nullable and non-nullable types, because nullability is such a common source of errors.

3. You're right that you can drop an error on the floor with sum types as well, just like an empty catch block with exceptions. But that just doesn't happen nearly as often in practice, because result types form a closed set. With checked exceptions, in practice you often end up with a method throwing both checked and unchecked exceptions, and the same syntax is used to handle both. I think checked and unchecked exceptions are fundamentally very, very different and mixing the two is a mistake.

Once again, thank you for your response.

Re:Rust is a hard way to make a web API by bombela on Saturday January 16, 2021 @12:42AM (#25800364)
Can anybody explain why the parent is being downvoted?

We can argue on the percentage. But the point on the direct and indirect damages makes a lot of sense.

Re:Rust is a hard way to make a web API by Guthur on Friday January 15, 2021 @10:10PM (#25799659)
At this stage I'm now convince you're trolling us all. Well done you had us all fooled :)
Re:Rust is a hard way to make a web API by damnyou on Friday January 15, 2021 @11:05PM (#25799923)
Not trolling. Entirely serious.
Re:Rust is a hard way to make a web API by exclusiv on Friday January 15, 2021 @08:29PM (#25799011)
Also - "I refuse to use most languages that have been used to solve a shit ton of problems".

Or, "I will not use any language which made software eat the world"

Re:Rust is a hard way to make a web API by timmattison on Friday January 15, 2021 @08:58PM (#25799182)
Does the Either type from the vavr library for Java cover this? https://www.baeldung.com/vavr-either
Re:Rust is a hard way to make a web API by PhantomBKB on Friday January 15, 2021 @11:24PM (#25800019)
I wouldn't say no, because it's built with the right spirit in mind. But at the same time, it's different from languages have native support for sum types. Advantage of sum type is that it can be matched exhaustively like an enum, but at the same time it's flexible to contain any data like a data class.
Re:Rust is a hard way to make a web API by damnyou on Friday January 15, 2021 @11:08PM (#25799934)
Having a type available in an optional library is not the same thing as having them baked in from the beginning. The absence of sum types warps a language in grotesquely terrible ways, such as Go's err != nil patterns or Java's exception system and visitor patterns.
Re:Rust is a hard way to make a web API by benibela on Saturday January 16, 2021 @02:46PM (#25804849)
C has unions

What more do you need?

Re:Rust is a hard way to make a web API by damnyou on Saturday January 16, 2021 @06:15PM (#25806450)
Are unions used for error handling and null checking? No? Then they're not very useful.