Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 839
Tomasz Kasperczyk Avatar asked Nov 29 '14 15:11

Tomasz Kasperczyk


2 Answers

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;
like image 179
Abstract type Avatar answered Sep 29 '22 05:09

Abstract type


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.

like image 30
Marco van de Voort Avatar answered Sep 29 '22 05:09

Marco van de Voort