Discussion:
Closures
(too old to reply)
August Karlstrom
2007-02-19 22:59:43 UTC
Permalink
Hi everyone,

What should the following program print and why? With the OO2C compiler
I get 0 but I think it should be 1.

MODULE Test;

IMPORT Out;

PROCEDURE Q(p: PROCEDURE); BEGIN p END Q;

PROCEDURE P;
VAR n: LONGINT;
PROCEDURE Inner; BEGIN INC(n) END Inner;
BEGIN
n := 0;
Q(Inner);
Out.Int(n, 0); Out.Ln
END P;

BEGIN
P
END Test.


Regards,

August
Chris Burrows
2007-02-20 00:21:59 UTC
Permalink
Post by August Karlstrom
Hi everyone,
What should the following program print and why? With the OO2C compiler
I get 0 but I think it should be 1.
MODULE Test;
IMPORT Out;
PROCEDURE Q(p: PROCEDURE); BEGIN p END Q;
PROCEDURE P;
VAR n: LONGINT;
PROCEDURE Inner; BEGIN INC(n) END Inner;
BEGIN
n := 0;
Q(Inner);
Out.Int(n, 0); Out.Ln
END P;
BEGIN
P
END Test.
Normally, in Oberon-2, Q(Inner) should not compile as local procedures may
not be used as procedure variables.

--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/gpcp
August Karlstrom
2007-02-20 18:35:58 UTC
Permalink
Post by Chris Burrows
Post by August Karlstrom
Hi everyone,
What should the following program print and why? With the OO2C compiler
I get 0 but I think it should be 1.
MODULE Test;
IMPORT Out;
PROCEDURE Q(p: PROCEDURE); BEGIN p END Q;
PROCEDURE P;
VAR n: LONGINT;
PROCEDURE Inner; BEGIN INC(n) END Inner;
BEGIN
n := 0;
Q(Inner);
Out.Int(n, 0); Out.Ln
END P;
BEGIN
P
END Test.
Normally, in Oberon-2, Q(Inner) should not compile as local procedures may
not be used as procedure variables.
OK, thanks.

Now when I look in the Oberon-2 report I find it in section 6.5.


August

Loading...