[PLA] Comment to PLA00004: simplification of expression blocks

Andreas Krennmair ak at synflood.at
Sun Nov 25 13:17:46 CET 2007


Hello everyone,

I read through PLA00004 and what struck my eye was this code example:

	a := 23;
	b := (
		x := 42;
		a := a + x;
		a * 3;
	);
	b = b + a;

Later on, function definitions are exemplarily described in the
following code snippet:

	sayhello := {
		print("Hello World!\n");
	};

As far as I understand, the difference between these two constructs is
that code within ( ) is evaluated immediately, while code within { }
represents a code block that can be assigned to a variable, which can
then be called (by specifying an argument list, e.g. sayhello() )

So, if I understand this correctly, why would the ( ) be needed at all?
An expression such as 

	( m * c * c )

Could instead be expressed as 

	{ m * c * c }()

right?

Regarding variable scopes in this example: to have the { }() construct
behave like the () construct, we need to take care of variables that are
outside the scope of the { } block. The (IMHO) obvious strategy for
variable name resolution is to resolve with all declared variables at
the time of creation of the { } block, i.e.

	a := 3;
	foo := { a * a }

would be ok, while

	foo := { a * a };
	a := 3;
	b := foo()

would yield an error at line 1, as variable a is undeclared at that
time. I don't see anything in PLA00004 that contradicts this strategy.
In addition, you get nested functions (with Pascal-style behaviour wrt
variable scoping) for free:

	e := {
		f := { %x + %y };
		f(y: 3);
	};
	e(x: 4) // evaluates to 7

Just my 2.97 US-¢. ;-)

Regards,
Andreas


More information about the PLA mailing list