Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > setting a specific range for primitive value for the compiler

Reply
Thread Tools

setting a specific range for primitive value for the compiler

 
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      06-07-2007
Dear group,

Somehow I thought there was something possible like setting a specific
range in java, but may-be I am totally confused with other
environments I used.

Look at this sort of pseudo code:

int(range 0 - 10) myInt;
myInt = 11; // Compile time error

I cannot find an answer since search on integer and range gives really
a whole lot of hits. And really sorry if this is something from a
totally different programming language, and I am writing 'bs'.

Marc Wentink

 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      06-07-2007
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

>Dear group,
>
>Somehow I thought there was something possible like setting a specific
>range in java, but may-be I am totally confused with other
>environments I used.
>
>Look at this sort of pseudo code:
>
>int(range 0 - 10) myInt;
>myInt = 11; // Compile time error


Not that I'm aware of. But it would be simple to implement a class
that has this behavior:

public class limitedInt
{
int x;
int high;
int low;

public limitedInt(int high, int low)
{
this.high = high;
this.low = low;
}

public void setInt(newval)
{
if (newval >= low && newval <== high)
x = newval;
else
/* throw an exception?? */
}

public int getInt()
{
return x;
}
}

It wouldn't give a compile time error, which is what you talked about,
but it would prevent you from assigning a too-high or too-low value at
run time.

--
Tim Slattery
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-07-2007
(E-Mail Removed) wrote On 06/07/07 11:51,:
> Dear group,
>
> Somehow I thought there was something possible like setting a specific
> range in java, but may-be I am totally confused with other
> environments I used.
>
> Look at this sort of pseudo code:
>
> int(range 0 - 10) myInt;
> myInt = 11; // Compile time error
>
> I cannot find an answer since search on integer and range gives really
> a whole lot of hits. And really sorry if this is something from a
> totally different programming language, and I am writing 'bs'.


You can't do this with Java primitives: Their ranges
are part of the language definition and can't be changed,
and there's no way to create a "derivative" primitive
with special behavior.

You could, though, create a class encapsulating a
range-limited mutable integer, something like

class RestrictedInt {
private final int min, max;
private int value;

RestrictedInt(int min, int max) {
if (min > max)
throw new IllegalArgumentError(...);
this.min = min;
this.max = max;
}

void setValue(int value) {
if (value < min || value > max)
throw new IllegalArgumentError(...);
this.value = value;
}

int getValue() {
return value;
}
}

That won't give you a compile-time error, but will
enforce the range at run time. (Compile-time errors aren't
always possible even in your original scenario; consider
`myInt = object.someMethod();'.)

If the reason for restricting the range is that you're
using the values to label a smallish set of things or
conditions, consider using an enum instead of a number.
That is, instead of saying "There are four types of Taste,
numbered one through four," say "The four Taste types are
Taste.SWEET, Taste.SOUR, Taste.SALT, and Taste.BITTER."

--
(E-Mail Removed)
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      06-07-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Dear group,
>
> Somehow I thought there was something possible like setting a specific
> range in java, but may-be I am totally confused with other
> environments I used.
>
> Look at this sort of pseudo code:
>
> int(range 0 - 10) myInt;
> myInt = 11; // Compile time error
>
> I cannot find an answer since search on integer and range gives really
> a whole lot of hits. And really sorry if this is something from a
> totally different programming language, and I am writing 'bs'.


Perhaps you're thinking of Pascal, which called these "range types".
(http://www.pascal-central.com/ppl/chapter3.html uses "sub-range types", but
I recall otherwise.)


 
Reply With Quote
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      06-08-2007
On 7 jun, 22:19, "Mike Schilling"

> Perhaps you're thinking of Pascal, which called these "range
> types".


Could be.... The last year I've done a little Delphi among other
languages, so I am getting a bit confused sometimes.

I am actually looking at the best way to implement integer with a
limited range like 'age' (0 .. 150) or 'date of birth' (1890 .. 2100).
And the sooner I get the warning/error the better so best would be if
something was possible at compile time. Though it's true the compiler
could not always tell if somethings could go wrong.

Thanks for your attention all

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-08-2007
(E-Mail Removed) wrote:
> I am actually looking at the best way to implement integer with a
> limited range like 'age' (0 .. 150) or 'date of birth' (1890 .. 2100).
> And the sooner I get the warning/error the better so best would be if
> something was possible at compile time. Though it's true the compiler
> could not always tell if somethings could go wrong.


You don't want compile-time ranges for that sort of thing. Use runtime
checks, preferably with bounds configured external to the code, i.e., in a
resource.

Scenario:

You deploy in January, 2008. In February, 2008, Institut de Recherche
announces they've found a way to dramatically reduce replication error in
human cells, effectively quintupling the human lifespan. Rich people are
already buying it illegally, but you can bet within a year it will have wide
availability. With compile-time ranges you have to release a new version of
your software that accepts age< 0, 999 >; with a resource you just change a
value without even re-loading the app.

And don't get me started on what you will have to do to deal with negative age
once time travel is perfected. Was perfected. Will have been previously to
now perfected.

--
Lew
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      06-08-2007
Lew wrote:
> (E-Mail Removed) wrote:
>> I am actually looking at the best way to implement integer with a
>> limited range like 'age' (0 .. 150) or 'date of birth' (1890 ..
>> 2100). And the sooner I get the warning/error the better so best
>> would be if something was possible at compile time. Though it's true
>> the compiler could not always tell if somethings could go wrong.

>
> You don't want compile-time ranges for that sort of thing. Use
> runtime checks, preferably with bounds configured external to the
> code, i.e., in a resource.
>
> Scenario:
>
> You deploy in January, 2008. In February, 2008, Institut de Recherche
> announces they've found a way to dramatically reduce replication
> error in human cells, effectively quintupling the human lifespan. Rich
> people are already buying it illegally, but you can bet within a
> year it will have wide availability. With compile-time ranges you
> have to release a new version of your software that accepts age< 0,
> 999 >; with a resource you just change a value without even
> re-loading the app.
> And don't get me started on what you will have to do to deal with
> negative age once time travel is perfected. Was perfected. Will
> have been previously to now perfected.


With time travel, you'll need at least two dimensions to represent age [1],
and I don't see how to accomplish that without re-implementing the class.

1. Time since birth date and subjective elapsed time.


 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      06-09-2007
(E-Mail Removed) wrote:
> On 7 jun, 22:19, "Mike Schilling"
>> Perhaps you're thinking of Pascal, which called these "range
>> types".

>
> Could be.... The last year I've done a little Delphi among other
> languages, so I am getting a bit confused sometimes.
>
> I am actually looking at the best way to implement integer with a
> limited range like 'age' (0 .. 150) or 'date of birth' (1890 .. 2100).
> And the sooner I get the warning/error the better so best would be if
> something was possible at compile time. Though it's true the compiler
> could not always tell if somethings could go wrong.


This is one of the things that shows Java is of C/C++ heritage
and not Pascal/Ada.

As stated then you will need to write code to check.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
'ArgumentError: bad value for range' for range of Times David Bird Ruby 1 06-23-2008 12:12 PM
primitive range John Ratliff C++ 3 09-29-2005 03:55 AM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM
Overloading SOAP Interpretations for Specific Primitive Types brian.mills ASP .Net Web Services 1 11-18-2004 11:14 PM



Advertisments