Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-03

Chaining method calls

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

Is it possible to achieve this dynamically without resorting to type casting? It feels like classes should have a builtin type for each class, like TClassType, which similar to TObject.ClassType but is a compile time type.

=========

{$mode objfpc}
program test;
type
  TBase = class
    // TClassType should be the type of the current class
    private type TClassType = ...;
    public
      function AndThis: TBase;
  end;
function TBase.AndThis: TBase;
begin
  result := self;
end;
type
  TMyClass = class (TBase)
  end;
var
  c: TMyClass;
begin
  // error: Incompatible types: got "TBase" expected "TMyClass"
  c := TMyClass.Create.AndThis;
  writeln(c.classname);
end.

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Ryan Joseph

Free Pascal - General mailing list wrote

Is it possible to achieve this dynamically without resorting to type casting? It feels like classes should have a builtin type for each class, like TClassType, which similar to TObject.ClassType but is a compile time type.

I didn't think enough before sending this because this clearly isn't a compile time issue. Currently this probably isn't possible unless there is an "AnyClass" type or something like that. Not sure if that exists in Pascal but methinks not.

-----
Regards,
Ryan Joseph

Reply by email

Michal Wallace
to Ryan Joseph

Hi Ryan,

It's possible. Just change your declaration of `c`:

var c: TBase;

It still prints TMyClass at the end.

Of course, you're only going to have access to methods that are declared in TBase.
You can also approach this sort of thing with interfaces...

-Michal ( http://tangentstorm.com/ )

On Tue, Mar 24, 2020 at 4:08 AM Ryan Joseph via fpc-pascal <

Show quoted text 25 lines
wrote: Free Pascal - General mailing list wrote > Is it possible to achieve this dynamically without resorting to type > casting? It feels like classes should have a builtin type for each class, > like TClassType, which similar to TObject.ClassType but is a compile time > type. I didn't think enough before sending this because this clearly isn't a compile time issue. Currently this probably isn't possible unless there is an "AnyClass" type or something like that. Not sure if that exists in Pascal but methinks not. ----- Regards, Ryan Joseph -- Sent from: http://free-pascal-general.1045716.n5.nabble.com/ _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Ryan Joseph
to Michal Wallace
Show quoted text 13 lines
On Mar 24, 2020, at 8:39 PM, Michal Wallace via fpc-pascal wrote: Hi Ryan, It's possible. Just change your declaration of `c`: var c: TBase; It still prints TMyClass at the end. Of course, you're only going to have access to methods that are declared in TBase. You can also approach this sort of thing with interfaces...

True, but then we're stuck in a typecasting game where I need to cast "c" back to TMyClass. :) I probably brought this up years ago and forgot but Objective-C has an "id" type which is "can assign to any class type". It seems like something that would be standard for OOP but I don't recall ever seeing it in Pascal or C++.

Regards,
Ryan Joseph

Reply by email

George Bakhtadze
to Ryan Joseph

Hi.

You can have such ID if you will use generics:

{$mode Delphi}
type
  TBase<T: class> = class
  public
    function AndThis: T;
  end;
function TBase<T>.AndThis: T;
begin
  result := self as T;
end;
type
  TMyClass = class;
  TMyClass = class (TBase<TMyClass>)
  end;
var
  c: TMyClass;
begin
  c := TMyClass.Create.AndThis;
  writeln(c.classname);
end.

Don't know how to write it with objfpc style generics but I believe it's possible.

24.03.2020, 16:55, "Ryan Joseph via fpc-pascal" :

True, but then we're stuck in a typecasting game where I need to cast "c" back to TMyClass. :) I probably brought this up years ago and forgot but Objective-C has an "id" type which is "can assign to any class type". It seems like something that would be standard for OOP but I don't recall ever seeing it in Pascal or C++.

---
Best regards, George

Reply by email