octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Round/curly bracket interchangeability for cell


From: Daniel J Sebald
Subject: Re: Round/curly bracket interchangeability for cell
Date: Mon, 9 Jan 2017 16:50:09 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1

On 01/09/2017 04:21 PM, Mike Miller wrote:
On Mon, Jan 09, 2017 at 16:03:30 -0600, Daniel J Sebald wrote:
I just noticed that once a cell variable exists, round brackets may be
substituted for curly brackets.  I first noticed this when I encountered a
simple error like the following:

octave:49> a{1} = 10;
octave:50> a{2}
error: a(2): out of bound 1

Notice that the error message has a round bracket rather than curly bracket,
in light of the variable 'a' being a cell.  My thinking was that the
following makes more sense for an error:

error: a{2}: out of bound 1

After further examination, I found the following is acceptable:

octave:54> b{1} = "curly";
octave:55> b(2) = "round"
b =


{
  [1,1] = curly
  [1,2] = round
}

(Well, first thing, notice two spaces between "b =" and the variable
contents display, is that a bug?)

I don't know if this curly/round bracket is a compatibility issue, but I
would think this is too free use of round brackets and can cause more
confusion than anything.  I mean, it is difficult enough for someone
learning the language to comprehend the difference between a cell and an
array.  Then introduce the idea that if I first use curly bracket, from then
on I can write lines of code that use the round bracket to index that cell.
Being a new user, one might type by mistake "()" rather than "{}" and be
completely confused.  Even an experienced user wouldn't like reading code
that did such a thing.

Yes, as I recollect this is a compatibility issue.

To further clarify, indexing on cell arrays with round brackets /
parentheses *is* different from indexing with curly brackets / braces,
both are intentionally supported and intentionally mean different
things, as in Matlab:

  >> a = {10, 11, 12};
  >> a{1}
  ans =  10
  >> a(1)
  ans =
  {
    [1,1] = 10
  }

When used as the LHS of an indexed assignment, they are also different:

  >> a(1:3) = 0
  a =
  {
    [1,1] = 0
    [1,2] = 0
    [1,3] = 0
  }
  >> a{1:3} = 0
  error: invalid assignment to cs-list outside multiple assignment

  >> [a{1:3}] = deal (3, 2, 1)
  a =
  {
    [1,1] =  3
    [1,2] =  2
    [1,3] =  1
  }

Only in the degenerate case of indexing a single element of the cell
array are they equivalent.

Does that clear things up?

Yes, a bit, thanks. It still might be nice to have the error message reflect the bracket usage for the line number that fails, e.g.,

octave:67> [b{1:5}] = deal(a{1:5})
error: a(5): out of bound 4

As pointed out, round bracket and curly bracket behavior is different.

octave:77> clear
octave:78> [a{1:2}] = deal(2)
a =


{
  [1,1] =  2
  [1,2] =  2
}

octave:79> [b{1:2}] = deal(a{1:2})
b =


{
  [1,1] =  2
  [1,2] =  2
}

octave:80> [b{1:2}] = deal((1:2))
b =


{
  [1,1] =

     1   2

  [1,2] =

     1   2

}

Dan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]