On Mon, 19 Feb 2018 12:35:19 +0000, bartc wrote:

> Type systems get rapidly very complicated when you have to deal with
> arbitrary sets of values and with arbitrary rules of interaction.
> Someone has to devise a programming language to allow all that without
> tying itself up in knots. Someone else has to program in it. And someone
> else has to try and understand it!

Indeed. That's exactly the point I'm making.

A type-system that allowed you to express extremely fine-grained 
distinctions would be effectively a fully fledged programming language 
itself, probably even Turing Complete, and so our type-specifications 
would be as error-prone as the code we write now.

Probably more so, due to the requirements that type specifications be a 
lot more compact than the rest of the code we write.

> Ones like C++ has already tied itself itself up in knots just doing the
> basics; I'm not sure how it would handle even my 1,3,5,7,9 type.
> But Python has classes and can do some of this stuff; how would it
> handle a numeric type that is constrained to be whole numbers within
> 0..9 inclusive?

This becomes easy at run-time:

class Digit(int):
    def __new__(cls, arg):
        instance = super().__new__(cls, arg)
        if not 0 <= instance <= 9:
            raise ValueError('argument is out of range')
        return instance

The above is obviously not a full-blown production-ready class. But it 
illustrates the basic concept. This is the sort of thing that dynamic 
languages excel at: enforcing constraints at run-time which are hard to 
enforce at compile-time.



Reply via email to