At 6:34 -0400 24/7/05, CBFalconer wrote:
Peter N Lewis wrote:
New(s,57) gives a runtime error on failure, and so the compiler knows that it need not bother checking.
s := New(String,57) potentially returns nil, so the compiler checks.
I'm not sure if that is reasonable. It makes a certain kind of sense in some respects - New as a procedure has no return value and thus nothing to report, New as a function returns a pointer (even for reference objects anyway) and therefore can return a success/failure result.
To start with, new is a standard PROCEDURE, not a function.
New (builtin) is both a procedure and a function. From the documentation:
New (Under construction.) Synopsis procedure New (var P: any Pointer ); or procedure New (var P: Pointer to a variant record; tag fields ); or procedure New (var P: Pointer to a schema; discriminants ); or procedure New (var P: Pointer to an object; constructor call ); or function New (any Pointer type ): same type ; or function New (variant record Pointer type ; tag fields ): same type ; or function New (schema Pointer type ; discriminants ): same type ; or function New (object Pointer type ; constructor call ): same type ;
This program compiles:
program peterT2;
type StringPtr = ^String; var s: StringPtr; begin New( s, 57 ); s := New( StringPtr, 57 ); end.
The constants following the argument specify variants and allow assignment of space for a particular variant of the actual type.
Yes, sort of. The constants can also be Schema Discrimenants, as in my example (except I should have used StringPtr as the type, not String).
You can only do the sort of thing you recommend if you override the level 0 predeclaration of new, which in turn makes memory
I'm not talking about overriding the New builtin. It is not possible to write a procedure with the semantics of the builtin New in Pascal (this has always seemed a weakness of Pascal to me, that it includes procedures like WriteLn and New that cannot actually be written in the language, but that is more a philosophical question).
Enjoy, Peter.