bugGNU Octave - Bugs: bug #40938, interaction among eval, anonymous...

 
 

bug #40938: interaction among eval, anonymous function, and functions

Submitter:  Felipe G. Nievinski <fgnievinski>
Submitted:  Tue 17 Dec 2013 02:19:29 PM UTC
   
 
Category:  Interpreter Severity:  2 - Minor
Priority:  1 - Later Item Group:  Matlab Compatibility
Status:  Invalid / Not an Octave Bug Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Wed 26 Feb 2020 07:07:36 AM UTC, comment #4: 

Agree to Andrew.  Maybe things changed in Matlab since 2014, or they read and fixed their bug before Octave became compatible to it ;-)

For Matlab R2019a I get:


>> doit_good

pi2 =

    3.1416

>> doit_bad
Error using eval
Undefined function or variable 'pi2'.

Error in doit_bad>@(var)eval([var,'2']) (line 2)
  pi2 = pi;  get2 = @(var) eval([var,'2']);  get2('pi')

Error in doit_bad (line 2)
  pi2 = pi;  get2 = @(var) eval([var,'2']);  get2('pi')


Closing item.

Kai Torben Ohlhus <siko1056>
Group Member
Tue 25 Feb 2020 11:37:43 PM UTC, comment #3: 

Are you absolutely sure that doit_bad() actually works in Matlab, and you haven't defined a global function named "pi2" or something like that? Because that's not how nested functions are supposed to work. They don't get references to enclosing workspaces; they only get copies of the individual variables that they explicitly statically reference, at function handle creation time. That doit_bad() should result in a run time error complaining about an "undefined function or variable" in a default Matlab setup. Unless something has changed in recent Matlab versions.

Andrew Janke <apjanke>
Fri 25 Nov 2016 10:43:38 PM UTC, comment #2: 

This behavior is still the same in Octave 4.2.0.

Hartmut <hardy>
Mon 29 Dec 2014 03:58:52 AM UTC, comment #1: 

Confirmed.  My guess is that Matlab is treating the anonymous function as a nested function, in which case it has access to the enclosing function's variables.  Octave is probably treating this as a subfunction, in which case the scope does not include the caller's variables.

This sort of esoteric compatibility is a low priority and I've marked the bug as such.

Rik <rik5>
Group administrator
Tue 17 Dec 2013 02:19:29 PM UTC, original submission:  

This works in Matlab but doesn't in Octave:

function doit_bad ()
  pi2 = pi;  get2 = @(var) eval([var,'2']);  get2('pi')
end

In the base workspace (i.e., entered in the terminal), that line statement doesn't work in Matlab nor in Octave.

In contrast, this version works in all combinations of Matlab/Octave and within-function/base-workspace:

function doit_good ()
  pi2 = pi;  get2 = @(var) evalin('caller', [var,'2']);  get2('pi')
end

So it seems there's some interaction among eval, anonymous function, and functions.

Felipe G. Nievinski <fgnievinski>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #29987:  doit_good.m added by fgnievinski (99B - text/plain)
file #29988:  doit_bad.m added by fgnievinski (86B - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Updated the item)
  • -email is unavailable- added by siko1056 (Posted a comment)
  • -email is unavailable- added by apjanke (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by fgnievinski (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 11 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-02-26 jwe Open/ClosedOpen Closed
    2020-02-26 jwe Open/ClosedClosed Open
    2020-02-26 siko1056 StatusConfirmed Invalid / Not an Octave Bug
        Open/ClosedOpen Closed
    2020-02-25 mtmiller Carbon-CopyRemoved 80942 -
    2016-03-31 mtmiller Severity3 - Normal 2 - Minor
        Operating SystemMicrosoft Windows Any
    2014-12-29 rik5 Priority5 - Normal 1 - Later
        StatusNone Confirmed
    2013-12-17 fgnievinski Attached File- Added doit_good.m, #29987
        Attached File- Added doit_bad.m, #29988

    Back to the top

    Powered by Savane 3.13-3230.
    Corresponding source code