Does any programming language support limiting restrictions on primitive data types?

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.

Is it possible? Is this done in any programming language? If so, what language does he have and what is called this technique?

+5
source share
8 answers

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.

+5
source

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.

+4
source

Ada - , :

type My_Int is range 1..100;

, My_Int 1 100, Ada Constraint_Error.

, Ada. , , .

+3

, , . , " " .

. . , Agda2 ATS (ats-lang.org).

, " " .

: - - -

+2

. , - , (, , t , Integer).

++ , , , . (+ =, - =, * = ..), .

, (, ML ), , , ++.

, . , , / , , , , ? , , , .

+1

! : C. C? C? short Integer? , C .

BTW: , , Pascal , . . , , . , , , , .

, , for .

+1

, . , ! , .

:

- , . , .

- , .

, , , . , (, , ).

0

SQL , . , telephone_number , ..

0
source

All Articles