开发者

Does any programming language support defining constraints on primitive data types?

Last night I was thining that programming languages can have a feature in which we should be able to constraints the values assigned to primitive data types.

For example I should be able to say开发者_如何学C my variable of type int can only have value between 0 and 100

int<0, 100> progress;

This would then act as a normal integer in all scenarios except the fact that you won't be able to specify values out of the range defined in constraint. The compiler will not compile the code progress=200. This constraint can be carried over with type information.

Is this possible? Is it done in any programming language? If yes then which language has it and what is this technique called?


It is generally not possible. It makes little sense to use integers without any arithmetic operators. With arithmetic operators you have this:

int<0,100> x, u, v;
...
x = u + v; // is it in range?

If you're willing to do checks at run-time, then yes, several mainstream languages support it, starting with Pascal.


I believe Pascal (and Delphi) offers something similar with subrange types.

I think this is not possible at all in Java and in Ruby (well, in Ruby probably it is possible, but requires some effort). I have no idea about other languages, though.


Ada allows something like what you describe with ranges:

type My_Int is range 1..100;

So if you try assign a value to a My_Int that's less than 1 or greater than 100, Ada will raise the exception Constraint_Error.

Note that I've never used Ada. I've only read about this feature, so do your research before you plunge in.


It is certainly possible. There are many different techniques to do that, but 'dependent types' is the most popular.

The constraints can be even checked statically at compile time by compiler. See, for example, Agda2 and ATS (ats-lang.org).

Weaker forms of your 'range types' are possible without full dependent types, I think.

Some keywords to search for research papers: - Guarded types - Refinment types - Subrange types


Certainly! In case you missed it: C. Do you C? You don't C? You don't count short as a constraint on Integer? Ok, so C only gives you pre-packaged constrained types.

BTW: It seems the answer that Pascal has subrange types misses the point of them. In Pascal array bounds violations are not possible. This is because the array index must of the same type as the array was declared with. In turn this means that to use an integer index you must coerce it down to the subrange, and that is where the run time check is done, not accessing the array.

This is a very important idea because it means a for loop over an array index type may access the array components safely without any run time checking.


Pascal has subranges. Ada extended that a bit, so you can do something like a subrange, or you can create an entirely new type with characteristics of the existing type, but not compatible with it (e.g., even if it was in the right range, you wouldn't be able to assign an Integer to your new type based off of Integer).

C++ doesn't support the idea directly, but is flexible enough that you can implement it if you want to. If you decide to support all the compound assignment operators (+=, -=, *=, etc.) this can be a lot of work though.

Other languages that support operator overloading (e.g., ML and company) can probably support it in much the same way as C++.

Also note that there are a few non-trivial decisions involved in the design. In particular, if the type is used in a way that could/does result in an intermediate result that overflows the specified range, but produces a final result that's within the specified range, what do you want to happen? Depending on your situation, that might be an error, or it might be entirely acceptable, and you'll have to decide which.


I really doubt that you can do that. Afterall these are primitive datatypes, with emphasis on primitive! adding a constraint will make the type a subclass of its primitive state, thus extending it.

from wikipedia:

a basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types.

a built-in type is a data type for which the programming language provides built-in support.

So personally, even if it is possible i wouldnt do, since its a bad practice. Instead just create an object that returns this type and the constraints (which i am sure you thought of this solution).


SQL has domains, which consist of a base type together with a dynamically-checked constraint. For example, you might define the domain telephone_number as a character string with an appropriate number of digits, valid area code, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜