Archive for July 2, 2014

Learning Smart Pascal, a primer

July 2, 2014 7 comments

One of the questions I get the most when it comes to Smart Pascal and Smart Mobile Studio, is “how do I learn the language?”. The short answer to this question is, if you know Delphi or Freepascal, that you already know it.

Linguistically Smart Pascal is more or less identical to ordinary Object Pascal (Delphi and FreePascal). So as a Delphi programmer you can jump straight into it and start coding – as long as you remember the following:

  • JavaScript has no pointers
  • You don’t need to postfix your event declarations with “of object”
  • Interfaces are supported by TObject
  • Threads are written in separate units (web workers)
  • Our run-time library is VJL, Visual JavaScript Library
  • Sets are not supported, but easily emulated
  • Generics is not supported

That really is everything you need to know, taking for granted naturally -that you already know Delphi or Freepascal and are not afraid to write code. I must admit that I did not expect people to be so dependent on the visual designer as they clearly are – but linguistically Smart Pascal is just “Delphi with a twist”. I don’t mean anything negative about that, I was just surprised that so many programmers are like fish out of water when you remove the visual aids. I rarely use the visual designer, except for form layout. Everything else is written in code, class by class, hierarchy by hierarchy.

Once you get used to compiling to JavaScript rather than machine code, you can begin to use the “smart” part of our pascal dialect, namely lambdas, array operations, partial classes, anonymous classes, class variables and much, much more (please see wikipedia article for detailed descriptions of these advances).

The single most powerful difference is probably array operations. Under Delphi you are used to TList, TObjectList and lately, generics (not to mention stacks and queues). Under Smart Pascal arrays are more evolved and replaces these concepts. All arrays, regardless of type, support a wide range of operations. Once you understand how flexible and fast this is – it will change how you write your code profoundly. You might even end up missing these features in Delphi and implement a helper for it (like I have done).

Procedure TForm1.playValues;
  mItems: Array of Variant;
  mItems.add('this is a string');

Check out Array Operations on wikipedia for a list of methods supported by all arrays.

Where to begin

Just like under C# you are expected to write code

Just like under C# you are expected to write code

The reason we did not supply documentation is twofold: when I started the project I designed it to be a Delphi only tool, meaning that you were supposed to simply write ordinary Delphi (hence the link to Delphi Basics in the application). SMS was initially a utility by Delphi programmers for Delphi programmers. Sadly, Embarcadero has made it perfectly clear that they regard us as a threat rather than friends so we had no choice but to abandon VCL emulation and focus on our own, proprietary run-time library.

This attitude still baffles me because it represents a great loss of income and opportunity for Delphi programmers worldwide. Object Pascal as a language is the bread and butter of the Delphi community, and the more people use our language – the greater the interest and investment in Delphi there will be.

But you can probably imagine what our compiler could do if deployed with Delphi. The live HTML5 reporting capabilities alone would revolutionize how we present numbers and interact with online services – and being able to compile to HTML5 would really set Delphi apart from other development platforms. Just imagine Intraweb powered by our compiler technology? Being able to use the same language both natively and in the browser – compiled into lightning fast JavaScript on the spot. That would be a very powerful technology indeed. But sadly this is not something Embarcadero wanted. I have given up on Embarcadero long ago and wont waste any more time regarding this topic.

The second reason is, obviously, JavaScript itself. The reality of the browser is so different and alien compared to the Microsoft Windows API, that a full VCL emulation layer would be fatal to execution time. So while it would be nice to be 100% Delphi compatible both linguistically and architecturally, performance wise it would be suicide. And since Embarcadero made it so clear that our hard work and service to the Delphi community is unwanted (due to the fact that it makes HTML5 builder look like a raving imbecile) it made more sense writing our own from scratch. Smart Mobile Studio is what Delphi for PHP should have been.

A brand new run-time library for a brand new platform, boldly taking Object Pascal where no compiler has gone before.

I am probably the first human being to compile an Object Pascal program and executing it on my Smart TV. Never before has Object Pascal run on a Phillips TV with an embedded browser — and never before have Object Pascal been used to control micro-processors. It was not Embarcadero that delivered this — it was us.

Real code, real work

Get a good book on JavaScript

Get a good book on JavaScript

If you go out and buy Mono C# for mobile development you are expected, just like under Smart Pascal, to study the RTL source-code and familiarize yourself with the units. We have reflected some of the VCL architecture (which really is universal, since that’s how most run-time libraries work), such as TW3CustomControl and TW3Component which are as identical to their Delphi equivalents as JavaScript allows.

You are also expected to tailor custom-controls yourself and adapt the standard controls and classes to your solution. This is how real software is made in all other languages, including Objective C, C# and C++. And while Delphi still retains it’s Visual Basic like, RAD, event driven development interface –Object Pascal is just as capable as C# and C++ for serious development. But the visual design tools quickly go out the window when you want to write rock-solid applications, be they for the mobile platform or not.

So in short: If you know Delphi and are not afraid of writing your own classes, which I really regard as a primer for calling yourself a programmer, then Smart Pascal will be mastered in a couple of days. You can also cut the learning curve even more by getting a book on Smart Pascal.

If you are new to Object Pascal then a quick visit to Delphi Basics will help you learn our language quickly and effectively.

And just like Delphi – we provide a “Demos” folder when you install Smart. This really is the #1 place to find example code. Everything from touch management, scrolling, hardware GPU powered sprites and more is covered here. A wast tome of knowledge ready to be played with and learned from.

If I was to learn Smart Pascal today, I would get the following:

It is not “vital” that you learn JavaScript, but learning about the browser objects, JavaScript libraries and elements is important. But it’s not complex and any middle-level programmer will master the browser quickly. Once you have played around with JavaScript a couple of days, reading about what JavaScript can do and how to do it — then pick up the Smart Book and enjoy.

JavaScript has no classes, no custom types — but Smart Pascal does. It will save you so much time and solve so many common JavaScript problems straight out of the box.

Knowing that you can use Smart Pascal to control hardware, like the new JS based controllers which is making their entry into the embedded marked, hard-core HTML5 mobile applications, games, multimedia and serious business applications — it’s more than worth the couple of days it takes learning the ropes.