Re: variable declaration reset

new topic     » goto parent     » topic index » view thread      » older message » newer message
_tom said...

to be duplicated ''i'' number of times that is a waste of energy

is actually duplicated for every loop iteration which causes a "reset"... very inefficient

To be pedantic, I doubt that any language actually duplicates variables, and I'd consider a forced/implicit reset more "somewhat shy of spectacularly helpful" than "inefficient".
One thing I hadn't considered is that in some languages (Julia, Python) variables are defined by being assigned, and some (Java) effectively force assignment, so you simply cannot define an "unassigned empty slot" as it were.

_tom said...

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

Sort of, not p2js compatible, and note that as-is this would play merry havoc with refcounts for non-integer variables:

sequence s = {1,2,2,3,4,4,5}  
for i=1 to length(s) do  
    integer curr = s[i], prev  
    -- reset prev to "unassigned": 
            mov eax,h4 
            mov [prev],eax 
            mov rax,h4 
            mov [prev],rax 
    if i>1 and curr=prev then  
    end if  
    prev = curr  
end for  

Maybe delete(prev) could be made to do that sort of thing(/emit that sort of code) properly, including any needed refcount cleanup.


I think I may have just found a potential solution (for p2js):

-- Phix code 
for i=1 to 7 do 
    integer prev 
end for 


// JavaScript code 
for (let i=1, prev; i<=7; i+=1) { 
Yet to give it a proper spin, but that would give prev the correct scope and behaviour, however it would only work for "for", and such unassigned vars would need to be bunched up at the start, so I might have to go back to the drawing board...
Of course, I've been using pwa/p2js aggressively for about a year and only hit this now, so any neat way to sidestep or trigger some kind of compilation error would not be dismissed.

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


Quick Links

User menu

Not signed in.

Misc Menu