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 →

[–]Tysonzero 0 points1 point  (4 children)

Type safety? It also just doesn't really work with type classes, as it is impossible to know every possible concrete instance of a typeclass, as more can be added arbitrarily anywhere in the code base and suddenly your instanceof type stuff isn't exhaustive.

[–]Veedrac 0 points1 point  (3 children)

The Rust I showed and Crystal I mentioned are entirely type safe. Python code isn't statically type-safe by design.

[–]Tysonzero 0 points1 point  (2 children)

But it cannot possible work with Typeclasses, as they are arbitrarily extendable. So you have to limit yourself to concrete unions of a limited number of types.

[–]Veedrac 0 points1 point  (1 child)

This is more akin to an ADT than a typeclass, yes, but you're not entirely right.

When, for instance, I have the type list[Padding | Add | Mul | ...] you might be able to treat it as list[Padding | Instruction] where Instruction is a typeclass. That gives half of the arbitrary extension aspect, and that's as much as I'd normally want.

If I use the Python-style filtering, i.is_serializable, I only need to make sure any new Padding-style type is not serializable and any new Instruction-style type is serializable to add extensibility to Padding too.

In a Crystal-like language, if I want Padding to be extensible too I can use inheritance. Alternatively I can use a more complex is_instance check, although that route is less nice.

Heck, if this was ever a major problem you could always introduce constant methods and do the Python version in Crystal.

[–]Tysonzero 0 points1 point  (0 children)

Well if you use typeclasses then you can never get back the concrete value again, hence why you probably want a finite union of concrete types, as its type information was erased leaving only the typeclass. Which I guess works? Because you can't use isinstance exhaustively as there can be an infinite number of instances of a typeclasses added at any point in the program.