Archive
Partial classes in Smart Pascal, how?

Partial classes
Ralf Wesseling mentioned something very important to me today, namely that since partial classes is not in Delphi – getting to know this under Smart Pascal, which sadly is seriously lacking in the documentation department, is a bit of a sour apple.
So to rectify this I hope this little document will help.
What is partial classes?
A partial class is, as the name hints to, a class that is only implemented in part. Depending on the language (and even dialect), partial classes can have many aspects. But in essence it comes down to:
- You can define parts of a class spread out over several units
- The class is not sealed (or considered complete) until the churned through everything
Ok — so let’s define a class and see what it looks like:
unit firstunit; interface uses SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, SmartCL.Fonts, SmartCL.Borders, SmartCL.Application; type // Our own new partial class ! TMyButton = partial class(TW3Button) public procedure Testmethod; end; implementation procedure TMyButton.Testmethod; begin end; end.
Nothing to fancy there, in fact, what is the difference?
The difference is that you can go into another unit, one that links to the unit we have our above code in, and then continue to implement it there:
unit secondunit; interface uses firstunit, SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, SmartCL.Fonts, SmartCL.Borders, SmartCL.Application; type // Our own new partial class ! TMyButton = partial class(TW3Button) public procedure Testmethod2; end; implementation procedure TMyButton.Testmethod2; begin end; end.
Normally this would not be allowed. Had we been using non-partial classes, the class in second unit would have to inherit from TMyButton in order to extend it.
What the compiler does when it encounter a partial class, is that it waits until all the units have been parsed and then it collects all the pieces and puts together “the final virtual method table” for that class.
Cool side effects
Start a new visual smart mobile studio project. Just a plain visual project.
Now go to the uses clause and add: “SmartCL.effects” to the list of units.
Now go down to InitializeForm, and write
self.
The code proposal window should pop-up: Notice how suddenly ALL TW3MovableControl based components have suddenly gained 20+ “fx” special effects methods 🙂
TW3MovableControl = partial class(TW3Component) function fxFadeOut(const Duration:Float):TW3MovableControl;overload; Procedure fxFadeOut(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxFadeIn(const Duration:Float):TW3MovableControl;overload; Procedure fxFadeIn(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxWarpOut(const Duration:Float):TW3MovableControl;overload; Procedure fxWarpOut(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxWarpIn(const Duration:Float):TW3MovableControl;overload; procedure fxWarpIn(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxZoomIn(const Duration:Float):TW3MovableControl;overload; Procedure fxZoomIn(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxZoomOut(const Duration:Float):TW3MovableControl;overload; Procedure fxZoomOut(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxScaleTo(const aToX,aToY,aToWidth,aToHeight:Integer; const Duration:Float):TW3MovableControl;overload; Procedure fxScaleTo(const aToX,aToY,aToWidth,aToHeight:Integer; const Duration:Float; const OnFinished:TProcedureRef);overload; function fxMoveTo(const dx,dy:Integer; const Duration:Float):TW3MovableControl;overload; Procedure fxMoveTo(const dx,dy:Integer; const Duration:Float; const OnFinished:TProcedureRef);overload; function fxMoveBy(const dx,dy:Integer; const Duration:Float):TW3MovableControl;overload; Procedure fxMoveBy(const dx,dy:Integer; const Duration:Float; const OnFinished:TProcedureRef);overload; function fxMoveUp(const Duration:Float):TW3MovableControl;overload; Procedure fxMoveUp(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxMoveDown(const Duration:Float):TW3MovableControl;overload; procedure fxMoveDown(const Duration:Float; const OnFinished:TProcedureRef);overload; function fxSizeTo(const aWidth,aHeight:Integer; const Duration:Float):TW3MovableControl;overload; Procedure fxSizeTo(const aWidth,aHeight:Integer; const Duration:Float; const OnFinished:TProcedureRef);overload; function fxScaleDown(aFactor:Integer; const Duration:Float):TW3MovableControl;overload; procedure fxScaleDown(aFactor:Integer;const Duration:Float; const OnFinished:TProcedureRef);overload; function fxScaleUp(aFactor:Integer; const Duration:Float):TW3MovableControl;overload; Procedure fxScaleUp(aFactor:Integer;const Duration:Float; const OnFinished:TProcedureRef);overload; function fxBusy:Boolean; Procedure fxSetBusy(const aValue:Boolean); End;
This is the magic of partial classes. Without the SmartCL.effects.pas unit, they were just boring old controls. But since TW3MovableControl is marked as partial, the effects unit can extend the class with all that new cool stuff.
Well, that was a crash course in partial classes — hope it clears things up. There is more to it than this ofcourse, but thats an immediate feature you can start using straight away 🙂
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.