Tomasz Kasperczyk
Tomasz Kasperczyk

Reputation: 2093

Can I define a type inside a class in Object Pascal?

Here's an example (which is not working):

type
    menu = class
        private
            menu_element = RECORD
                id: PtrUInt;
                desc: string;
            end;
        public
            procedure foo();
    end;

Upvotes: 5

Views: 2117

Answers (2)

Marco van de Voort
Marco van de Voort

Reputation: 26358

Free Pascal accepts this if you change the "=" to a ":". Fields are declared with ":", types with "="

{$mode Delphi}
type
    menu = class
        private
            menu_element : RECORD
                id: PtrUInt;
                desc: string;
            end;
        public
            procedure foo();
    end;

procedure menu.foo;
begin
end;


begin
end.

Turbo Pascal and Delphi (and FPC's before 2.2) forbid this. Free Pascal reinstated this old (Classic Pascal) behaviour because of Apple dialects.

Upvotes: 3

Abstract type
Abstract type

Reputation: 1921

Yes you can. But since you want to declare a type, you must type a valid type expresssion

type menu = class
  private
    type menu_element = RECORD
      id: PtrUInt;
      desc: string;
    end;
end;

Upvotes: 5

Related Questions