Last night I was tired of the fact that programming languages may have a function in which we should be able to limit the values assigned to primitive data types.
For example, I have to say that my int variable can have a value from 0 to 100
int<0, 100> progress;
This will then act as a normal integer in all scenarios, except for the fact that you cannot specify values from the range defined in the constraint. The compiler will not compile the code progress=200. This restriction may carry with type information.
progress=200
Is it possible? Is this done in any programming language? If so, what language does he have and what is called this technique?
This is usually not possible. It makes no sense to use integers without any arithmetic operators. Using arithmetic operators, you have the following:
int<0,100> x, u, v; ... x = u + v; // is it in range?
If you are ready to perform checks at runtime, then yes, it supports several major languages, starting with Pascal.
I believe that Pascal (and Delphi) offers something similar with subband types .
I think this is generally not possible in Java and Ruby (well, in Ruby it is possible, it is possible, but it takes some effort). However, I have no idea about other languages.
Ada - , :
type My_Int is range 1..100;
, My_Int 1 100, Ada Constraint_Error.
, Ada. , , .
, , . , " " .
. . , Agda2 ATS (ats-lang.org).
, " " .
: - - -
. , - , (, , t , Integer).
++ , , , . (+ =, - =, * = ..), .
, (, ML ), , , ++.
, . , , / , , , , ? , , , .
! : C. C? C? short Integer? , C .
short
BTW: , , Pascal , . . , , . , , , , .
, , for .
, . , ! , .
:
- , . , .
- , .
, , , . , (, , ).
SQL , . , telephone_number , ..
telephone_number