Smart Mobile Studio have had support for partial classes for quite some time. It’s already an integral part of our run-time library and brings invaluable features to how we extend and work with the base system. But for those new to this concept, a short introduction is in order.
What is a partial class?
A partial class is, essentially, that you divide the implementation of a class into smaller pieces. These pieces can reside in the same unit or across multiple units of your codebase. When the compiler encounters a class marked as partial, it will wait until the compilation is complete before it seals the virtual method table for that class.
That sounds very technical, but it’s really super easy to use and you dont have to get into the nitty-gritty of things to take advantage of the fantastic new abilities it gives you.
You may ponder what on earth is the use for something like this? Well, first of all it allows you a finer grain of control when dealing with multi-platform support. Let’s say you have some code that you need to work on both visual (SmartCL namespace) client applications and server-side (node.js) as well. Naturally SmartCL depends on the document object model (DOM) being present, but under node.js there is no such thing. If you try to use the units prefixed with SmartCL.* under node.js, it will either crash the server or produce some spectacular access violations.
With partial classes you can solve this quite elegantly by putting the base-class in one unit, and then finish the class in separate units: one for browser-based code and the other for node.js based environments.
Extending functionality
But the power of partial classes is not really in organization. The above example could just as easily be solved with some clear-cut compiler defines. Not as elegant, but just as effective. The real power is in how partial classes can extend any class which is defined as partial. Like TW3CustomControl, which is the foundation for all visual controls.
Let’s say you want to add a function for fading out any control. Not just a single control, but all controls (since all visual control inherit from TW3CustomControl, any addition to that class affects all descendants equally). If you take a look at the class (which is found in SmartCL.Components.pas) you will notice that it’s marked as partial.
TW3CustomControl = partial class(TW3MovableControl) .. .. end;
That means that we can extend the class at any time. So if we add the following code to one of our units:
type TW3CustomControl = partial class(TW3MovableControl) public procedure FadeOut; end; procedure TW3CustomControl.FadeOut; begin // Add code here end;
Hit compile so the compiler picks up on this, and voila – the code suggestion will now show the new member FadeOut() for all controls. Every control that inherits from TW3CustomControl gets this new member. That is pretty cool! You could ofcourse do something similar with a class helper (which Smart Mobile Studio also supports), but the difference between a class-helper and a partial class is quite radical.
A class helper is not a true member of a class, it’s more (as the name implies) a helper that operates around an instance. But a partial class is the class itself, with full access to protected and private members. You have just told the compiler that “oh by the way, add this to TW3CustomControl”. Any class defined as partial will not have it’s VMT sealed until the very last moment.
Need to extend that websocket client you find lacking? Well, if it’s marked as partial you can extend the class as you see fit without messing about with the RTL. It’s also a good way to extend the copyrighted RTL with new features that can be sold and distributed as a package. You wont violate anything because you are using partial classes!
Incidentaly this is how Mono C# solved the problem of targeting Android and Apple iOS from the same codebase. We have done more or less the same in our upcoming RTL where we introduce 3 distinct namespaces. So there is a lot of goodies in the bag for x-mas 🙂
Effects
If you add the unit SmartCL.Effects.pas to your project you will notice something very cool! Namely that TW3CustomControl suddenly get’s around 30 new members. These are special effects methods that will help you write some truly amazing controls. Want a button to shake when the user touches it? No problem. Want a panel to fade out, or perhaps slide off to the right? Easy as apple pie. Just open up SmartCL.Effects.pas and have a look. You can even daisy-chain effects to each other (with callbacks and duration settings) and Smart will execute them in sequence (!)
Verdict
I hope this introduction to partial classes was helpful and that it enriches the way you write Smart solutions. Naturally there is more than one system for partial classes on the market. C# has a slightly different take on this technology. But personally and speaking from experience (I have worked quite a lot with Mono C#) I feel our variation on the theme is better. You have to type more in C# to achieve what Smart does in a couple of lines, and the level of control you have is just awesome.
And ofcourse: It makes it that much easier to deal with multi-platform code, which is what our next updates is all about.