Referencing existing globals which may not exist + exceptions - examples #7
btsimonh
started this conversation in
Developer-Tools
Replies: 1 comment 1 reply
-
Avoid Why would you ever want to do this for a global variable? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thanks to @sfromis, @Jason2866 and @s-hadinger for information helping me to remove an existing driver before adding it again.
When coding in Berry, you can paste code into the Berry console, and run it. If such code adds a driver to tasmota, for example, every time you paste, you will get an extra driver. (there will be other circumstances too where you create some global resource).
If, for example, you added your driver by using a berry variable D1, there are two ways we've found to remove the driver:
1/ use global.D1 - global variables exist on the global object, and if referenced via the global object, do not throw a comile time exception, and returns falsy if the variable does not exist at runtime.
i.e. you can detect and remove your driver using:
2/ use of exceptions. - note that a simple reference to D1 will throw a COMPILE TIME exception, which you can't catch in your code.
i.e. this does not work:
to get around this, you can use the compile() function.
i.e. to remove a driver using exceptions:
Using exceptions to control this is not better than using global.D1, but it is a good working illustration of compile().
Beta Was this translation helpful? Give feedback.
All reactions