Continuing the theme of recent – and upcoming – posts about new (and not so new) syntax in modern (and not so modern) variations on the Pascal language, I just have to comment on what I regard as yet another stunningly good job that the guys at RemObjects have done in their “Nougat” flavoured Oxygene. Specifically in relation to how they have implemented the named method parts syntax in Objective-C.
The example that Marc Hoffman provides in his recent blog post, is the constructor method for a string,
This would be called in Objective-C thusly:
[myString stringByPaddingToLength: 9 withString: @"." startingAtIndex:0];
How on earth can you contrive to make such a call in a Pascal derived language ?
Well, as Marc describes, there are a number of possible approaches, most of which are dictated by whether you are extending the language or merely providing a bridge from the language into the runtime.
The bridge approach results in the most cumbersome – and imho downright ugly – solutions but remains strictly and technically within the limits of the existing Pascal language.
A first cut at providing a bridge to this Objective-C API might simply ignore named method parts by implementing them as simple parameters to the base method name, overloading where necessary:
myString.stringByPaddingToLength(9, '.', 0);
Or you might encode the method part names in the method itself either by preference or by necessity (if there are methods whose method parts differ in name, but not type – though I am not as yet away of any such examples in the Objective-C runtime):
myString.stringByPaddingToLengthWithStringStartingAtIndex(9, '.', 0);
Not Your Daddy’s Pascal
Whilst both of these are demonstrably “Pascal”, within the existing syntax of the language, neither maps very comfortably or intuitively to the Objective-C runtime.
Extending the language, by contrast, creates something that is arguably not Pascal at all – by definition, since it involves changes or additions to the syntax – but which is, ironically, the most Pascal-like in terms of preserving elegance and clarity not only in the language but in way that it exposes the underlying RTL:
// [myString stringByPaddingToLength: 9 withString: @"." startingAtIndex:0]; myString.stringByPaddingToLength(9) withString('.') startingAtIndex(0);
imho, not only is this cleaner code and closer to the underlying RTL, but it is, if anything, an improvement even on the “native platform” code for that RTL!
Whilst some might rail at this, crying “But this isn’t Pascal!!“, I would ask whether this would be considered more or less “Pascal-ish” than:
TMyWindowClass = class(TMyBaseControl) procedure WMPaint(var aMessage: TWMPaint); message WM_PAINT; end;
Colour Me Excited
For myself, I am increasingly excited to see how RemObjects approach targeting new platforms with the language that I am most familiar with, in the same way that I was excited when I first saw Delphi 1.0 and how the language was adapted to suit Windows, back in the day.