News for hackers. Stuff that matters.
Re:Go Replaces Interface{} with 'Any' by dragonwriter on Tuesday December 14, 2021 @06:46PM (#29559785)
> Java (and Javascript in its attempt to copy it) are about the only languages that actually promote using exceptions for errors

Python does. Ruby does. It's not just Java and JS. Go is very open about its approach being a departure.

> And actually, many APIs in the wild do represent errors as integers.

Many, many APIs in the wild are implemented in (or meant to be consumed from) C, which doesn't even have exceptions, so not using exceptions makes sense for them.

Very often idiomatic non-C host language wrappers for those APIs will fire exceptions when they get an error return.

Re:Go Replaces Interface{} with 'Any' by kortex on Tuesday December 14, 2021 @08:26PM (#29560814)
> Python does

And it's awful. I use EAFP locally (to avoid TOCTOU and the like) at low level interfaces but I don't let it bubble up out of a function scope, because it is a goto in all but name.

I've also been increasingly using the `result` library/data structure. It's incredibly liberating to return an error as an object you can compose into other functions, vs try/catch, which does not compose.

Yes I write python almost like rust, and it's great. Strong types, interfaces, immutable types. It looks nothing like "old school python" but also behaves nothing like it. Gone are the day of "oh it crashed again, fix one line and rerun".

Exceptions should be for exceptional circumstances, not errors.

Edit: I see this is controversial. What do you take objection to? Making your python look less dynamic and more like rust? Try it before you knock it. Python's my favorite language, but I do not agree that many of the common "pythonic" patterns are good at scale.

https://returns.readthedocs.io/en/latest/pages/result.html

Re:Go Replaces Interface{} with 'Any' by Karunamon on Wednesday December 15, 2021 @10:55AM (#29567712)
Maybe I'm just too inexperienced, but I don't see the point of this library.

The linked example shows a really basic sqlalchemy model lookup. What does spewing these new types all over my code get me that returning None or an empty dict/list doesn't without the overhead?

    def find_user(user_id: int) -> Optional[User]:
      user = User.objects.filter(id=user_id)
      if user.exists():
        return user[0]
      else:
        return None
Not only is this idiomatic, it conveys the same semantic meaning. I'm using an IDE, as is anyone else working in a large codebase. I'll be told at the point of invocation that find_user could return None and I need to possibly deal with that.
Re:Go Replaces Interface{} with 'Any' by dragonwriter on Wednesday December 15, 2021 @12:28PM (#29568974)
> Not only is this idiomatic, it conveys the same semantic meaning

No, it doesn’t. For a single computation like this, that pattern is roughly equivalent to Maybe (which contains no information about the case where there is no success result besides that it is absent) rather than Result (which has error information, kind of like an exception, but in the normal return path.)

For a series of computations, the composability of both Maybe and Result means that they are semantically richer.

Re:Go Replaces Interface{} with 'Any' by Karunamon on Wednesday December 15, 2021 @01:00PM (#29569350)
Couldn't I just return a tuple instead if I wanted to give the caller information on the nature of the success or failure?

Also, both Django and SQLAlchemy throw proper exceptions on bad queries or DB errors, which is probably the right thing to do in the average app using these libraries (the exception bubbles up, getting logged, returning the appropriate http error, etc).

I'm not crapping on this library, mind, I just can't find the use case that justifies it.

Re:Go Replaces Interface{} with 'Any' by linkdd on Wednesday December 15, 2021 @01:37PM (#29569892)
It's about function composition.

How do you compose a function that returns an int or an error with a function that takes an int as parameter?

Yes you can split this in multiple steps, or you can use monads to handle the composition for you, making you type less code, giving more information to the typesystem (mypy for python for example) about what is valid and what's not.

This is a completely different programming style, it's functional programming, aka: "how to make functions by composing other functions".

Re:Go Replaces Interface{} with 'Any' by eru on Wednesday December 15, 2021 @12:02AM (#29562296)
I find, that the problem with exceptions in Python is not so much that it is a goto, but that they are a bit like dynamic scoping (vs static scoping of variables), so you can not reason about them statically.

An example: suppose you have a function that does some work with the filesystem, and also calls some user-supplied code. (Perhaps because the user can subclass something etc, or because you are getting a callback, the details don't matter.)

Naturally your function might have some idea how to handle its own filesystem trouble, but you have no clue how to handle any filesystem exceptions that come from user provided code.

It's rather awkward to get this exactly right.

Re:Go Replaces Interface{} with 'Any' by randomdata on Tuesday December 14, 2021 @07:00PM (#29559937)
> Ruby does.

Definitely not. Especially because early Ruby implementations brought huge overhead when exceptions were used, you were strongly advised to only use exceptions for actual exceptions. Ruby was one of the first languages that really started pushing the idea that exceptions should be reserved for exceptions, even if was just for technical reasons.

Those overhead problems have been addressed and are no longer a problem, but the sentiment has continued to ring true. I agree that doesn't stop people from trying to overload them, as I said earlier. But idiomatic? Not at all.

Re:Go Replaces Interface{} with 'Any' by rezonant on Wednesday December 15, 2021 @12:42AM (#29562512)
Ruby has two kinds of exception handling. There is try/catch, this is used for flow control and should not be used for error handling (it doesn't have stack traces). And then there is raise/rescue.

Using try/catch as intended is a bit of an art, but raise/rescue is everywhere. It is absolutely the primary way of handling errors. I think you might be confusing these two.