Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7220

Re: enhance an array's static type by a lower length-bound.

Newsgroups comp.lang.java.programmer
From Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at>
Subject Re: enhance an array's static type by a lower length-bound.
References <slrnj4nm33.6gl.avl@gamma.logic.tuwien.ac.at> <6k7p47d9442mcld6j2p5l4hekodard6oss@4ax.com> <4c11ff9c-3c83-45cc-85a7-9b0c05de17ff@glegroupsg2000goo.googlegroups.com> <slrnj4pr3b.6gl.avl@gamma.logic.tuwien.ac.at> <W4-dnThc3ORFidDTnZ2dnUVZ_oydnZ2d@earthlink.com>
Message-ID <slrnj4ql9f.6gl.avl@gamma.logic.tuwien.ac.at> (permalink)
Date 2011-08-18 18:08 +0000

Show all headers | View raw


Patricia Shanahan <pats@acm.org> wrote:
> I don't think I've ever seen a language with dynamic array creation that
> has a lower bound on the length of an array a variable can reference.
> I'm not sure how it would work, or how I would use it.

The particular approach may appear new, but it isn't much different
from the well-known object-hierarchy. Except: the "sub-tree" starting
from an unbounded array type[] is just the linear chain of all bounded
arrays type[#], sorted by their bound value.

> Usually, if I have a non-zero lower length bound it is 1 or 2, but most
> of my array handling code is designed and tested for zero length and up.

As we're all used (and even "bound") to the current state (while doing Java,
at least), it's no surprise at all, that you do such checks at runtime.

What do you do, if the array is smaller than required?
throw an exception?  That should/could be compiler's job.
You perform some other sensible task? Then bounded arrays
won't help, nor affect you.

> Even when there is a lower bound, I'm not sure knowing it would help
> much with compile time error detection. For example, a method to
> calculate the sample mean of some observations requires at least one
> element, but is going to iterate over the whole array.

Maybe I forgot to explain, that this lower-bounding doesn't have an
impact on applicable indices:

  int[10] ia = getArray(); // defined as: int[12] getArray() { ... }
  ia[9]++; //  is guaranteed *not* to throw an AIOOBE!
           //  If the array were too small, then getArray()
           //    couldn't have returned it.
  ia[15]++; // still perfectly legal. But it *may or may not* throw
            //   an AIOOBE at runtime, depending on the actual array.

There is no general guarantee, that some ia[i] will not throw an AIOOBE,
unless there's also a guarantee on i's range.    JIT might make use of
such extra guarantees, that do not depend on the actual array.

double mean( double[1] values ) {
    // just go ahead and sum up, then divide by values.length
    //   as it will be non-zero.  Statically ruling out "null"
    //   is another day's work - or maybe can already be done
    //   using Java's Annotations (?)
    ...
}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-17 15:04 +0000
  Re: enhance an array's static type by a lower length-bound. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-08-17 12:34 -0500
    Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 13:14 +0000
      Re: enhance an array's static type by a lower length-bound. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-08-18 10:30 -0500
        Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 18:12 +0000
          Re: enhance an array's static type by a lower length-bound. Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-08-18 20:59 -0500
  Re: enhance an array's static type by a lower length-bound. Robert Klemme <shortcutter@googlemail.com> - 2011-08-17 20:42 +0200
    Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 21:44 +0000
      Re: enhance an array's static type by a lower length-bound. Robert Klemme <shortcutter@googlemail.com> - 2011-08-19 13:24 +0200
        Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 17:20 +0000
  Re: enhance an array's static type by a lower length-bound. Roedy Green <see_website@mindprod.com.invalid> - 2011-08-17 22:11 -0700
    Re: enhance an array's static type by a lower length-bound. Lew <lewbloch@gmail.com> - 2011-08-17 23:17 -0700
      Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 10:41 +0000
        Re: enhance an array's static type by a lower length-bound. Patricia Shanahan <pats@acm.org> - 2011-08-18 06:34 -0700
          Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-18 18:08 +0000
            Re: enhance an array's static type by a lower length-bound. Roedy Green <see_website@mindprod.com.invalid> - 2011-08-18 17:53 -0700
              Re: enhance an array's static type by a lower length-bound. Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-20 17:31 +0000
                Re: enhance an array's static type by a lower length-bound. Lew <lewbloch@gmail.com> - 2011-08-20 10:50 -0700
                Re: enhance an array's static type by a lower length-bound. Robert Klemme <shortcutter@googlemail.com> - 2011-08-21 10:47 +0200
          Re: enhance an array's static type by a lower length-bound. Michal Kleczek <kleku@poczta.onet.pl> - 2011-08-21 20:27 +0200

csiph-web