Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined var when checking if the same var is defined #3103

Open
numberlesstim opened this issue Mar 1, 2024 · 2 comments
Open

Undefined var when checking if the same var is defined #3103

numberlesstim opened this issue Mar 1, 2024 · 2 comments
Milestone

Comments

@numberlesstim
Copy link

numberlesstim commented Mar 1, 2024

Using either the terminal or running as a program:

if not (defined hdg) and not (defined hdg@) lock hdg to 90.

throws an error Undefined Variable Name 'hdg'.

If the code was not run as a program but input through the terminal prompt, checking for defined hdg afterward will result in the same error as above, while defined hdg@ is 'false'.

Interestingly, also after using the terminal prompt for the above code,

set hdg to 1.

print hdg.
print defined hdg.

unset hdg.

print hdg.
print defined hdg.

results in

1
false
1
false

and not

1
true
<error>
false
@nuggreat
Copy link

nuggreat commented Mar 1, 2024

In this line if not (defined hdg) and not (defined hdg@) lock hdg to 90. this (defined hdg@) jumps out at me as violating what should be done with defined as you are not using an identifier at that point but something more complex. Also while the documentation doesn't tell you this from what I remember defined only works well with vars and not functions and as locks are functions you are going to get issues.

As to the strangeness of this

set hdg to 1.

print hdg.
print defined hdg.

unset hdg.

print hdg.
print defined hdg.

after running the erroring command you are trying to set something that is was adjacent to a lock command which often leads to problems even if the lock didn't execute. The advice is to never set something you ever though about locking and never lock something you set for a very very good reason. It can work just fine most of the time but when it breaks does it ever explode.

@numberlesstim
Copy link
Author

"The advice is to never set something you ever though about locking and never lock something you set for a very very good reason".
Thats basically what the purpose of the line if not (defined hdg) and not (defined hdg@) lock hdg to 90 is. If the user has already defined or locked hdg use that, otherwise lock it to 90.
For anyone stumbling over this looking for a workaround, this

set locks to lex("hdg", false).
if not (defined hdg) and not (defined hdg@) locks:hdg on.
if locks:hdg lock hdg to 90.

seems to work just fine (and can be extended for additional var-checks).

@JonnyOThan JonnyOThan added this to the 1.5.0.0 milestone Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants