2005-10-10
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 4 of 5 )
's New, Pascal?">
What's New, Pascal?
Except for the help file omissions, the little things are worthy of overlooking for the meat of Chrome: the bold expansion of the language into previously undiscovered country.
There are some minor extensions and alterations of interest, some you might not notice at all at first. I didn't notice the addition of the keyword “method” at first, and only peripherally paid attention to the replacement of the “unit” keyword with “namespace”. (Though it's cool that you can use a namespace across multiple source files.)
The more you work with it, the more you notice nice, little touches. These are especially nice if you've been using Pascal and Delphi for a long time: the try...except block can have a finally; there's an async method designation that can be used to easily spawn worker threads; you can specify an unbounded multi-dimensional array merely by leaving off the upper bounds. Deeper changes also emerge.
The Chrome compiler is not a single-pass compiler like Delphi, so you're allowed to make circular references. I think experienced Delphi programmers learn to think without circular references but a lot of beginners have had trouble trying to use unit A from unit B while using unit B from unit A.
It's not all freedom, though. Chrome defaults to restricting globals. This a typically well-handled change. Globals are necessary for backwards compatibility, but also generally considered non-optimal. The default encourages good behavior but you're allowed to turn this off if you must.
Another interesting prospect is the “delegate.” A delegate is a method type used in an event delegation. And, well, at this point, this seems to be just window dressing. But the concept behind it addresses a long-standing grievance I've always had with GUI builders: They encourage the very bad practice of attaching all code to form events. I can see the desire in Chrome to rectify that, even if not yet fully realized.
Putting a Contract Out
For me, the most interesting extension is in what's known as class contracts. I first encountered the concept of class contracts about 10 years ago when reading Bertrand Meyer's Eiffel books, but Borland and Delphi embraced “contractless” programming so it hasn't found its way to Pascal before now.
Actually, “contract programming” and “contractless programming” aren't necessarily opposed, and Delphi has its own cross-platform assert call, but Chrome take it all the way by adding the new keywords require, ensure, invariant and old.
It works like this: Let's say you're building a class to represent a calendar month. You might start by setting the parameters in which the object is correct in an invariants section:
AMonth = public class
private
FDays: Integer {:= 30};
function RealWeeks: Real;
property Days: Integer read FDays write FDays;
public invariants
(FDays > 27) and (FDays < 32);
RealWeeks < 5;
end
The compiler inserts code to validate objects of the AMonth class as needed. In fact, if you were to create an AMonth object, you'd get an error because it starts out in an invalid state, with FDays equal to 0. Assertions are not exceptions; you can simply ignore a contract violation and move on. Therefore, while the above invariants may seem redundant, they aren't necessarily.
Without getting too caught up in contracts, Chrome implements require, ensure, and old, as well as invariants. These can be summed up with a single (silly) example:
procedure SomeClass.SomeMethod(I: Integer); require I > 0; begin Inc(I); ensure old I + 1; end;
The above will guarantee that: the I passed is greater than zero and that when the code is done, the new value of I will be equal to the old value plus one.
When you're done with your project's implementation, you turn off assertions, making them a good way to check for program errors while coding without bogging down your executable.
![]() |
|


