Tutorial: String Variables 3

From GECK

This is the third article in a tutorial series on string variables.


'Let' vs 'Set' Syntax

String vars were introduced to OBSE right before the 'let' and 'if eval' syntax was introduced, so keep in mind that most of the explanations in the obse docs about them are still based on the pre-'let' era. Before you could simply go 'let sv_mystringvar := "I'm a string"' you had to go 'set sv_mystringvar to Sv_Construct "I'm a string"'. With 'let', the Sv_Construct function is entirely unnecessary, and to keep things straightforward I will never mention "set" equivalents to ways of doing things if there is no need for them. Use "let", seriously.


The main reason I'm frowning on the 'set' way is that if you 'set' a string var to another they actually refer to the same string, and changing one of them will automatically change the other one too:

string_var sv_string1
string_var sv_string2
set sv_string1 to sv_construct "I'm string 1"
printc "string1 says '%z'" sv_string1

    --> string1 says 'I'm string 1' 

set sv_string2 to sv_string1
printc "string2 says '%z'" sv_string2

    --> string2 says 'I'm string 1'

set sv_string1 to sv_construct "I'm a different string now"
printc "string1 says '%z'" sv_string1

    --> string1 says 'I'm a different string now'

printc "string2 says '%z'" sv_string2

    --> string2 says 'I'm a different string now'

set sv_string2 to sv_construct "In that case, I'm changing too"
printc "string1 says '%z'" sv_string1

    --> string1 says 'In that case, I'm changing too'

printc "string2 says '%z'" sv_string2

    --> string2 says 'In that case, I'm changing too'

and in order to make sure that didn't happen, you had to do this to make sv_string2 refer to a copy of sv_string1's string, rather than the same one:

set sv_string2 to sv_construct "%z" sv_string1

and that was pretty much the only way of 'cleanly' copying a string var's contents to another.


When you 'let' a string var to another one, however, it will refer to a copy of the first's string rightaway, and if you later change something about either of them, the other one will remain intact:

string_var sv_string1
string_var sv_string2
let sv_string1 := "I'm string 1"
printc "string1 says '%z'" sv_string1

   --> string1 says 'I'm string 1'

let sv_string2 := sv_string1
printc "string2 says '%z'" sv_string2 

   --> string2 says 'I'm string 1'

let sv_string1 := "I'm a different string now"
printc "string1 says '%z'" sv_string1  

   --> string1 says 'I'm a different string now'

printc "string2 says '%z'" sv_string2

   --> string2 says 'I'm string 1'

let sv_string2 := "In that case, I'm changing too"
printc "string1 says '%z'" sv_string1   

   --> string1 says 'I'm a different string now'

printc "string2 says '%z'" sv_string2  

   --> string2 says 'In that case, I'm changing too'

In my view, the 'set' way not only involves more typing, but is more dangerous, in that people may accidentally lose the contents of a string that they didn't mean to overwrite. And let's face it: why have 2 string vars around that'll always be the exact same thing? You're much more likely to want to copy one and then change one of the 2, than to want to have duplicates of the same thing all the time. The same difference applies, btw, if you 'set' or 'let' string vars to the values returned by functions like GetName, increasing the chance of accidental fuck-ups with 'set'.


Tutorial part 4: Destroying String Vars


External Links

Personal tools