Home > Delphi, JavaScript, Object Pascal, OP4JS, Smart Mobile Studio > Smart Pascal and the Buddha object

Smart Pascal and the Buddha object

I’m not sure how to introduce the full implications of this topic. It greatly depends on your background with curly languages (C#, C++, Java) and your love for all things Delphi, FreePascal, Oxygene and Smart Mobile.

But stick with me, it will be fun!

The buddha object

In traditional object pascal all classes must inherit from TObject right? No matter what you do you just could not escape that fact. Unless you used records, defined functions as fields and mapped the entry points manually. Which we sometimes have to do when talking to old-school C libraries or esoteric kernel code, but that’s another matter. TObject is the mother of all classes, thats the general rule (or was).

But what if someone took the time to completely re-design this from scratch. I mean the whole inheritance system inside the compiler? What if we got rid of TObject as the ultimate ancestor and instead introduced a “self-less” object? Kind of like a Buddha object? One that can become anything?

Live prototypes

Our chief compiler engineer, Eric Grange, is nothing short of a wizard when it comes to these things. So a while back he introduced exactly what I described above. So now we get to do super cool tricks that C#, Java and JavaScript have been doing for a while.

Passing self-less classes as parameters and calling back into it!

Passing self-less classes as parameters and calling back into it!

So what just happened above? Well, when the user clicks a button (w3button5 in this case), we call the method “ProcedureToCall”. But wait! We pass as the parameter a class! A class of no specific type, which requires no initiation (no constructor).

But notice how we set a new value (UnKnown.NewValue) which was not in the original declaration. And after that, we call back into the ghost object and dump the “this” property.

Without getting to technical, “this” means the same as “self”. But since this is an anonymous ghost class, it has no self — so “this” then refers to the current context. Now let’s look at what the output is when we dump the object to console:

obj_dump

Its all there, even the new property

Pretty cool huh? Now let’s look at the code generated for these procedures. I think you will agree it’s both readable and straight forward:

That's almost a 1:1 code generation!

That’s almost a 1:1 code generation!

Mind: Blown!

If you don’t quite get why this is important and powerful, then think about what this does for talking with external libraries, refactoring older code, and most importantly – constructing objects in real-time by code. Once you start playing around with this I think you will agree — it’s the bomb!

Now ponder why W3Button5 has a “self” parameter 😉

Advertisements
  1. sglienke
    April 4, 2016 at 2:57 pm

    So while TypeScript is adding type safety on top of JS you are removing it? 🙂

    • Jon Lennart Aasenden
      April 4, 2016 at 6:09 pm

      That was a very strange take on what is being presented here, i take it some pun is involved?

      SMS has supplied strong typing since day 1, in fact – it has been so strong that we had to use ASM statements to bypass the object pascal layer. Which works, but its neither elegant or practical.

      Records were added to make it more compatible with older delphi code, but these are not suitable for communicating with other JS libraries (since the compiler handles naming to avoid conflicts).

      Hence the “ghost” class was introduced. In Smart Pascal TObject actually inherits from JObject, which is the original JavaScript object. This is actually defined in JS as just: “{}”.

      What the buddha or “anonymous class” brings, is a way to create small classes which should not be inherited from, classes you know has no VMT and which the compiler will not meddle with (names and identifiers).
      Making it the ultimate “bridge” between ordinary javascript libraries, nodeJS libraries and packages etc. etc.

      But you can also use it to create low-cost classes yourself.

      So typing is absolutely there, in all other aspects of SMS. But you can now opt to bypass this and go old-school, low-level (or whatever) when the need arises.

      We are way ahead of TypeScript. In fact, that sneaky little Anders Hejlsberg commented on my OP4JS notes weeks before he released typescript with a “;)” .. so he is following our stuff 😀

  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: