DWScript September 2012 news: FreePascal, RTTI and lambda syntax

Here is the news summary for September 2012, top new items are initial FreePascal support and introduction of lambdas syntax.

Minor language improvements

  • Hexadecimal literals prefixed with ‘0X’ (zero x/X) are now supported.
  • Binary literals prefixed with ‘0B’ (zero b/B) are now supported.
  • Added ClassParent to TObject.
  • Methods can now be marked as “empty“, the compiler then assumes an empty implementation body (same as in Oxygene).

FreePascal Support

Latest SVN now compiles under FreePascal 2.7.1, and a fair amount of the unit tests now pass in Windows.

Impacts on Delphi usage:
  • generic collections in (dwsUtils) were adapted, and may require changes in your code if you were using them, the updated collections are a bit “less” generic, but should run faster.
  • enumerators that were using anonymous methods (“reference to”) have been changed to standard object methods (“procedure of object”), you’ll have to adjust your code if you were using them. On the upside, they’ll should be slightly faster now.
  • code no longer compiles in XE3, whose compiler now gives cryptic internal errors. Code still compiles fine under XE and XE2, so my advice is to wait for XE3 update 1.
Limitations in FreePascal:
  • Delphi and DWScript assume UTF-16 strings, while FreePascal uses UTF-8, with UnicodeString support in FPC being still limited, the code currently relies on UTF-8 in FreePascal + conversions, this means String are currently slow in the script engine, and can be quite glitchy for non-BMP Unicode characters.
  • The asm helpers haven’t been ported to FreePascal yet, so overall performance in the script engine, even outside of strings, is quite lower in FreePascal (about twice slower AFAICT).
  • The code has been tested only with a Win32 target, it likely doesn’t compile or runs for other targets yet.

Hopefully the above limitations will get resolved or worked around in future versions.

RTTI first steps

Initial support for RTTI has been introduced, currently it’s limited to properties and class attributes, and only the low-level storage and interfaces have been defined. If you’re interested, see the unit tests for sample code.

It allows enumerating attributes, getting property types, as well as invoking property getters & setters for simple types.

SmartMobileStudio improvements

  • Introduced Lambda syntax, the lambda expression syntax is similar to C#’s syntax with “lambda” keyword in front to make it less of an operator soup
    DoSomething( lambda (param1, param2) => param1+param2 );

    lambda statements are supported too, when ‘=>’ is not present

    DoSomething(
       lambda (param1, param2)
          DoSomethingElse(param1);
          Result := ComputeSomething(param2);
       end );

    Lambdas are currently capable of compile-time type-inference, ie. they are strongly-typed and can be used as less verbose alternatives to the “anonymous methods” syntax.

  • Fixed a bug when Exit() is used in a constructor
  • Fixed support of ‘?’ in asm (JavaScript) sections

And yes, current lambda syntax differs from Oxygene (which uses just ‘->’ with no prefix), we felt the “=>” operator was more readable and more “standard”, the presence of a “lambda” prefix makes them more prominent, and less prone to operator-soup.
There are other advantages, such as parameter-less lambdas (which are not necessarily constants, since they are effectively closures), and the presence of a keyword, which means support for alternative lambda syntax can be added at future times without affecting backward compatibility. I’ll try to discuss these in a future article.

Lambda support will be supported in the script engine later, when it’ll have transitioned from stack-based to closure-based, ad interim, this feature is currently limited to the JavaScript CodeGen and Smart Mobile Studio.

4 thoughts on “DWScript September 2012 news: FreePascal, RTTI and lambda syntax

  1. Hello,

    I wonder how I can access the methods and properties of a TForm within a script.
    example:

    var
    s: String;

    caption: = ‘test’;
    s: = caption;

    Showmessage (s);

  2. ‘code no longer compiles in XE3, whose compiler now gives cryptic internal errors’

    Ok, that’s kinda scary… Any idea what the story is there?

    Paul

  3. Eraldo :

    I wonder how I can access the methods and properties of a TForm within a script.

    You can use the RTTI connector, cf.
    http://delphitools.info/2011/10/03/tdwsrtticonnector-read-anything-write-anything/

    Paul :

    Ok, that’s kinda scary… Any idea what the story is there?

    You can try it with the SVN version and XE3 trial: I’m getting some cryptic looking internal URWxxx error on the “end.” line of a unit (dwsExprs last time at tested, but it moves around).
    I’ve no idea where it comes from or what causes it, as the error only gives URW followed by a number.
    Also the same code compiles with no issue at all in XE and XE2, so I can only assume it’s a new XE3 compiler bug.

Comments are closed.