[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #57255] += operator behaves differently to man
From: |
Tasos Papastylianou |
Subject: |
[Octave-bug-tracker] [bug #57255] += operator behaves differently to manual increment and ++ operator |
Date: |
Mon, 18 Nov 2019 05:54:54 -0500 (EST) |
User-agent: |
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0 |
URL:
<https://savannah.gnu.org/bugs/?57255>
Summary: += operator behaves differently to manual increment
and ++ operator
Project: GNU Octave
Submitted by: tpapastylianou
Submitted on: Mon 18 Nov 2019 10:54:53 AM UTC
Category: Interpreter
Severity: 3 - Normal
Priority: 5 - Normal
Item Group: Incorrect Result
Status: None
Assigned to: None
Originator Name: tpapastylianou
Originator Email:
Open/Closed: Open
Discussion Lock: Any
Release: 5.1.0
Operating System: Any
_______________________________________________________
Details:
When evaluating statements with side effects in the context of a cell array,
the += operator seems to treat scope differently to the ++ operator and manual
incrementation.
E.g. consider the following function:
%%% In file makecounter.m
function counter = makecounter ( StartIndex )
if nargin == 0; StartIndex = 0; endif
Index = StartIndex;
counter = @() {
% Index = Index + 1; % Version 1
% ++Index % Version 2
% Index += 1 % Version 3
}{end};
endfunction
%%% End of file makecounter.m
and suppose you call it at the terminal like so:
counter = makecounter (10);
C = cell (1, 5); for i = 1:5; C{i} = counter(); endfor; C
The three versions output as follows:
C = { 11, 12, 13, 14, 15 } # Version 1 - correct
C = { 11, 12, 13, 14, 15 } # Version 2 - correct
C = { 1, 1, 1, 1, 1 } # Version 3 - incorrect!
This bug may relate to the underlying cause for bugs #52363 and #53871, but
here the focus is on anonymous functions / cell scope, so it may be more
involved.
Context: I was trying mechanisms to produce closures, which work around
octave's current lack of support for handles to nested functions (which is how
one would typically produce a closure in matlab). Thankfully, octave supports
a) cell arrays whose cells can contain statements with side-effects, and b)
chained operations. This allows one to return a valid closure from a 'factory'
function, by returning an anonymous function, which: creates a cell, evaluates
statements with side effects as consecutive cells in a cell array, and indexes
the final cell on the spot as a chained operation, thereby effectively
treating it as the return value of the anonymous function.
At first I thought the bug was a side-effect of the fact that the 'Index'
variable did not appear on the right hand side explicitly, and therefore did
not get to be inherited from its parent scope. The more I think about this now
though, even if this is the case, there is no excuse for += returning a
different result from the other two versions; therefore I would classify this
as a bug.
_______________________________________________________
Reply to this item at:
<https://savannah.gnu.org/bugs/?57255>
_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
- [Octave-bug-tracker] [bug #57255] += operator behaves differently to manual increment and ++ operator,
Tasos Papastylianou <=
- [Octave-bug-tracker] [bug #57255] += operator behaves differently to manual increment and ++ operator, Mike Miller, 2019/11/18
- [Octave-bug-tracker] [bug #57255] "+=" not parsed correctly in anonymous function within cell array context, Rik, 2019/11/19
- [Octave-bug-tracker] [bug #57255] "+=" not parsed correctly in anonymous function within cell array context, John W. Eaton, 2019/11/19
- [Octave-bug-tracker] [bug #57255] "+=" not parsed correctly in anonymous function within cell array context, Rik, 2019/11/19
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, Rik, 2019/11/19
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, John W. Eaton, 2019/11/20
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, Rik, 2019/11/21
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, John W. Eaton, 2019/11/21
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, Rik, 2019/11/21
- [Octave-bug-tracker] [bug #57255] "+=" in anonymous function within cell array context should emit an error, John W. Eaton, 2019/11/22