On 19/02/2018 02:59, Chris Angelico wrote:
On Mon, Feb 19, 2018 at 1:14 PM, bartc <b...@freeuk.com> wrote:

How would even a type for the odd numbers from 1 to 10 inclusive work?
(That, a type consisting of one of the values in {1,3,5,7,9}.) Would they be
ordered or unordered? Can I do arithmetic with them: will 3*3 work, but not

The type is "positive odd number below ten" and could be written as
int(1..9|1%2). That is an orderable type; you can say that 3 < 7, for
instance. And yes, arithmetic would be defined just fine;

Sometimes, the reason for creating a special numerical type is precisely so you can't do arithmetic on them, if it's not meaningful for the type.

So the special type of the values 65..90 might not allow the type be multiplied or divided, or added to itself. Because they represent characters A..Z. Or house numbers. Or the age of pensioners. (You'd need to convert to ordinary integers, is that is allowed.)

 there's no
requirement for the result of an operation to have the same type as
its inputs:

5 / 2 # two integers

Try that when the type of {1..13} represents playing card ordinal values.

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!

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?


Reply via email to