Function pointers, direct method implementation now in DWScript

The googlecode SVN version of DWS introduces delegates capability:

  • Function pointers are now supported in DWS, the syntax is similar to Delphi, though no distinction is made between standalone functions and methods, ie. as long as the parameter(s) and return types match, the following will work:
    type TMyFunc = function (i : Integer) : String;
    var f : TMyFunc;
    f := IntToStr;
    f := SomeObject.SomeMatchingFunc;
    f := TSomeClass.SomeMatchingClassFunc;
  • Support for direct method implementation from within the class declaration has been added, ie. the following code (which will likely be controversial ;-)) is now accepted:
    type
       TMyClass = class
          Field : Integer;
    
          procedure Add(v : Integer); begin Field += v; end;
    
          function ToString : String; virtual;
          begin
             Result:=IntToStr(Field);
          end;
       end;

    It is intended for short implementations, and only class members which have been declared before the implementation point are accepted (which follows the usual scoping rules).

  • Initial methods for enumerating/browsing the compiled expression tree have been added (GetSubExpr, GetSubExprCount).
  • UTCDateTime has been added to the standard time functions.
  • fixed a bug that affected class functions invoking class functions when invoked from an object instance.
  • misc. fixes, code cleanups and coverage improvements.

4 thoughts on “Function pointers, direct method implementation now in DWScript

  1. Eric, this is great stuff and thanks for your efforts. Would it be possible though to tidy up the links between DWS and (say) SynEdit? I did get an IDE working on one machine a few months ago and would love to integrate your script within an App but I’d like to start with a simple IDE that supports simple breakpoints and watches (which yours does – and I see code completion works too!). It would a great help to have a project in the repo that worked with the latest SynEdit though. Brian.

  2. What about anonymous delegates?
    It’s very common in JavaScript, especially if you want to implement a similar behavior like http://nodejs.org using DWS as the scripting engine.

    This is a nice move, in all cases.

  3. @Brian Frost
    At the moment I’m a bit wary of SynEdit, as it isn’t actively developed anymore, and is thus falling quite a bit behind the curve.
    For use at work I’m currently looking at scintilla & dscintilla, though nothing has been decided yet.

    @A.Bouchez
    The same syntax is planned to accept anonymous delegates, as for implementing it, I’m looking around at what exists, as I’m not too found of the “true” closure approach, given that it’s AFAICT source of more trouble than good in every languages where it’s implemented (it’s a bit like “asm” IME, very powerful, and very easy to get awfully wrong for the overwhelming majority of developers). Eiffel’s more restrictive approach seems a lot more safer f.i.

Comments are closed.