octave-maintainers
[Top][All Lists]
Advanced

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

Re: Deprecate nfields function?


From: Daniel J Sebald
Subject: Re: Deprecate nfields function?
Date: Sat, 01 Mar 2014 14:33:02 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

On 03/01/2014 02:03 PM, Rik wrote:
On 03/01/2014 11:07 AM, John W. Eaton wrote:
On 03/01/2014 01:15 PM, Daniel J Sebald wrote:
On 03/01/2014 12:06 PM, Lukas Reichlin wrote:
On 01.03.2014, at 17:21, Rik<address@hidden>   wrote:

Would anyone be heartbroken if we deprecated the nfields function in
version 4.2?

Yes, I'm using it in the control package and I think it's a handy
function.

I'm indifferent about the shortcut being in the core (it's so short,
doesn't seem to hurt), but why nfields() as opposed to numfield() to be
more consistent with numel()?

I suspect nfields is older than numel (a name that was invented by TMW
because length was already taken and defined in a strange way that is not
equivalent to numel).  And I just chose nfields because it seemed like a
good idea the time.  It's easy to look back and say that a past decision
wasn't perfect.  It's much harder to know whether a decision you make now
will still seem like a good idea 20 years later...

There was definitely no criticism on my part.  These things do evolve and
it is hard to pick exactly the correct strategy both for today and for 20
years from today.  Do we want to rename nfields to something else
(numfields, numfld, numflds) or remove it entirely?

If any, I like the second, numfld(). It's short to match the shortness of numel().

The justification for such a function is that a structure is a fundamental class and where it is straightforward to get the number of elements of classes with numel(), it seems slightly clumsy to do 'numel (fieldnames (x))' to get the number of fields.

I wanted to be careful that such a function makes sense in various situations, and in trying a few things here--unless my copy of Octave is too far out of date--I've found something buggish when removing a structure field. It may not be a bug but just reporting a successful operation as opposed to an error message. Try the following:

octave-cli:26> foo.f1 = 'abc';
octave-cli:27> foo.f2 = 1;
octave-cli:28> foo(2).f1 = 'xyz';
octave-cli:29> foo(2).f2 = '2';
octave-cli:30> foo(2)
ans =

  scalar structure containing the fields:

    f1 = xyz
    f2 = 2

octave-cli:31> rmfield(foo(2), 'f2')
ans =

  scalar structure containing the fields:

    f1 = xyz

octave-cli:32> foo(2)
ans =

  scalar structure containing the fields:

    f1 = xyz
    f2 = 2

octave-cli:33>

The "bug" is that removing the field in the second last command suggests that field 'f2' has been removed from the second element of the structure array when in fact that might not be a valid thing to do.

Dan


reply via email to

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