Home > Delphi, OP4JS > Learning Smart Pascal, a primer

Learning Smart Pascal, a primer

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;
var
  mItems: Array of Variant;
begin
  mItems.add(1200);
  mItems.add('this is a string');
  mItems.add(12.5);
  mItems.push(19.22);
  writeln(mItems.pop);
end;

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.

Enjoy!

Advertisements
  1. abouchez
    July 28, 2014 at 6:57 pm

    AFAIR sets are supported in SmartPascal.

    You can define sets of enumerates, and use the “in” operator, and “include” or “exclude” pseudo-functions.

    And use of generics is not a huge problem, since you have built-in efficient arrays support, and “class of” variables, able to create a given class instance at runtime, by specifying the class type as parameter – just as powerful as in object pascal.

  2. Bee
    December 1, 2014 at 4:04 am

    Have you ever tried or –at least– heard of Morfik? 🙂

    • Jon Lennart Aasenden
      December 1, 2014 at 7:20 pm

      Yeah but it sucked. It only produces native executables, we compile for JS with OOP — beats morfik hands down

      • Bee
        December 2, 2014 at 3:34 pm

        What? Have you ever really tried Morfik? At least, read their wiki. I’ve tried it. FYI, Morfik splits the program into 2 parts, the server side code, and the client side code. The server side code is compiled into native code, using FreePascal (a bit modified). And the client side code is compiled into advanced javascript code. Get your fact straight!

        • Jon Lennart Aasenden
          December 2, 2014 at 10:11 pm

          EH.. Facts straight? I think you better check out Smart Mobile Studio before you start talking like that. You just said the exact same thing as I did. I did not bother to mention the javascript stuff since morfik has a simple and primitive pattern based emitter – to be frank the result is useless in a production scenario (try processing 2 million live transactions through morfik — lol!). So we seem to have different definitions of what advanced means. Morfik’s codegen doesnt even support a VMT, and the way it deals with inheritance is a joke. And who the hell wants native on the server? nodeJS is where things are happening with JavaScript. You want to scale horizontally and upscale the amount of node’s depending on payload – and you want to do this within a cloud framework.
          Already morfik is off the map because it scales vertically only with NADA support for anything even remotely corporate in mind.

          And I could duplicate the effects library in a day, perhaps even less. So why bother.

          Sorry, but Morfik is not something I even consider, but if you have an in-house server I suppose it can be useful in some rare cases. But for serious fortune 500 requirements it wont even make the scratch list.
          I can pretty much write both the client and the server in SMS, upload both bundles to any nodeJS server (even free hosting), and have my app running for all devices in less than 15 minutes. Add another 30 minutes for native iOS, Android and blueberry builds via Cordova (or just use nodeWebkit for desktop apps).

          SMS is used by huge corporations for high-end, high-payload nodeJS servers dealing with hundreds and thousands of transactions a minute. Not exactly the scenario where you whip out interbase and your local, friendly html website designer.

          For serious development? Sorry, get your facts straight and think a lot bigger. A LOT bigger..

          • Bee
            December 3, 2014 at 6:14 am

            I admit, I didn’t use Morfik for serious development. It’s not because Morfik is bad, but because I have already rolled my own (proprietary) system before Morfik was out. I do have high-end high-payload server apps handling several thousands of transactions per minute. And I build it using native solution, FreePascal compiled code. I didn’t want to waste my mature and well tested system just for Morfik. Fortunately, it was a good decision because Morfik couldn’t stand long in the business.

            You got me curious. Do you really think node.js and SMS can out perform native code? Is SMS really that robust? Do you have any benchmark results to prove that? I plan to jump into mobile software development, especially for front-end. Hopefully using pascal-based solution. Do you think SMS is the right answer?

            Thank you.

  3. Bee
    December 3, 2014 at 6:23 am

    Oh, one more thing… are you interested to make pascal coding app for iOS based on SMS?As I commented on the other post, I’d love to have a pascal coding app for my Apple devices. And I see it as a new promising market for Smart Pascal. What do you think? 🙂

  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: