Why don't Java, C# and C++ have ranges?

2020-04-02 08:26发布

Ada, Pascal and many other languages support ranges, a way to subtype integers. A range is a signed integer value which ranges from a value (first) to another (last). It's easy to implement a class that does the same in OOP but I think that supporting the feature natively could let the compiler to do additional static checks.

I know that it's impossible to verify statically that a variabile defined in a range is not going to "overflow" runtime, i.e. due to bad input, but I think that something could be done. I think about the Design by Contract approach (Eiffel) and the Spec# ( C# Contracts ), that give a more general solution.

Is there a simpler solution that checks, at least, static out-of-bound assignment at compile time in C++, C# and Java? Some kind of static-assert?

edit: I understand that "ranges" can be used for different purpose:

  1. iterators
  2. enumerators
  3. integer subtype

I would focus on the latter, because the formers are easily mappable on C* language . I think about a closed set of values, something like the music volume, i.e. a range that goes from 1 up to 100. I would like to increment or decrement it by a value. I would like to have a compile error in case of static overflow, something like:

volume=rangeInt(0,100);
volume=101;  // compile error!
volume=getIntFromInput(); // possible runtime exception

Thanks.

11条回答
劳资没心,怎么记你
2楼-- · 2020-04-02 08:30

The flexibility to roll your own is better than having it built into the language. What if you want saturating arithmetic for example, instead of throwing an exception for out of range values? I.e.

MyRange<0,100> volume = 99;
volume += 10; // results in volume==100
查看更多
Bombasti
3楼-- · 2020-04-02 08:32

In C# you can do this:

foreach(int i in System.Linq.Enumerable.Range(0, 10))
{
    // Do something
}
查看更多
乱世女痞
4楼-- · 2020-04-02 08:33

JSR-305 provides some support for ranges but I don't know when if ever this will be part of Java.

查看更多
forever°为你锁心
5楼-- · 2020-04-02 08:36

This is an old question, but just wanted to update it. Java doesn't have ranges per-se, but if you really want the function you can use Commons Lang which has a number of range classes including IntRange:

IntRange ir = new IntRange(1, 10);

Bizarrely, this doesn't exist in Commons Math. I kind of agree with the accepted answer in part, but I don't believe ranges are useless, particularly in test cases.

查看更多
Melony?
6楼-- · 2020-04-02 08:38

C++ allows you to implement such types through templates, and I think there are a few libraries available doing this already. However, I think in most cases, the benefit is too small to justify the added complexity and compilation speed penalty.

As for static assert, it already exists. Boost has a BOOST_STATIC_ASSERT, and on Windows, I think Microsoft's ATL library defines a similar one.

boost::type_traits and boost::mpl are probably your best friends in implementing something like this.

查看更多
女痞
7楼-- · 2020-04-02 08:46

Java has had an assert keyword since version 1.4. If you're doing programming by contract, you're free to use those to check proper assignment. And any mutable attribute inside an object that should fall within a certain range should be checked prior to being set. You can also throw an IllegalArgumentException.

Why no range type? My guess is that the original designers didn't see one in C++ and didn't consider it as important as the other features they were trying to get right.

查看更多
登录 后发表回答