|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 06, 2009 5:26 pm
[3.03a - 3.04a] %vartype |
Can the %vartype get fixed to accept full variable name? I wanna prompt user for a variable and check it's type first.
Code: |
#PRINT @{//Module/Class/Variable}
#PRINT %vartype({//Module/Class/Variable})
|
While first line works, the second one does not (it returns -1).
Update: to be more specific, I want to access a variable from another package.
Update 2: thread renamed |
|
Last edited by Arde on Fri Mar 06, 2009 9:14 pm; edited 1 time in total |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Mar 06, 2009 5:52 pm |
I'll see what I can do. Shouldn't be hard. But the better syntax would be:
#PRINT %vartype("//Module/Class/Variable")
Nothing wrong with using {}, but {} should only be used as string delimiters when you really want to expand variable or function calls within it. When just using a literal string, it's better style to use " quotes. |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 06, 2009 6:26 pm |
If I would put these 2 lines in an alias with use of double quotes in the %vartype, CMUD will not execute it. Even the first #PRINT statement. Double quotes somehow affects %vartype, because when I test them in my package, all aliases and events have suddenly ceased to work. I was forced to restart CMUD to get it operational again.
|
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Mar 06, 2009 8:09 pm |
Have no idea what could be wrong. Doesn't cause a problem in my testing here. And the %vartype just accepts a normal string argument, so I don't know how that could be messed up.
In any case, 3.04 supports the full syntax, so give it another try there. But if aliases and events stop working, then my guess is there is something else more seriously wrong in your package. |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 06, 2009 8:20 pm |
Cool! Thanks for the quick fix!
|
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 06, 2009 9:15 pm |
Quick test in 3.04a
#PRINT %vartype(//untitled/test/tv) - OK
#PRINT %vartype({//untitled/test/tv}) - OK
#PRINT %vartype("//untitled/test/tv") - Error compiling script: illegal character: |
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Mar 06, 2009 10:02 pm |
Ah, OK, I see the problem. %vartype is actually marked as taking a "VarName" argument instead of a "String" argument. I'll make that change for the next version. I think this was done so that if you did
%vartype(@varname)
it would still return the type of the "varname" variable, rather than expanding the variable and fetching the type of the variable named in @varname. What I might do instead is make sure the VarName argument type can accept a " quoted literal string. |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Mon Mar 09, 2009 9:53 pm |
Zugg wrote: |
I'll make that change for the next version. |
That would be nice because when writing a script I can guess myself what type a variable will have. |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Mon Mar 09, 2009 10:56 pm |
Already fixed for the next version.
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Mon Mar 09, 2009 10:57 pm |
Wow! Thanks a lot, Zugg!!!
|
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Thu Mar 12, 2009 10:40 pm |
Is there any way for %vartype to expand its argument? Or a workaround, maybe? In zScript or in Lua?
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 6:10 am |
Zugg, could you tweak %vartype a bit more? Let it check its argument and if it is a local variable name, then expand it.
|
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Fri Mar 13, 2009 9:36 am |
Local variables can be typed as well and we need %vartype to be able to set them. If you are using them as an indirect pointer to a variable then you will just have to uses a function to expand them.
A thought I have been having is that indirection is being used more often. Perhaps it should get a special character, ^ comes to mind. Doing something like "%vartype($local^,5)" and "#ADDITEM $local^ {a}" could be directly compiled; and looks a little bit cleaner than using another function to expand the local. I am pretty sure it would only need to be added to the VarName arg type, but it would be a bonus if "$local^=6" was doable. Something for a later beta. |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Mar 13, 2009 1:01 pm |
I really don't think $local^ looks better than %string($local). And if it's not uglier, it'll certainly be more opaque to new users.
If you're using indirection, just use %string. |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 1:19 pm |
Ah, I have completely forgotten about %vartype can set vartype as well!.. :(
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 1:38 pm |
Vijilante wrote: |
If you are using them as an indirect pointer to a variable then you will just have to uses a function to expand them.
|
Yes, I store full variable name in the local variable (or it can be an argument in alias\function) and want to check type of that variable. Can you give me an example on how I can expand local variable for use with %vartype function?
Btw, with my original suggestion - if %vartype have only 1 argument specified (i.e. if one want to get vartype) and that argument is a local variable, could it _then_ expand the local variable?
%vartype($localVar, 4) - $localVar is not expanded.
%vartype($localVar) - in this case %vartype could expand the $localVar |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Fri Mar 13, 2009 1:54 pm |
No, because you might also want to know what type the local variable is. It may not seem likely or very useful, but it is concievable if, for instance, the type were set in a complex switch statement to one of several possibilities.
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 2:04 pm |
Yes, that is what I thought about. But you should know what type your variable has, aren't you?
|
|
_________________ My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Mar 13, 2009 4:02 pm |
%vartype(%string($localvar)) will do what you're after, Arde.
|
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Fri Mar 13, 2009 8:34 pm |
Yes, usually you should know what type a local variable is because it gets set within that very script. But there are cases such as the compex #switch I suggested, where a vartype might be set to any of several choices, where you might not know what the type is without running another #switch. So it is concievable (not very likely, but concievable) that you might want to check the vartype of a local variable.
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 8:58 pm |
Fang Xianfu wrote: |
%vartype(%string($localvar)) will do what you're after, Arde. |
It doesn't work, Fang. %vartype takes literal string as a parameter, not expanded string. In your example it will search for "%string($localvar)" variable and, naturally, will return -1. I wish it would be as easy as you suggested, but currently I see only 2 solutions for this:
1) Tweak how %vartype handles its 1st parameter
2) Change its parameter type to expanded string. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Fri Mar 13, 2009 8:59 pm |
Also, by way of an example, you might use a local variable to store the parameter to a function, and then want to check to see what kind of value was passed to the function to do different things to it.
|
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 9:09 pm |
Fang Xianfu wrote: |
Also, by way of an example, you might use a local variable to store the parameter to a function, and then want to check to see what kind of value was passed to the function to do different things to it. |
Heh, that's exactly the thing I'm trying to do! And still can't implement it. Btw, function parameter is already a local variable, you do not need to store it again (AFAIK). Fang, we posted at the same time, hope you haven't missed my reply on your post. :) |
|
Last edited by Arde on Fri Mar 13, 2009 9:27 pm; edited 1 time in total |
|
|
|
Arde Enchanter
Joined: 09 Sep 2007 Posts: 605
|
Posted: Fri Mar 13, 2009 9:22 pm |
Rahab wrote: |
Yes, usually you should know what type a local variable is because it gets set within that very script. But there are cases such as the compex #switch I suggested, where a vartype might be set to any of several choices, where you might not know what the type is without running another #switch. So it is concievable (not very likely, but concievable) that you might want to check the vartype of a local variable. |
Yes, Rahab, I understand you and thought about that myself too. What I wanted to say is that currently an alias
Code: |
#LOCAL $strVarName
#PRINT %vartype($strVarName) |
will print -1 - i.e. CMUD 3.05 doesn't accept local variables as a parameter and it didn't accept them in previous versions (I believe so). Hence, now is the right moment to define some rules about handling local variables as %vartype parameter(s) - it will not affect anyones script. Your example is good, but how you think you'll get it to work? I would like to see your solution! :) |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Mon Mar 16, 2009 5:06 pm |
I think that the following:
Code: |
#local $myvar
#call %vartype($myvar, 4)
#show %vartype($myvar) |
should print 4.
What you seemed to be suggesting is that:
Code: |
#local $myvar
#var newvar
#call %vartype(newvar, 4)
$myvar = "newvar"
#show %vartype($myvar) |
should print 4, which I disagree with. I do agree that there ought be a way of getting a value of 4, perhaps something like: %vartype(@{$myvar}), except that %vartype wasn't set up to deal with the @ sign. I don't have a solution for that, unfortunately. |
|
|
|
|
|