Home > Delphi, JavaScript, Object Pascal, OP4JS, Smart Mobile Studio > Partial classes in Smart Pascal, how?

Partial classes in Smart Pascal, how?

Fast, binary, pure object pascal

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 🙂

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: