Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy don't Java, C# and C++ have ranges?
    text
    copied!<p><a href="http://en.wikibooks.org/wiki/Ada_Programming/Types/range" rel="nofollow noreferrer">Ada</a>, <a href="http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html" rel="nofollow noreferrer">Pascal</a> 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.</p> <p>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 <a href="http://en.wikipedia.org/wiki/Design_by_contract" rel="nofollow noreferrer">Design by Contract</a> approach (Eiffel) and the Spec# ( <a href="http://research.microsoft.com/en-us/projects/contracts/" rel="nofollow noreferrer">C# Contracts</a> ), that give a more general solution.</p> <p>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 <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Static_assertions" rel="nofollow noreferrer">static-assert</a>?</p> <p><strong>edit</strong>: I understand that "ranges" can be used for different purpose:</p> <ol> <li>iterators</li> <li>enumerators</li> <li>integer subtype</li> </ol> <p>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:</p> <pre><code>volume=rangeInt(0,100); volume=101; // compile error! volume=getIntFromInput(); // possible runtime exception </code></pre> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload