yoy.be "Why-o-Why"

just an idea: var in code for variables of limited scope

2016-02-03 16:18  var-limited-scope  coding delphi  [permalink]

I've been reading around a lot about the new programming languages (Go, Rust, D, WebJavaECMACoffee6asm.ScriptX) and there's ofcourse this thing, but I still do most work for work and distraction in Delphi. So all of a sudden, in a large code block (I know, I know, one is supposed to keep their methods many and small) I thought of something like this:

all_kinds_of_important_code();

var
s:string;
begin
s:=MyObject1.GetSomeString();
ProcessString(s);
StoreStringForFutureReference(s,Now);
end;

more_code_that_makes_the_world_tick(); //perhaps even using 's' but the one declared way above...

In light of the asynchronous programming most languages get pushed upon them, and the need to have 'scope' outliving the usual runtime of the function call, this may have been considered. But purely on the basis of syntax, this could be done. A 'var' token at that point is never valid, so could get used by a parser to initiate a declaration block, followed by a begin/end block, that the compiler would know how to convert into a separate scope (with unwinding, and auto-calling release on interfaces/reference-counted-instances)...

But it currently doesn't. The closest thing you could get is define an anonymous function and call it once there and then, but that may look ugly and is too much code for what it does, in my humble opinion.

So for now I will just be (re-)using a single s:string; on top, I'm afraid. (I feel your sqeam, but I'll be really carefull I promise.)

twitter reddit linkedin facebook