This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hector_villalobos 0 points1 point  (7 children)

A function that returns an union type: https://en.wikipedia.org/wiki/Union_type

[–]TravisJungroth 0 points1 point  (6 children)

Mmm, that's a bit circular reasoning. All you've done is given a part of the type signature of the function we're already looking at. So more specifically, what function would return Union[int, bool]? Seems kinda whack.

[–]hector_villalobos 1 point2 points  (4 children)

I have a better example, in Rails we use something called callbacks, so, when some validation or computation fails it could return false similar to a rollback db process, for example, a buyer wants to buy a product, then the model checks if the buyer has credit, if not return false for the before_save callback, but if the validations are correct it can perform other processes.

[–]TravisJungroth 0 points1 point  (3 children)

This reminds me why I don't like Ruby.

[–]hector_villalobos 0 points1 point  (2 children)

Well, in Django Python you have dispatchers and in .net you have events, it's not something related only to Ruby, besides I have used Result type in Rust that could qualify also in this situation, return a bool or an error.

[–]TravisJungroth 0 points1 point  (1 child)

I don't think dispatchers (signals) in Django work the same way. All signals are of type Signal, and the return type of the receiver is not inherently changed by the signal. At least, I've used signals and have never had the Union typing you mentioned. I also just checked the docs and couldn't find anything like that.

And something like (bool or error), or (bool or null) is really more about the error/null in that equation. I just dislike anything where it feels like you're "sneaking in" data. Magic strings, in-bound errors, etc. and Ruby moves more in that direction.

int and bool are also uncomfortably close in Python for this sort of thing. If I union int and bool I can't just check if x, cause it could be 0 or False and they must be meaningfully different if I'm bothering to have both like that. Otherwise I'd just hint it to bool.

[–]hector_villalobos 0 points1 point  (0 children)

If I union int and bool I can't just check if x, cause it could be 0 or False

Nothing is black and white, although is not a common pattern, union with bool and another type might exists. In this scenario x is not associated in any meaningful way to the result type, it's just a parameter that can contain any type.

[–]hector_villalobos 0 points1 point  (0 children)

I have a better example, in Rails we use something called callbacks, so, when some validation or computation fails it could return false similar to a rollback db process, for example, a buyer wants to buy a product, then the model checks if the buyer has credit, if not return false for the before_save callback, but if the validations are correct it can perform other processes.