Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > const references -- how to document intent of const qualification

Reply
Thread Tools

const references -- how to document intent of const qualification

 
 
Andrew R. Thomas-Cramer
Guest
Posts: n/a
 
      07-09-2003

Java fails to provide a const-qualifier for references, for compile-time
const checking.

In the past, I've tended to document intended const-qualified references
(e.g., method parameters, return values, fields, local variables) with C++
syntax: "const*". Does anyone have a better suggestion, that doesn't
require knowledge of C++?


andy thomas-cramer
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      07-09-2003
Andrew R. Thomas-Cramer wrote:
> Java fails to provide a const-qualifier for references, for compile-time
> const checking.
>
> In the past, I've tended to document intended const-qualified references
> (e.g., method parameters, return values, fields, local variables) with C++
> syntax: "const*". Does anyone have a better suggestion, that doesn't
> require knowledge of C++?


Method parameters, fields, and local variables may be declared final,
which seems to be what you are looking for if I understand you
correctly. I don't understand how the question is relevant to return
values, but perhaps my C++ is too rusty.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-09-2003
On Wed, 09 Jul 2003 17:16:12 -0000, Andrew R. Thomas-Cramer
<(E-Mail Removed)> wrote or quoted :

>In the past, I've tended to document intended const-qualified references
>(e.g., method parameters, return values, fields, local variables) with C++
>syntax: "const*". Does anyone have a better suggestion, that doesn't
>require knowledge of C++?


java has final which is similar. See
http://mindprod.com/jgloss/final.html.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Timo Kinnunen
Guest
Posts: n/a
 
      07-09-2003
Andrew R. Thomas-Cramer <(E-Mail Removed)> wrote:

> Java fails to provide a const-qualifier for references, for
> compile-time const checking.
>
> In the past, I've tended to document intended const-qualified
> references (e.g., method parameters, return values, fields, local
> variables) with C++ syntax: "const*". Does anyone have a better
> suggestion, that doesn't require knowledge of C++?


I believe the convention is to place a big warning text next to
everything where immutability isn't enforced. For example, in
javax.swing.event.EventListenerList:

<quote>
public Object[] getListenerList()

Passes back the event listener list as an array of ListenerType-listener
pairs. Note that for performance reasons, this implementation passes
back the actual data structure in which the listener data is stored
internally! This method is guaranteed to pass back a non-null array, so
that no null-checking is required in fire methods. A zero-length array
of Object should be returned if there are currently no listeners.
WARNING!!! Absolutely NO modification of the data contained in this
array should be made -- if any such manipulation is necessary, it should
be done on a copy of the array returned rather than the array itself.
</quote>

Personally, I would write an even stronger warning, something like:

WARNING!!! The returned array is not immutable, but MUST be treated as
such.

--
No address munging in use. I like the smell of nuked accounts in the
morning.
 
Reply With Quote
 
Jesper Nordenberg
Guest
Posts: n/a
 
      07-10-2003
Andrew R. Thomas-Cramer <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Java fails to provide a const-qualifier for references, for compile-time
> const checking.
>
> In the past, I've tended to document intended const-qualified references
> (e.g., method parameters, return values, fields, local variables) with C++
> syntax: "const*". Does anyone have a better suggestion, that doesn't
> require knowledge of C++?


Create a const interface that your class implements and return this
rather than a class reference. I've built my own type safe collections
library with const interfaces. It works great. Say no to array copying


/Jesper Nordenberg
 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      07-10-2003
Roedy Green <(E-Mail Removed)> writes:

> java has final which is similar. See
> http://mindprod.com/jgloss/final.html.


Yes, but it only gives const pointers, not const objects.
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      07-10-2003
Andrew R. Thomas-Cramer <(E-Mail Removed)> wrote:

: Java fails to provide a const-qualifier for references, for compile-time
: const checking.

: In the past, I've tended to document intended const-qualified references
: (e.g., method parameters, return values, fields, local variables) with C++
: syntax: "const*". Does anyone have a better suggestion, that doesn't
: require knowledge of C++?

Look at:

http://david.tribble.com/text/javaconst.html

....and...

Need a 'const' qualifier
Closed, will not be fixed
http://developer.java.sun.com/develo...s/4069541.html

const - qualifier for methods, parameters and return-value
Closed, will not be fixed
http://developer.java.sun.com/develo...s/4093718.html

Java should support const parameters (like C++) for code maintainence
In progress, request for enhancement
http://developer.java.sun.com/develo...s/4211070.html

....note that some people believe this:

RFE: Add Immutable types to Java
In progress, request for enhancement
http://developer.java.sun.com/develo...s/4617197.html

....is a more important umbrella proposal...

....and then vote accordingly.
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed)
 
Reply With Quote
 
Doug Pardee
Guest
Posts: n/a
 
      07-10-2003
Andrew R. Thomas-Cramer <(E-Mail Removed)> wrote:
> Java fails to provide a const-qualifier for references, for compile-time
> const checking.


That's because it doesn't need it. "const" is a C++ kludge, needed
only because C++ can't distinguish between value types and reference
types.

In Java, we distinguish between value types (primitives and
immutables) and reference types (mutable classes).

> In the past, I've tended to document intended const-qualified references
> (e.g., method parameters, return values, fields, local variables) with C++
> syntax: "const*". Does anyone have a better suggestion, that doesn't
> require knowledge of C++?


Use value types: primitives and immutables. For formal parameters,
fields, and local variables, also specify 'final'.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-11-2003
On Wed, 09 Jul 2003 17:16:12 -0000, Andrew R. Thomas-Cramer
<(E-Mail Removed)> wrote or quoted :

>Does anyone have a better suggestion, that doesn't
>require knowledge of C++?


I have four suggestions at http://mindprod.com/jgloss/immutable.html.

None give you the fine control of C++, but then in C++ I found most
C++ programmers did not really understand the subtleties and then
tended to either not use, or remove const.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Does XP support Epson 4000 Absolute Rendering Intent? Anthony Digital Photography 0 03-12-2006 02:21 PM
Re: Law & order criminal intent Annette Kurten Computer Support 5 05-09-2005 12:05 AM
Law & order criminal intent Annette Kurten Computer Support 0 05-08-2005 11:06 PM
Re: Letter of Intent to Apply? DC Computer Support 0 07-13-2004 02:57 PM



Advertisments