Re: problems with the scoping in eu4

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

Concerning variables declaration I repeat my suggestion to comeback to version 3.x situation.
The actual situation create inconsistancies and confusion without bringing any benefit.

I'm lost in this thread, what exactly are you speaking of that was added that does not provide any benefit? Are you speaking of:

sequence names = { "John Doe", "Jim Doe", "Jack Doe", "Joe Doe" } 
 
for i = 1 to length(names) do 
    sequence name = names[i] 
    sequence firstName = get_first_name(name) 
    sequence lastName = get_last_name(name) 
 
    if equal(lastName, "Doe") then 
        printf(1, "CAUTION: Last name is really not known\n") 
    end if 
 
    printf(1, "Dear Mr. %s,\n\nThank you for your suggestion %s!", { lastName, firstName }) 
end for 

i.e. declaring variables inside of a loop and those variables not persisting? If so (I think that is what we are talking about), that feature brings huge benefit into clean function design. I really hated not being able to do it 3.1. Being able to is a big step ahead for Euphoria. It would be a terrible thing if this functionality were to be reverted.

If you want a variable to persist, then you declare it in the scope it should persist in. For example:

sequence names = { "John Doe", "Jim Doe", "Jack Doe", "Joe Doe" } 
sequence lastNameProcessed = "None" 
 
for i = 1 to length(names) do 
    sequence name = names[i] 
    sequence firstName = get_first_name(name) 
    sequence lastName = get_last_name(name) 
 
    if equal(lastName, "Doe") then 
        printf(1, "CAUTION: Last name is really not known\n") 
    end if 
 
    printf(1, "Dear Mr. %s,\n\nThank you for your suggestion %s!", { lastName, firstName }) 
 
    lastNameProcessed = named 
end for 
 
printf(1, "Sent %d messages, last name processed was %s\n", { length(names), lastNameProcessed }) 

This is clean design and I'd suggest that everyone begin programming by keeping the variable to the minimum scope necessary.

Jeremy

I don't think that the complaint is persistence outside of the loop. After all, that doesn't even make sense.

I think that the complaint is persistence from iteration to iteration. The variable is either unassigned, or it is reassigned some default value on every new iteration of the loop.

The simple example initially given was a good one, trying to use two counters in one loop.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu