DWScript extended language features

DWScript has supported several extensions to the Delphi language since the beginning. Here are a few you may wish the Delphi compiler supported too (and not just Delphi Prism…):

  • generalized case of, which supports non-ordinal type, for instance you can write
    case myString of
      'hello': PrintLn('Hello!');
      'goodbye': PrintLn('See you!');
    end;
  • variables can be declared in-line anywhere in the script, allowing code like
    for i:=1 to 10 do begin
       var k : Integer;
       ...
    end;
  • variables are always initialized, not just global or managed variables
    procedure MyProc;
    var
       k : Integer;
    begin
       ...k is guaranteed to be zero here (unlike in Delphi)
  • variables can be initialized to custom values upon declaration
    var obj : TMyObject = TMyObject.Create;

9 thoughts on “DWScript extended language features

  1. > generalized case of, which supports non-ordinal type, for instance you can write

    > variables can be declared in-line anywhere in the script, allowing code like

    Delphi got reasons why they don´t support such things.
    Like “case-Strings” cause of performance and most time style fail and again Style and Clean Code of Variable definitions. (Methods shouldn´t be to long)

  2. I disagree, Case-Strings performance can only be better if it is compiler supported than a series of “if then else” ever could, which is the alternative you have, though neither case-of nor if-then-else can be considered efficient (be it on an ordinal type or not, both suffer heavily from branch mispredictions).
    Same goes for the code clarity and length: a single case of is cleaner, both in readability and making known the developer’s intent than a series of if-then-else, and not prone to typos or copy-paste errors like a series of if-then-else.
    The length of methods is not relevant here, if you make a very long method with a single case, it would have been even longer with a series of if-then-else.

    Keep in mind a case-of doesn’t do anything you couldn’t do without an if-then-else, it merely helps clarify the coder’s intents (both for the compiler and the human readers), and it offers a cleaner, less typo-prone way to express it.

  3. The case on any type is very useful, and I’d love to see it in Delphi. It doesn’t make performance any worse than a chain of “else ifs,” and one thing it would vastly improve is debugging. Just hit F8 once and get taken to the right branch immediately, instead of hitting it again and again to walk down a long list of else ifs.

    I also like the automatic initialization of all variables. But declaring variables inline anywhere? Like Mylen said, there’s a very good reason why this isn’t supported in Delphi. Two, actually. One, it makes the compiler slower, and two, it makes the code harder to read since you don’t automatically know where to look to find your variable declarations anymore.

  4. @Mason Wheeler
    Speed-wise, it doesn’t affect the parser or compiler because you can’t use a variable before having declared it, so it’s still one-pass parsing.

    In terms of readability, it allows scoping a local variable to a local scope (rather than having all your locally scoped variables be scoped as “global to the procedure”). Sure, if you have a monster procedure, you could use it to hide declarations in odd places, but a monster procedure is a problem in itself. In practice however, it allows to declare variables closer to the spot where they are necessary, which is often neater IME.

  5. @Eric
    Speed-wise, it doesn’t affect the parser or compiler because you can’t use a variable before having declared it, so it’s still one-pass parsing.

    But the executable … it is no longer just a jump. I think it is a relict … of the past.

    But agree I prefer this too…

    Mike

Comments are closed.