setting a specific range for primitive value for the compiler

Discussion in 'Java' started by marcwentink@hotmail.com, Jun 7, 2007.

  1. Guest

    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
    , Jun 7, 2007
    #1
    1. Advertising

  2. Tim Slattery Guest

    "" <> 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://members.cox.net/slatteryt
    Tim Slattery, Jun 7, 2007
    #2
    1. Advertising

  3. Eric Sosman Guest

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

    --
    Eric Sosman, Jun 7, 2007
    #3
  4. <> wrote in message
    news:...
    > 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.)
    Mike Schilling, Jun 7, 2007
    #4
  5. Guest

    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 :)
    , Jun 8, 2007
    #5
  6. Lew Guest

    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
    Lew, Jun 8, 2007
    #6
  7. Lew wrote:
    > 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.
    Mike Schilling, Jun 8, 2007
    #7
  8. 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
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jun 9, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    592
    Victor Bazarov
    May 9, 2005
  2. John Ratliff

    primitive range

    John Ratliff, Sep 29, 2005, in forum: C++
    Replies:
    3
    Views:
    299
    John Ratliff
    Sep 29, 2005
  3. Daniel Pitts
    Replies:
    7
    Views:
    463
  4. brian.mills

    Overloading SOAP Interpretations for Specific Primitive Types

    brian.mills, Nov 17, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    172
    Dan Rogers
    Nov 18, 2004
  5. David Bird
    Replies:
    1
    Views:
    199
    Tiago Macedo
    Jun 23, 2008
Loading...

Share This Page