Welcome to Web-News
A Web-based News Reader
Subject in/inout/out for arrays needs clear defining
From Stewart Gordon <smjg_1998@yahoo.com>
Date Tue, 06 Jul 2004 11:02:45 +0100
Newsgroups digitalmars.D,digitalmars.D.bugs

Using DMD 0.94, Windows 98SE.

I've long noticed a potential cause for confusion.  The passing of
arrays as (address, length) tuples, while sensible on the whole, leads
to counter-intuitive behaviour.  While it's logical to this form, it
isn't what one might expect, and the spec doesn't clearly define the
semantics of in/inout/out on arrays.

In short: if an array is passed as in, someone might think that the
contents of the array are in, when in reality only the (address, length)
tuple is in; the actual array contents are inout.

Of course, it's probably too late to clean up the semantics without
breaking plenty of existing code.  But I suppose the main point is that
the semantics we have need to be clearly explained.


Still, a possible idea for the future would be to allow the contents and
dimensions to have separate in/inout/out settings.  The idea I came up
with a while back is a notation like

        void qwert(inout int[in] yuiop);

meaning that the length is in, and the contents are inout.  There would
be six possibilities:


in int[in]
        "Give me an array to look at"
        Pass in (address, length); either disallow changes to data in the
function body, or copy on entry if any changes can occur

inout int[in]
        "Give me the size and some starting data, and I'll play around with the
data"
        Pass in (address, length); allow the contents to be modified in-place
(current treatment of in int[], except that changing the length should
be disallowed)

out int[in]
        "Give me the size, and I'll give you the data"
        Pass in (address, length); initialise the array elements on entry;
allow the contents to be modified in-place

inout int[inout]
        "Give me an array, and I'll do what I like with it"
        Pass in reference to (address, length); anything goes (current
treatment of inout int[])

out int[inout]
        "Give me a starting size, and I'll give you the data, changing the size
if I want"
        Pass in reference to (address, length); initialise the array elements
on entry; anything goes

out int[out]
        "I'll give you an array"
        Pass in reference to (address, length), which is initialised to null


Of course, the other combinations in[inout], in[out], inout[out] make
little or no sense.

I guess the extension to arrays of arrays would be straightforward, but
I'm not sure.

And combined with the existing system, while the syntax of my idea
doesn't clash with it, it would cause confusion.  And it would probalby
clash if we tried to extend it to static array parameters.  Maybe
there's a better notation....

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment.  Please keep
replies on the 'group where everyone may benefit.

Recent messages in this thread
 
-# in/inout/out for arrays needs clear defining (Current message) Stewart Gordon 06-Jul-2004 06:02 am
.-# Re: in/inout/out for arrays needs clear defining Norbert Nemec 06-Jul-2004 06:14 am
..-# Re: in/inout/out for arrays needs clear defining Walter 07-Jul-2004 01:24 pm
...|# Re: in/inout/out for arrays needs clear defining Norbert Nemec 07-Jul-2004 03:27 pm
...\# Re: in/inout/out for arrays needs clear defining Regan Heath 07-Jul-2004 06:03 pm