setting a specific range for primitive value for the compiler

M

marcwentink

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
 
T

Tim Slattery

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.
 
E

Eric Sosman

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."
 
M

Mike Schilling

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.)
 
M

marcwentink

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 :)
 
L

Lew

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.
 
M

Mike Schilling

Lew said:
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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

On 7 jun, 22:19, "Mike Schilling"

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top