Dear Chris,
The example results in a run-time error:
Runtime error: 'missing lambda return' fatal error
Runtime error occurred in ? (29), call trace:
(29): ?
The code is invalid, but this was not clear from the documentation. I have added
the following after noting that variables are captured by value:
However, internally, the local variables are captured by reference. So a lambda
function that accesses local variables can only be used when these are in
existence. As a consequence, care is required returning a lambda function: when
the function surrounding the lambda returns, the local variables disappear, and
the lambda is invalid. In other words: the closure is incomplete, because it is
by reference. This implementation is less flexible than, say, JavaScript, but is
efficient because large matrices are not unneccesarily copied.
If you wish to create a function with closure it is better to do it as a member
of an object (but similarly, you must ensure that the object persists while
using the function). This is because a member function in Ox automatically
captures its object.
I'm finding Lambda functions increasingly useful, but am not so keen on the [=]
notation which is modelled on C++.
Best wishes, Jurgen
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
oxoxoxox 16th OxMetrics user conference 2015
oxoxoxox Cass Business School London, 3-4 September
oxoxoxox 2015 Econometrics Summer School
oxoxoxox TBC
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
Dr Jurgen A Doornik
James Martin Fellow, Institute for New Economic Thinking
at the Oxford Martin School, University of Oxford
http://www.doornik.com
http://www.oxmetrics.net
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
On 2015-03-27 14:43, Chris Ferrall wrote:
> The attached program demonstrates an unexpected crash when using the new Lambda function capability. It seems to happen when using a locally declared variable within a static method context. The program is a bit convoluted, and a simpler demonstration may exist, but this isolates what happens in a much larger program. In fact, when the larger program crashes, Ox does not print out a nice error message, so tracking this down was not easy.
>
> The program computes a parameter in a model that is a function of data (stored in static G::x) and coefficients (stored in static G::b). The parameter is a probability so it is specified as prob = exp(x*b)/(1+exp(x*b)). Because b can change, prob needs to be a function, either a static method of G or a Lambda function defined inside a static method of G.
>
> As written the program uses a Lambda function and it runs and prints out the value:
> --------------- Ox at 10:13:29 on 27-Mar-2015 ---------------
> prob=
> 0.57444
>
> Remove the comments at Step 1 to use the local variable v0 to avoid two exp() calls and the program crashes:
>
> --------------- Ox at 10:25:24 on 27-Mar-2015 ---------------
> Runtime error: 'missing lambda return' fatal error
> Runtime error occurred in ? (28), call trace:
> (28): ?
>
> The issue is with using the local variable not the reference to static members of the Lambda context.
>
> Remove the comments at Step 2 to use a static method instead of the Lambda function and the program runs (not surprising).
>
>
>
|