Peter N Lewis wrote:
CW takes the approach that any unknown type identifier it sees in a type definition is pointer sized (a pointer or object) and must be defined by the end of the type block.
Does it allow the same in nested scopes?
Yes.
procedure Test; type a = record b: c; end; c = ^Integer; begin end;
AFAIU objects are not allowed in nested scopes, but pointers still make sense.
Correct. As near as I can tell, it removes the keyword "object" once you leave the main unit/program scope. Once in a nested scope, this:
procedure Test4; type ObjectBase = object function Doit: Integer; end; begin end;
gives an error "; expected" at "function" because object is no longer recognized as being a keyword, and so is assumed to be a forward pointer type definition.
Also, what happens if the name is a predefined type (or a name declared in outer scope)? Normal Pascal rules are that the new definition should be used (even if the definition follows use), are they obeyed?
No, they are not obeyed. This compiles:
procedure Test5; type ch = Integer; Integer = Char; var c: ch; d: Integer; begin c := 42; d := 'a'; end;
Please note, I was not in any way suggesting that GPC implement this technique. Only explaining CW's solution to the problem, which does have a degree of elegant simplicity, even if it is not entirely standards compliant.
But I agree, in practice with non-reference objects, the issue is a lot less prevalent.
Actually, once we have procedural types in the language forward type declarations could be usefull also for records, functions and procedures:
type automaton = function (input) : automaton;
would be a reasonable declaration for a function implementing a single step transition of a finite state machine (returning a new transition function, for the next step).
Agreed, but once you step away from the "forward definitions for pointer sized types only" policy, you get in to trouble with the types only being allowed in certain uses (primarily, not as fields for records, unless you are going to go back and recalculate all the record locations afterwards, which I guess would be doable).
ATM I have implemented OOP forward declaration:
type o = class .. end; ...
It requires the `class' keyword. There is a problem, that `class' keyword is (ATM) not accepted in Mac Pascal mode. It would be easy to implement syntax with `object' as an alternate keword, but than we would have to decide what to do if `object' is used in a forward declaration in one mode, but the proper declaration uses different mode.
I would think it would be reasonable that it work only in reference-based-object mode both at the time of forward definition and the time of actual definition.
As described previously, forward definitions for non-pointer sized types is much harder, and forward definitions for non-reference based objects is much less commonly needed.
I'll try out your patch, hopefully today. I'll either bash it so it works with object .. end, or I'll try this:
type obj = class .. end; ... obj = object ... end;
which probably will work currently and solve my short term issue with object support.
Thanks, Peter.