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


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

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> <j2gu2q$2tm$1@dont-email.me>
Message-ID <slrnj4q40p.6gl.avl@gamma.logic.tuwien.ac.at> (permalink)
Date 2011-08-18 13:14 +0000

Show all headers | View raw


Joshua Cranmer <Pidgeot18@verizon.invalid> wrote:
> On 8/17/2011 10:04 AM, Andreas Leitgeb wrote:
>> I believe such a change would be source-compatible, but might require
>> some severe changes at the bytecode/class-file-format level, to allow
>> to specify such a lower bound in a type signature. Such a change should
>> be possible in a way, that old class-files can still be used. e.g.:
>>   get9th:(]10I)I    (currently without bounds:  get9th:([I)I )
> You could do it by adding a few new annotations,

I must admit, that I've so far mostly ignored Annotations for my
tasks. When you write "adding a few annotations", do you just mean
adding certain standard annotations to the respective places, or did
you mean defining a custom Annotation and write some code for a
compiler-plugin that would then deal with one's new Annotations?

Either way, I doubt, that it would go as far as the feature I
posted (especially wrt overloading).

> one to annotate 
> min-length values for method parameters/return value and one for the 
> fields. Note that this means that the type information is lost at 
> runtime, so an int[123] is sugared at runtime as int[] and could not be 
> exposed as int[123] (you would also have to add more information into 
> the LocalVariableTable).

I wouldn't mind this loss. At runtime one can still check the .length
of the array. (Btw., I'm also quite content with generics' erasure ;-)

> The issue you get here is that often times the size of the array bound 
> isn't useful in terms of a static number constant, but rather in terms 
> of previous constants, e.g. if you want to get the i'th pixel of an rgb 
> pixel byte array, you need to guarantee that the length is at least 3 * i.

It's obvious, that if some array's length doesn't have any *static*
boundaries, then a static check won't do anything useful.
If arrays are used to implement the high-level concept of output
parameters, then a method might want to do this:
  void foo( int in, int[1] out ) {
     out[0]=in; /* example doesn't need to do sthg useful */
  }
to ensure that it won't be called with a zero-length array.

> On the other hand, I'm not sure the utility of this is particularly 
> great. You wouldn't be able to avoid any more runtime checks beyond what 
> the JIT could most likely already optimize out.

The primary goal was of course compile-time checks. If any runtime-check
could be saved at all, that would be merely a nice side-effect.

This is some "solution in search of a *practical* problem to solve".
Maybe, requiring a minimum length for an array is already an anti-pattern.
Maybe someone else happens to know of a plausible usecase.
But I'd be mostly interested if anyone could find any plain
inconsistency in it.

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