property reintroduce – when a method becomes a property

A tentative new language feature has been pushed to the DWScript repository: “property reintroduce”.

The aim is to allow a method to be reintroduced as a property, and do so in a non-fatal fashion so it can be pushed to production and existing code can be migrated iteratively.

Take the following example:

type
   TMyClass = class
      function Attribute : String;
   end;
...
var o : TMyClass;
...
PrintLn( o.Attribute() );

in that code the extra parenthesis () on the method call are not required but are allowed. However existence of this code would preclude changing Attribute into a property as the above code would fail.

type
   TMyClass = class
      property Attribute : String read GetAttribute write SetAttribute;
end;

Yes, that wouldn’t have happened if Attribute had been declared as a property in the first class, but hey, sometimes that past is the past 🙂
If you had a method, you were faced with the following conundrum:

  • Add a SetAttribute anyway and damn coherence forever
  • Force the change to property, and woe to the heathen that used extra ()
  • Add the property as Attribute2 and have them both live forever like you’re a 1993 COM interface

Enter property reintroduce, it’s a contextual keyword that’s used as follows

type
   TMyClass = class
      property Attribute : String read GetAttribute write SetAttribute reintroduce;
end;

Now if the compiler encounters an Attribute(), instead of failing, it will hint that the () is unnecessary and ignore them.
So the heathens that used the extra () have an opportunity to redeem themselves and remove the () at the next opportunity.

Why a keyword ? well because the extra () cannot always be ignored, if the property returns a function pointer, then those () mean you want to call that function.

 

Leave a Reply

Your email address will not be published. Required fields are marked *