Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Thu Jul 01, 2010 7:08 pm   

3.21 preserving variable type
 
I can't seem to figure out how to preserve the variable type after clearing the variable

if astringlistvar is a string list or database record for instance and you clear it with any of the normal ways below, it loses its variable type

#var astringlistvar ""
#var astringlistvar {}
astringlistvar = ""
astringlistvar = {}

is this the original desired functionality??

(I noticed this problem after converting most of my scripts to use variable by reference and found out that doing on %numitems on a variable that is supposed to have type string list but doesn't, obviously fails, which is good form to me)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Jul 01, 2010 7:30 pm   
 
Yeah, if I use %null it goes to autotype. Not sure if this is intended functioning either, but it happens to me as well.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Thu Jul 01, 2010 7:34 pm   
 
it seems this is not just a problem when clearing it
when setting a string list variable like:
queryresult = %mapquery (....)

it resets the variable type to "Autotype"
Reply with quote
GeneralStonewall
Magician


Joined: 02 Feb 2004
Posts: 364
Location: USA

PostPosted: Thu Jul 01, 2010 8:13 pm   
 
Pretty sure this is intended.

#var blah "" sets it to a literal
#var blah {} sets it to an expanded
#var blah %null sets it to autotype

Is there any reason you wouldn't want it to be set to autotype?
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Thu Jul 01, 2010 8:31 pm   
 
It doesn't hurt anything, but why would I want it set to auto type? If it's a database variable, I want it to be a database variable. *shrug*
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Thu Jul 01, 2010 8:44 pm   
 
it seems when using variable by reference, the types are strict, or otherwise it will take the variable name as a literal string and not dereference the variable!, that is why
Reply with quote
GeneralStonewall
Magician


Joined: 02 Feb 2004
Posts: 364
Location: USA

PostPosted: Thu Jul 01, 2010 8:57 pm   
 
You should probably writeup a short example.
Reply with quote
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Thu Jul 01, 2010 9:38 pm   
 
as an example:

#additem testlist42 a
#additem testlist42 c
#additem testlist42 b
#additem testlist42 d
#echo %numitems(testlist42)

returns 4, as that is the correct number of items in the list (notice using variable by reference)

Thus far package editor has the correct type of testlist42 as string list -- even if it wasn't set to this type, if the user SETS it to string list, cmud shouldn't change that type automatically by itself if the contents change once the variable is created -- in most programming languages you define a variable a type, and the type doesn't change depending on its contents

however, now that testlist42 is a string list, if we want to reset it to an empty list, there is no way to do that short of #delitem on every single item in the list

then next do:

testlist42 = ""

you should be able to do this to set it to an empty string list, and keep the type as string list, but this doesn't happen, it gets set to type string literal; more importantly since the variable was not originally set to type auto, cmud shouldn't change the variable type no matter what (the user should have to change it in the package editor manually)

this is why it is recently important, when using variable by reference:

#echo %numitems(testlist42)

returns 1, not 0, since it is not dereferencing testlist42 since %numitems expects a string list as the variable type, not a string literal (this type of problem probably also occurs with other functions since I noticed it was strict with the type)

the reference by variable is strict with the types it expects, as it should be, but if the type is not exactly what it expects, it doesn't dereference it and treats it as a string literal

obviously
#echo %numitems(@testlist42)
returns 0, as it should

---- on a separate note...
here is a related problem, possibly a different bug .. trying to workaround doesn't always work (using vartype to set it explicitly to string list)

zztest="h|j|d"
#call %vartype(zztest,4)
#if (%ismember(h,@zztest)) {zztest=%replaceitem(nw,%ismember(h,@zztest),@zztest)}
#call %vartype(zztest,4)
#echo index of j is %ismember(j,@zztest) in zztest: @zztest is of var type %vartype(zztest)

running this script returns:
index of j is 3 in zztest: nw|j|d is of var type stringlist

the index should be 2 not 3!
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Thu Jul 01, 2010 10:08 pm   
 
On the separate note, that's definitely a bug. The problem is with the %replaceitem routine not updating the internal index properly. This should be fixed in the next update.

But back to the original issue: I can't fix this because this is how CMUD has always worked and messing with this has lots of side effects. In general, you should not need to worry about %vartype. If you find yourself needing %vartype, then either you are doing something wrong or there is a bug in CMUD. In the above case, the bug is with %numitems not returning zero for a null list.

You should allow CMUD to handle the vartype. Messing with %vartype can cause un-needed data conversion or copies to be made and can slow down your script.

Functions and commands that need a string list or record type will deal with it automatically and will handle the conversion in an efficient way internally.

A "null" variable really has NO TYPE. It is not a null record or a null string list or a null string...it's just null, and the properly internal type for a null variable is Auto (because the variable is allowed to become anything that it wants).

So, the bottom line: Don't worry about %vartype. If something isn't working, then report it as a possible problem (like the %numitems issue).
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Thu Jul 01, 2010 10:19 pm   
 
Btw, you can see in this example how crazy this backwards compatibility can be, especially with "lazy scripts". Look at your example line:
Code:
#echo %numitems(testlist42)

Well, lots of people forget to put " quotes around string values. Old CMUD and zMUD would treat "testlist42" as a string value and not as a "by-reference" variable name. And when treated as a string value, the proper return of %numitems IS 1.

So when using the above line, you need to be careful. It is fixed in CMUD 3.22 so that if the variable @testlist42 exists, then the value of the variable gets passed to %numitems, and if @testlist42 is null, then %numitems(testlist42) will properly return zero.

But think of the case where the @testlist42 variable does not exist at all yet. The %numitems function is a "readonly" function, which means it will not create the variable if it doesn't exist (like %additem will). If the variable does not exist, then CMUD has no way to tell this is an invalid variable reference and not just a string value without the " quotes.

So you get the odd behavior now in v3.22 that if @testlist42 exists and is null, then %numitems(testlist42) returns zero, but if @testlist42 does not exist at all, then %numitems(testlist42) returns 1.

So another bottom line: Don't use "by-reference" in read-only functions like %numitems, %db, etc. Just go ahead and use the normal @varname syntax just like always. There is no performance decrease for using @list with read-only functions like %numitems.

Remember that the purpose of by-reference syntax was really for functions like %additem to modify a list directly rather than making a copy of it. Let's not get carried away with this feature.
Reply with quote
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Thu Jul 01, 2010 11:40 pm   
 
ahh I see thanks for that much more detailed info, I'll go through it again and revert back for those read only functions

did you look at that %replaceitem problem, that is a separate bug that I don't think has anything to do with variable reference, I just found out that doing %trim(%replaceitem(...)) fixes what I described above, which is very weird
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Fri Jul 02, 2010 12:08 am   
 
Yep, the %replaceitem bug is already fixed for v3.22. It's an internal index problem and I'm not sure how %trim would really help except that maybe it causes the table to be converted to a string and then back to a table again, which recomputes the internal index (and is also slow).
Reply with quote
dbosst
Apprentice


Joined: 15 Jun 2010
Posts: 121

PostPosted: Fri Jul 02, 2010 12:21 am   
 
sweet! you rock! :)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net