[Feature Request - New syntax : Declare local variables with value - https://quality.embarcadero.com/browse/RSP-14867]

[Feature Request - New syntax : Declare local variables with value - https://quality.embarcadero.com/browse/RSP-14867]

Since we can already do this (for global variables):
var
Variable: Type = Value;

And this :
procedure Example;
const
Symbol: Type = Value;
begin
// code
end;

How about this syntax?
procedure Example;
var
Variable: Type = value;
begin
// code
end;

This would only initialize the variables that are specifically assigned a starting value.

When applied to managed types (string, interface), this syntax can also avoid the otherwise automatic cleaning of the stack, since this syntax already assigns a value to these variables. I would expect an initialization each time the function is called.

(In this regard, it's a bit similar to default argument values - https://quality.embarcadero.com/browse/RSP-13289)

Dynamic arrays can already be initialized from declaration since XE7, I think it could get expanded to other types as well :D

Comments

  1. what do you expect ? an initialization each time the function is called or a "static" value ?

    BTW local var can't be initialized (E2195)

    ReplyDelete
  2. Nice idea. Paul TOTH, I'd expect initialization each time the function is entered.

    ReplyDelete
  3. Paul TOTH I would expect an initialization each time the function is called :D

    ReplyDelete
  4. Paul TOTH I actually would expect it to be initialized
    - only once when the variable is not changed afterwards (immutable) plus a hint that it can be changed to const
    - each time when it is changed (mutable)

    Do I expect way too much from a compiler in 2016?

    ReplyDelete
  5. Marco Cantù Please, could it come in the next release? :D

    ReplyDelete
  6. I am a newbie in compiler things, sorry if I'm talking nonsense, in the case of a mutable var, an pre-processor could translate this:
    procedure Example;
    var
    Variable: Type = value;
    begin
    // code
    end;

    in this one:
    procedure Example;
    var
    Variable: Type;
    begin
    Variable := value;
    // code
    end;

    and compile after, is this possible or the compiler could be smarter?

    ReplyDelete
  7. In fact your code will be

    procedure Exemple;
    var
    variable:Type{$IFDEF LOCALINIT}Value{$ENDIF};
    begin
    {$IFNDEF LOCALINIT}variable := Value;{$ENDIF}
    // code
    end;

    useless :D

    ReplyDelete
  8. Horácio Filho To answer that question one would know what happens inside the // code block.
    If Variable is being set or passed to a var/out parameter then the compiler could in fact translate to what you wrote. If he sees that the value is not being changed/passed to var/out then it could hint that it can be changed to const (and/or possibly implicitly doing so).
    But that is all theory as I know as little about compilers as you do - I just know what other languages and compilers are capable of.

    Anyway I think a first step could be to just let the compiler translate the initial value into an explicit assign in the order of variable declarations. So then this feature would be "just" syntax sugar ("mhhhh, sugar" homer simpson voice)

    ReplyDelete
  9. I'd rather have inline variable declaration with initialization while we're at it. Go big or go home style.

    ReplyDelete
  10. Asbjørn Heid While I would like that too that opens an order of magnitudes bigger can of worms though.
    I think that adding local variable initialization the way it can be done for global variables that translates into simple statements executed at first (even if in the prologue of the method it still does the initialization) would be a nice addition and could clean up some code.

    Everything beyond that could lead into religious war (I know many developers just love the verbose explicitness of Delphi/Pascal).

    ReplyDelete
  11. With FPC, in Free Pascal, this concept exists and I am using it.

    Under this link is a reference document:
    http://ftp.icm.edu.pl/packages/fpc/docs-pdf/ref.pdf Section 4.4 is talking about that.

    And just like the persons here who were asking "What would you expect", it's written:

    "This is also true for local initialized variables. Local initialized variables are initialized whenever the routine is called. Any changes that occurred in the previous invocation of the routine will be undone, because they are again initialized."

    He is an example:
    { TMainCommands.cm_LoadFavoriteTabs }
    procedure TMainCommands.cm_LoadFavoriteTabs(const Params: array of string);
    var
    bUseTreeViewMenu: boolean = false;
    bUsePanel: boolean = true;
    p: TPoint = (x:0; y:0);
    iWantedWidth: integer = 0;
    iWantedHeight: integer = 0;
    sMaybeMenuItem: TMenuItem = nil;
    begin
    etc...

    Denis.

    ReplyDelete
  12. Attila Kovacs No need to be snarky. There are still rules being applied what can be used as a value for initialization. If glob_z is a const then there is no problem debugging this. If glob_z is a global variable that could have a different value at the time of hitting this local method then it cannot be used.

    ReplyDelete
  13. Here's a question: what do you guys do for "write-once" variables? That is, a variable whose value you set once and never change again, but read from many times.

    In Delphi that is something like,
    var
    x : Integer;
    begin
    x := Foo();
    // Use x in the rest of the method multiple times

    In C++ you can just have:
    const int x = foo();
    // and use x in the rest of the method

    I like const-ifying things and I would like to be able to const-ify a variable used in this pattern, for compiler errors accidentally writing to it, if nothing else.

    ReplyDelete
  14. David Millington Scala has a nice way for doing that, val means immutable - but that is not a const, its only a write-once, variable and then it has var.

    See http://www.tutorialspoint.com/scala/scala_variables.htm

    Following code declares the Car class including the ctor that takes 2 parameters and a readonly field/property and a writable field/property - think about how much code you have to write in Delphi to achieve that.

    class Car(val Name: String, var Miles: Int)

    ReplyDelete
  15. I was browsing some code made with ADA and it supports var init declarations, they can refer to params too (var x:integer=myparam1)

    ReplyDelete
  16. David Berneda Thanks for the suggestion I have added it to the request entry :D

    ReplyDelete

Post a Comment