Article View: comp.lang.java
Article #68Re: Constness in languages
From: Brian Strelioff
Date: Wed, 31 Jan 1996 00:50
Date: Wed, 31 Jan 1996 00:50
74 lines
3747 bytes
3747 bytes
Another way to view "const" is as a language rule. For example,
"2 := 3" is usually not allowed, and not many folks argue
that it should be allowed: clearly there is a well understood
"const" quality to literals.
In C/C++, const value arguments are useful as a safety check,
particularly given the increased use of macros which may hide
attempts to change the value of an argument. const references
& pointers (actually pointers to const) are also extremely
useful from an API specification standpoint as they clearly
point out (and enforce assuming no casting is used) which
arguments can/will be changed by a routine.
In Eiffel, values of arguments are already "const", but there is still
the question of how to make references "const". Pre/Post-conditions
help to some degree, but not only does this defer detection until
run-time it also makes enforcement "optional" since checking
of pre/post conditions is itself optional. Also, lets look at
a relatively simple example of ensuring that a STRING is not
modified:
class xxx
...
no_touch(s: STRING) is
require
s_exists: s /= Void
do
...
ensure
???
end
end
How do we encode the (dynamically checked) postcondition?
1) s = old s -- won't work since this just checks that the object
referenced by s is the same object, not that the object didn't change
(Since the value of s cannot change, this check in fact is worthless)
2) s.is_equal(old s) -- won't work either (since s = old s, the
field-by-field comparison of s and itself is essentially useless)
3) declare a local string, copy s into at, and check at the end
of the routine? Can't do it since Eiffel (the language) doesn't
allow use of locals in postconditions (NOTE: some vendors do allow
this as an vendor dependant extension)
4) add a field to xxx where s is copied into and check at the end
of no_touch? Doable, but ugly and slow.
OTOH, in C/C++ this specification is easy and statically checkable:
no_touch(String const * const s) {
...
}
[Note const routines (i.e. routines that do not change the object
used to invoke them) are an automatic necessity once const arguments
enter the language]
Granted the initial introduction of "const" into an existing class
library is painful, but I think definitely worth it. For classes
being written from scratch I find there to be no incremental cost
in appropriate use of "const" arguments.
As to const function results, I think it necessary to allow for them.
For example, given CONTAINER[Const STRING], any function returning
an item from that container should return it as a "Const STRING".
Yes it may be possible to emulate const in Eiffel via duplicate
class definitions (i.e. writable, read-only), but not completely
and not efficiently (both in development and execution).
What I would like to see in both Eiffel & Java is "const constness"
in which the semantics of C/C++ const are available without the
ability to override (via casting in C/C++, inheritance in Eiffel).
Technical Details
Message-ID:
<310EBCDE.32DE@interramp.com>Path:
arc1-ca.pugleaf.net!interramp.com!usenetReferences:
<30FBED5F.28B8@achilles.net> <4dj4t4$a4@kai.com> <4e6dtm$si5@gaia.ns.utk.edu> <9602901.11916@mulga.cs.mu.OZ.AU> <4ej1eo$s7@stc06.ctd.ornl.gov>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!interramp.com!usenet From: Brian Strelioff <pp001729@interramp.com> Newsgroups: comp.lang.java,comp.lang.eiffel,comp.lang.misc,comp.lang.c++ Subject: Re: Constness in languages Date: Tue, 30 Jan 1996 16:50:38 -0800 Organization: BKS Systems, Inc. Message-ID: <310EBCDE.32DE@interramp.com> References: <30FBED5F.28B8@achilles.net> <4dj4t4$a4@kai.com> <4e6dtm$si5@gaia.ns.utk.edu> <9602901.11916@mulga.cs.mu.OZ.AU> <4ej1eo$s7@stc06.ctd.ornl.gov> NNTP-Posting-Host: ip137.los-angeles6.ca.interramp.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 2.0b6a (X11; I; SunOS 5.5 sun4m)