[fpc-devel] Postfix operator overload or default properties

Ryan Joseph genericptr at gmail.com
Fri Jun 7 22:41:05 CEST 2019


I was thinking about my patch for default properties and how invasive the changes are into the compiler and heavy handed the approach is. I’m not even sure the compiler team would accept it like it is since it has such potential for overreach and having strange unpredictable effects.

In my opinion C++ has a more reasonable approach which does 95% of the things we actually need for the use cases (i.e. nullable types/ARC-like classes, like shared_ptr in C++). Their approach is to allow overriding the “postfix" operator (-> in C++ but “.” for classes in FPC) which is then used in conjunction with various other operator overloads (like := and =). 

I think this solution is better because:

1) The changes in the compiler sources are maybe 10% of what they need to be with default properties.
2) The user can better manage access to the underlying class instead of hoisting up the entire namespace up for all possible circumstances. This approach gives the user much greater control overall.
3) The effects are very predictable, that is, you know it only affects the record when you use “.”. Default properties need to have wide-reaching effects in a multitude of different syntaxes.
4) It has the support of the C++ team and years of real world usage (std::shared_ptr etc…).

Does that make sense? I’d like to scratch the idea of default properties and do this instead if it was permitted.

========================

type
  TAuto<T> = record
    m_obj: T;
    // … other management operators …
    class operator postfix: T;
  end;

class operator TAuto.postfix: T;
begin
  result := m_obj;
end;

var
  a: TAuto<TObject>;
begin
  writeln(a.ClassName); // a.m_obj.ClassName

Regards,
	Ryan Joseph




More information about the fpc-devel mailing list