Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

setting a specific range for primitive value for the compiler

Thread view: 
marcwentink@hotmail.com - 07 Jun 2007 16:51 GMT
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
Tim Slattery - 07 Jun 2007 17:21 GMT
>Dear group,
>
[quoted text clipped - 6 lines]
>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.

Signature

Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt

Eric Sosman - 07 Jun 2007 17:36 GMT
marcwentink@hotmail.com wrote On 06/07/07 11:51,:
> Dear group,
>
[quoted text clipped - 10 lines]
> 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."

Signature

Eric.Sosman@sun.com

Mike  Schilling - 07 Jun 2007 21:19 GMT
> Dear group,
>
[quoted text clipped - 10 lines]
> 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.)
marcwentink@hotmail.com - 08 Jun 2007 12:43 GMT
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 :)
Lew - 08 Jun 2007 15:03 GMT
> 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.

Signature

Lew

Mike Schilling - 08 Jun 2007 15:16 GMT
>> I am actually looking at the best way to implement integer with a
>> limited range like 'age' (0 .. 150) or 'date of birth' (1890 ..
[quoted text clipped - 19 lines]
> 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.
Arne Vajhøj - 09 Jun 2007 19:37 GMT
> On 7 jun, 22:19, "Mike  Schilling"
>> Perhaps you're thinking of Pascal, which called these "range
[quoted text clipped - 8 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.