bugGNU Octave - Bugs: bug #39257, handles to nested functions are...

 
 

bug #39257: handles to nested functions are not yet supported

Submitted by:  None
Submitted on:  Sat 15 Jun 2013 12:08:54 AM UTC  
 
Category: InterpreterSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Matlab Compatibility
Status: ConfirmedAssigned to: None
Originator Name: Matthew MoskewiczOriginator Email: -unavailable-
Open/Closed: OpenRelease: dev
Operating System: GNU/Linux

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

(Jump to the original submission Jump to the original submission)

Thu 16 Nov 2017 10:08:00 AM UTC, comment #13:

Just a vote to say that I too am very interested in having Octave officially support handles to nested functions. I frequently use such handles in my libraries and programs and it would be great for my users if they could run my codes under Octave out of the box. If it would be helpful, I could help test such patches by trying my codes with them. Tim

Anonymous
Sat 17 Jun 2017 05:51:23 PM UTC, comment #12:

I have tried to apply the last patch, but it failed.
the log attached.
Please help...

(file #40931)

Avinoam Kalma <avinoam>
Project Member
Fri 28 Apr 2017 07:19:13 AM UTC, comment #11:

Hi,

I applied the most recent patch set and tested this:

The code fails with the error message:

"Handles of nested functions cannot be function return values or the value of global variables when a function returns"

This is a severe limitation for me, especially considering I can return handles to anonymous functions. Can this feature be added in a new patch?

Running the nested function inside the host instead of returning a handle works though.

CH <atcl>
Wed 01 Mar 2017 01:24:44 AM UTC, comment #10:

Hi Lachlan, I'm very keen to use your patch since I have lots of cellfun(@func,cellA) in my codes, where "func" is a nested function defined within a main function containing the cellfun(@func,cellA). But since I'm on Windows and using the latest Octave 4.2.0, could you please let me know how can I patch and recompile it? Thanks in advance!

Random Nobody <testplayer>
Fri 15 Jul 2016 11:59:53 AM UTC, comment #9:

Here is a refreshed patch, reflecting recent code changes.

(file #37889)

Lachlan Andrew <lachlan>
Project Member
Tue 16 Feb 2016 10:23:44 PM UTC, comment #8:

Quentin, the fix requires patching the source and recompiling.

Is there a reason you can't do that, or is it just that you have never done it before? What OS are you running? If you have time, I could try to talk you through the steps.

I've also attached an updated patch; the previous one failed when returning non-scalar structures.

(file #36358)

Lachlan Andrew <lachlan>
Project Member
Tue 16 Feb 2016 02:23:30 PM UTC, comment #7:

I have been using a lot of handles + nested functions with Matlab in order to build GUIs. It seems that you have found a solution to solve this trouble in Octave.
Is there any way for me to try it or it has to be implemented in the source (thing that I can not do)?

Quentin Juette <qjuette>
Sun 07 Feb 2016 11:00:21 AM UTC, comment #6:

The attached patch allows a handle to be taken of a nested function, and prevents that handle being exported (either as a return value or through a global variable) out of the scope in which the shared variables are defined.

This does not allow constructs like the one in comment #3. However, it should allow GUIs made through Guide. Please report back here if they cause problems.

I plan to check how Matlab handles constructs like comment #3. Specifically, I want to know if it use the most recent value assigned to x, including subsequent calls to test_closure, or if it sticks with the value of x at the function exit. In the former case, we could just make x persistent. In the latter case, the handle would somehow have to keep its own copy of x, like an anonymous function does.

(file #36269)

Lachlan Andrew <lachlan>
Project Member
Sat 06 Feb 2016 12:20:52 PM UTC, comment #5:

Rik, you wrote in comment #1 that "during the implementation of subfunctions, there was something really troublesome about handles to nested functions". Do you by any chance remember the trouble (from back in 2013)?

When I remove the explicit prohibition, and try the code in comment #3, it complains that x is undefined. If that is the problem, I'm interested in exploring solutions.

That problem only occurs if the return value is a handle to a nested function that accesses a variable from a higher scope. That shouldn't affect the GUIDE-generate code, or code (such as mine) written by tweaking Matlab example code.

If a return value contains such a handle, one approach may be to copy the parse tree and replace all references to those variables from a higher scope by references to local copies of those variables, initialized at the start of each call to have the value at the time the handle was created.

A complication would be if this function called another nested function which referred to different variables from the higher scope.

If this issue of using variables after they go out of scope was the problem, the error could just be replaced by a warning, and people could use the handles for the cases that do work.

Thoughts?

Lachlan Andrew <lachlan>
Project Member
Thu 09 Jul 2015 02:57:31 PM UTC, comment #4:

I'd like to add another data point for why it would be nice to have this ability, now that Octave supports nested functions. I have a large GUI built in Matlab Guide that I'd like to export. Matlab will put all the figure generation code into one .m file so you don't have to read the .fig file for the GUI. However, the way Matlab implements the code for the callbacks in Guide GUIs is via calls to nested function handles.

This seems to me to be a fairly large class of Matlab scripts that it would be really nice to be able to run in Octave.

Anonymous
Wed 10 Jun 2015 07:49:31 PM UTC, comment #3:

I can confirm that the issue is still present in octave 4.0.0.
As a consequence one cannot use nested function to define a closure. The following code would run in matlab but not in octave:

where test_closure.m is defined as:

Alexander Barth <abarth>
Mon 24 Jun 2013 01:01:29 AM UTC, comment #2:

Marking the issue as confirmed. I also just happened to have found a use for handles to nested functions in one of the core Octave m-files. I had to work around it too.

Rik <rik5>
Project Administrator
Thu 20 Jun 2013 03:21:35 AM UTC, comment #1:

From what I remember during the implementation of subfunctions, there was something really troublesome about handles to nested functions and they were therefore specifically disallowed. Unfortunately, this probable means that this issue won't get fixed soon.

Rik <rik5>
Project Administrator
Sat 15 Jun 2013 12:08:54 AM UTC, original submission:

as per:
http://hg.savannah.gnu.org/hgweb/octave/rev/be18c9e359bf

nested functions with scope rules compatible with matlab are now supported. however, also as per the patch, it appears that handles to nested functions are not yet supported. i didn't find any discussion of this issue or an existing open bug.

i ran into this limitation when trying to run some computer vision related code from here with octave 3.7.5:
http://www.vision.caltech.edu/Image_Datasets/CaltechPedestrians/
code archive:
http://www.vision.caltech.edu/Image_Datasets/CaltechPedestrians/files/code3.0.1.zip

the file dbBrowser.m uses many references to subfunctions.

Anonymous

 

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

Attach File(s):
   
   
Comment:
   

Attached Files
file #40931:  bug_39257_v3.log added by avinoam (1KiB - application/octet-stream)
file #37889:  bug_39257_local_v3.cset added by lachlan (10KiB - text/x-diff)
file #36358:  bug_39257_local_v2.cset added by lachlan (10KiB - application/octet-stream)
file #36269:  bug_39256_local.cset added by lachlan (9KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by amro_octave
  • -unavailable- added by marsian
  • -unavailable- added by avinoam (Updated the item)
  • -unavailable- added by atcl (Posted a comment)
  • -unavailable- added by testplayer (Posted a comment)
  • -unavailable- added by qjuette (Posted a comment)
  • -unavailable- added by lachlan (Posted a comment)
  • -unavailable- added by abarth (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by None (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 7 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Thu 07 Dec 2017 04:46:45 PM UTCamro_octaveCarbon-Copy-=>Added amro_octave
    Tue 14 Nov 2017 08:51:21 PM UTCmarsianCarbon-Copy-=>Added marsian
    Sat 17 Jun 2017 05:51:23 PM UTCavinoamAttached File-=>Added bug_39257_v3.log, #40931
    Fri 15 Jul 2016 11:59:53 AM UTClachlanAttached File-=>Added bug_39257_local_v3.cset, #37889
    Tue 16 Feb 2016 10:23:44 PM UTClachlanAttached File-=>Added bug_39257_local_v2.cset, #36358
    Sun 07 Feb 2016 11:00:21 AM UTClachlanAttached File-=>Added bug_39256_local.cset, #36269
    Mon 24 Jun 2013 01:01:29 AM UTCrik5StatusNone=>Confirmed

    Back to the top


    Powered by Savane 3.1-cleanup1