March 28, 2013
Delphi coming ARM compiler (for iOS) adds a nice feature to the language. Class operators overloading.
Delphi coming ARM compiler (for iOS) will add a nice feature to the language. Operators overloading for classes, a feature that is currently only for records.
This means you can write a class like this:
type
TNumber = class
private
FValue: Integer;
procedure SetValue(const Value: Integer);
public
property Value: Integer read FValue write SetValue;
class operator Add (a, b: TNumber): TNumber;
class operator Implicit (n: TNumber): Integer;
end;
And use it as follows:
a, b, c: TNumber;
...
c := a + b;
ShowMessage (IntToStr (c));
This new feature is a side effect of the new Automatic Reference Counting support that will be in the ARM compiler and I'll cover in details. In fact, the temporary objects created by expressions are managed in memory, with no need to free them manually. Sweet.
posted by
marcocantu @ 7:30PM | 16 Comments
[0 Pending]
16 Comments
Class Operators in Delphi
When will the objects be freed? At the end of the
method (like what happens for interfaces), or once the
reference counting gets to zero (in your example, just
before the ShowMessage statement)?
Comment by Nicholas Ring
[]
on March 28, 21:32
Class Operators in Delphi
this is really not new, Delphi for. NET compiler had this feature
Comment by Johnson on March 29, 01:19
Class Operators in Delphi
Nicholas, objects (including temporary ones) are reference counted, and
the reference count is decreased when they go out of scope (typically at
the end statement of a method, if they are declared locally). If there only
one reference, they are destroyed. You can alter this behavior in different
ways, for example setting a variable to nil.
Comment by Marco Cantu
[http://www.marcocantu.com]
on March 29, 09:12
Class Operators in Delphi
When will we see CO in Delphi Win32/64?
Comment by Peter on March 29, 15:08
Class Operators in Delphi
Marco: You post states that the ARM compiler (for
iOS) brings this feature. Will it also be available
for VCL/Windows?
Comment by Greg
[http://www.porpoisemedia.com]
on March 29, 15:08
Class Operators in Delphi
Cool, kinda' C-like but still cool.
Comment by ObjectMethodology.com
[http://www.objectmethodology.com]
on March 29, 15:50
Class Operators in Delphi
There was sign of a new memory model since Delphi xe3 in the
system.pas unit.
See http://blog.synopse.info/post/2011/12/08/Avoiding-Garbage-
Collector%3A-Delphi-and-Apple-on-the-same-side
And http://blog.synopse.info/post/2012/10/06/Delphi-XE3-is-
preparing-reference-counting-for-class-instances
I'm very pleased to see that the ARC model was chosen, and not a
GC.
I hope it will be available for all platforms. :-)
Comment by Arnaud Bouchez
[http://synopse.info]
on March 29, 21:04
Class Operators in Delphi
I'm happy to see Delphi language being upgraded. Yet
there are things can be taken from C.
Comment by Denis on March 30, 18:45
Class Operators in Delphi
Any chance of adding the ability to have multiple Record
Helpers for Intrinsic Types ?
The current version with its single helper per type is a
limitation that we would like to see removed...
Regards
Andrew
Comment by Andrew Tierney
[http://www.castlesoft.com.au]
on March 30, 23:27
Class Operators in Delphi
Marco, sounds great! I'm also interested in if both this feature and/or
reference-counted objects will work for another platform target - ie,
Windows or OSX - or if it's just iOS.
Comment by David M
[]
on March 31, 12:57
Class Operators in Delphi
Im pretty sure that class operators are usable in
Delphi starting from version 2010 (maybe even
earlier), but only in the context of advanced records,
not classes.
Comment by Jikulis on April 2, 14:28
Class Operators in Delphi
In fact class operators were introduced for records with
Delphi 2006.
Comment by Sebastian Jänicke on April 3, 15:01
Class Operators in Delphi
Current record operator overloading implementation in
Delphi does not allow to overload record assignment.
You can write overloaded Implicit or Explicit
operators but they are ignored and records with
overloaded operators are assigned like any records -
by (shallow) copy.
The inability to overload assignment creates problem
when the record has members that point to heap
objects. The solution I currently use is to access
these heap objects via refcounted interfaces.
Class operator overloading may suffer from the same
problem.
Regards, Serg
Comment by Sergey Kasandrov
[http://sergworks.wordpress.com]
on April 8, 07:07
Class Operators in Delphi
"...with no need to free them manually. Sweet."
Do'in it for me! I like it.
Comment by Rick
[http://visualdelphi.wordpress.com]
on November 28, 20:00
Class Operators in Delphi
Perhaps we could get this for interfaces at least
then.
In the meantime, I suppose proxy records would be the
way to go.
Comment by C Johnson on May 14, 19:42
Class Operators in Delphi
I think interface operator could be implemented without free problem.
(TInterfacedObject free automatic)
The size of problem is similar to the solved interface property feature.
Comment by Feri on June 13, 05:58
Post Your Comment
Click
here for posting
your feedback to this blog.
There are currently 0 pending (unapproved) messages.