Re: variable declaration reset

new topic     » goto parent     » topic index » view thread      » older message » newer message

Phix is working as expected.

JavaScript is working in a plausible, internally consistent, ?


Consider the creation of a variable, which is a two step process.

  • instantiate a type and give it a name integer prev
    • prior to this, any code that used the name ''prev'' crashes Phix
    • just after this moment: ? object(prev) is False.
  • assign a value prev := s[i]
    • after this moment: ? object(prev) is True.
    • since it is a loop, prev is now in-scope before the declaration, next time the loop is performed ... this makes sense, but maybe not obviously

  • observations
    • prev is out-scope after ''end for''
    • it makes no sense (in a compiled program) for ''integer prev'' to be duplicated ''i'' number of times that is a waste of energy
    • the variable is created exactly once in the loop
    • in a traditional interpreter (one-line-at-time design) it is not surprising that ''integer prev'' is actually duplicated for every loop iteration which causes a "reset"... very inefficient
    • I would expect ''integer prev'' to be ignored in a loop after the first encounter
    • once the loop goes past ''one iteration'', then prev is in-scope from the top of the loop ? object(prev) just before the declaration returns True

sidebar

This seems to be more idiomatic for OE/Phix:

for i=2 to length(s) do 
	if s[i-1] == s[i] then 
		? { s[i-1], s[i] } 
	end if 
 
	---- optional 
	integer curr,prev 
	{curr,prev} := { s[i-1], s[i] } 
end for 

question

It would be interesting if a statement existed that would "undo" an assignment and "reset" a variable to an assigned state so ? object(prev) does return False

be well
_tom

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu