[fpc-devel] Object upgrades

Ryan Joseph genericptr at gmail.com
Mon Jun 10 19:56:09 CEST 2019



> On Jun 10, 2019, at 1:19 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
> 
> I don't think that for classes this should be allowed. Sven and Florian already explained the reasons for this in detail.

Yes but that was in regards to binary operators which would have memory management issues. The operators I listed return boolean so there’s risk. It would be pretty inconsistent however.


> First of all, I don't think you need to make objects "look" more like classes.
> 
> There is nothing wrong with alerting people that they're using a different
> kind of animal when using objects. I for one want to see at a single glance
> what code does. So seeing "New" instead of "Create" alerts me to the fact that I am
> dealing with objects. I consider this a plus, and if they start sharing more
> functionality, this plus becomes more important.
> 
> So IMO there is no need to change the compiler for this.
> Just add a static function that returns a new instance, which you can call "create" if this pleases your sense of beauty or symmetry.

I don’t think this is possible. Just did little test and the size of “result” is always the size of the base class. Does this work?

Also FPC doesn’t have something “self” but for the class level so you need to cast the return value. Btw adding “ClassSelf” is something I’ve wanted for a while, so I can reference the class type within the class declaration.

type
  PBase = ^TBase;
  TBase = object
    x: integer;
    constructor Initialize;
    class function Create: PBase; static;
  end;

class function TBase.Create: PBase;
begin
  writeln('create: ', sizeof(result));
  new(result, Initialize);
end;

constructor TBase.Initialize;
begin
  writeln('create tfoo')
end;

type
  PChild = ^TChild;
  TChild = object (TBase)
    y: string;
  end;

var
  c: PBase;
begin
  c := TChild.Create;
end.


Regards,
	Ryan Joseph




More information about the fpc-devel mailing list