const references -- how to document intent of const qualification

  • Thread starter Andrew R. Thomas-Cramer
  • Start date
A

Andrew R. Thomas-Cramer

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
 
J

John C. Bollinger

Andrew said:
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
(e-mail address removed)
 
T

Timo Kinnunen

Andrew R. Thomas-Cramer said:
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.
 
J

Jesper Nordenberg

Andrew R. Thomas-Cramer said:
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
 
T

Tim Tyler

: 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/developer/bugParade/bugs/4069541.html

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

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

....note that some people believe this:

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

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

....and then vote accordingly.
 
D

Doug Pardee

Andrew R. Thomas-Cramer said:
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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top