Why do I get “Lexical with name '$x' does not exist in this frame” when using “will leave”? - raku

I have the following Raku code:
class Thing {
method close {
say "closed";
}
};
for 1..1000 {
my $x will leave { .close } = Thing.new;
}
Running it, I get the error:
Lexical with name '$x' does not exist in this frame
in block <unit> at c.raku line 7
Interestingly, this only happens if the number of iterations is high enough (with 500 or 100, I do not get any error messages).
If I change the body of the cycle to
my $x = Thing.new;
LEAVE $x.close;
then everything works without errors as well.
What is happening here? Did I misunderstand the will leave construct? (It seems to me the two versions should be equivalent.)
EDIT: Further observation – when running the code multiple times, the error appears nondeterministically. This suggests that the problem is somehow connected to garbage collection, but I am still confused as to what actually happens here.
I am using Rakudo v2021.03.

This is a bug. Have made an issue for it: https://github.com/rakudo/rakudo/issues/4403
I suggest using the workaround in the meantime.

Related

How can I tell selenide to refesh a page in between retries without raising an error?

I want to test a feature that requires a pageload to be updated. Since selenide only checks if the element has appeared without reloading the page, it will not pick up on the updated state.
I am currently doing it like this:
element(byAttribute("type", "submit")).click()
try {
element(byText("some specific text")).shouldBe(visible)
} catch (e: com.codeborne.selenide.ex.ElementNotFound){
refresh()
element(byText("some specific text")).shouldBe(visible)
}
This is bad in multiple ways:
it catches an error, which is not intended to be caught (otherwise it would have been an exception)
it triggers side effects, like creating error-screenshots
it only retries exactly once
it waits the full first timeout, which may not even be needed. e.g. maybe it could have refreshed immediately then be successful
in case you are wondering: the feature in question is related to a cache being warm or cold. Forcing the cache to be cold at the beginning of the test would probably be possible, but it would also add a lot of complexity (e.g. interacting directly with the database), which I would like to avoid.
I'm not familiar with selenide, but I think you can fix at least several problems you listed with the following changes:
Put the try-catch block inside a while true loop so as if the element found visible break is applied, otherwise continue looping.
In case you want to avoid infinite loop add some counter there so that in case after X iterations the element was not found visible - leave it.
Decrease the timeout for waiting for the expected condition. You did not share here where you define if, so I don't know how to change that.
The first paragraph changes could be something like the following:
element(byAttribute("type", "submit")).click()
for(int i=0;i<100;i++){
try {
element(byText("some specific text")).shouldBe(visible)
break
} catch (e: com.codeborne.selenide.ex.ElementNotFound){
refresh()
element(byText("some specific text")).shouldBe(visible)
}
}

Cannot interpolate trying to emulate EXPORT_OK Tag

I cannot interpolate ::GLOBAL::EXPORT($var)::. What am I doing wrong?
I'm trying to play with exports because they are hard to optimize sometimes.
I try to produce tags for a group of constants.
In reality a few constants are in visible in module scope, but some of them are in a TAG just to separate them and to use them on demand.
But the objective of my question is just to understand why I cannot interpolate what should seem to be very easy.
The error is an exception with new; I've lost the trace for now.
See the code below.
unit module TestImports;
sub bar is export { say "bar sub" }
sub baz is export { say "baz sub" }
my $var = 'He heu hey';
my $aar = 'He heu hey';
my $vaa = 'He heu hey';
my $kar = 'He heu hey';
my $vor = 'He heu hey';
constant A = 333;
constant APPART is export(:AP) = 174;
constant COLOR_A = 24;
constant COLOR_B = 84;
constant COLOR_C = 88;
constant COLOR_D = 92;
constant COLOR_E is export(:SPECIAL)= 144;
constant COLOR_F = 98;
constant COLOR_G = 118;
constant COLOR_H = 214;
constant COLOR_I = 800;
my package EXPORT::MYTAG {
#filter the colors;
my %c = TestImports::.grep(*.key.match(/^ ( COLOR_ <[A..Z]>+ ) $ /));
#filter allready in TAG exported : so we exclude those that we do not want.
my %e = EXPORT::.grep( *.key.grep( none /^ : ( ALL || DEFAULT || MYTAG ) $ /));
#This nearly works but will fail for :SPECIAL Tag because it's yet exported :(
for %c {
next if .key eq 'EXPORT';
.key.say;
OUR::{.key} := .value;
}
#NOTE: So we must exclude, :SPECIAL (and ev. others) building a loop with e.
#IF i did well red the doc ALL SPECIAL should be in GLOBAL::EXPORT::SPECIAL::%(kv);
#and
::GLOBAL::EXPORT::SPECIAL::.raku.say => WORKS; ==> (COLOR_E(80)) SO:
for %e.kv -> $k,$v {
#HERE IS THE QUESTION!!!!!!
::GLOBAL::EXPORT::($k)::.raku.say; #SHOULD WORK but does not: WHY.
}
}
Main script just for testing is this one.
#!/bin/env raku
use TestImports :DEFAULT, :MYTAG :AP ;
#Most of the time : Everything works!but it should work always
say TOTITOTO;
say INTER;
say WANDER;
say WANDER_FULL;
say WANDER3FULL;
say APPART;
say TestImports::A;
#say COLOR_INTER;
bar();
baz();
Thing.new.foo;
This answer is barely an answer, but then your question is barely a question, so perhaps it all balances out. ;)
Cannot interpolate trying to emulate EXPORT_OK Tag
I googled "EXPORT_OK". It's Perl. Simple forms of it seem simple. Why aren't you just sticking with simple tagging?
I cannot interpolate ::GLOBAL::EXPORT($var)::
There's no such package as GLOBAL::EXPORT. The EXPORT generated by Rakudo is a lexical package (declared with my) not a global package (declared with our). Your EXPORT::MYTAG package is explicitly declared with my.
::GLOBAL::EXPORT($var) attempts to call a package as if it were a function which won't work either. But that's presumably just a typo.
At a guess you meant EXPORT::($var) or something like that.
I don't know what you really want because your code doesn't compile and is too long and messy.
I'm trying to play with exports because they are hard to optimize sometimes.
If you mean Perl exports, Raku isn't Perl.
If you mean Raku exports, and mean performance, then please share your --profile results. If you mean some other aspect of optimizing, then please clarify what that is.
In reality a few constants are in visible in module scope, but some of them, are in a TAG just to separate them and to use them on demand.
The constant declarator has an implicit our declarator by default. If you don't want that, put my in front, i.e. my constant ....
But the objective of my question is just to understand wy i cannot interpolate what should seam to be very easy.
If the above hasn't answered your question, please produce a much shorter and cleaner version of your question and then we might be able to help.
The error is an exception with new, i'v lost the trace for now.
The error I see in your deleted "answer" is:
Failure.new(exception => X::NoSuchSymbol.new(symbol => "GLOBAL::EXPORT::SPECIAL"))
That's a Failure value (which is an error value containing an unthrown exception that allows code to continue to run provided nothing tries to use the error value as if it were OK).
It says there's no such symbol (NoSuchSymbol) as GLOBAL::EXPORT::SPECIAL. That's correct, because there is no such symbol/package, as explained near the start of this answer.
I see this in your comments under your deleted "answer":
I think the example is too long
Yes. I'm pretty sure that, whatever it is that you're trying to ask, it should be askable in less than 10 lines of code. See Minimal Reproducible Example for further guidance on how best to ask a question.
But I suspect the primary problem is more basic than that.
If you are experiencing problems with Raku exporting, then it seems likely that you're just using it incorrectly.
If you are using it correctly, then the first thing to do, before thinking about trying to optimize anything, is to use --profile in accord with Knuth's dictum that "Premature optimization is the root of all evil" and share your results.

AutoHotKey Global Variable that can be accessed and modified by different macros?

I've seen a similar topic on sof but its solution did not help me. This is ticking my mind and basically all i want is to have some method of accessing and modifying a value that will maintain its last changed state through out my macros in my single .ahk file.
See example below ,
~Home::Suspend
XButton1::
tog()
return
LButton::
shot()
return
var := "1"
tog(){
var *= -1
}
shot(){
If (var = "1") {
Loop, 1 {
Send {k}
Sleep 65
Send {WheelDown}
Sleep 100
Send {WheelUP}
Sleep 10
}
} Else {
Send {k}
}
}
I am aware that the above is incorrect, and i tried to use"global" in my functions but i just couldn't get my desired effect.
Using the "global" should work. Something like:
shot(){
global var
If (var = "1") {
That points the 'var' variable in the shot() function to the existing 'var' variable defined outside the function.
I had the same issue and after some trial and error I found my mistake which is the same as in the provided code:
The correct way to declare a global is before other functions
var := "1"
XButton1::
; code...
return
The code in the OPs script will hit the return first and never declare the variable
XButton1::
; code...
return ; Returns Here
var := "1" ; Incorrect Will Not Be Declared
I just wanted to provide this as an answer because while I did see this information in one of the comments, I didn't see it until after I'd already spent an additional hour figuring it out myself. As this is the answer I needed, having it as an actual prominent answer may help someone else save time.
What I did, especially since I sometimes have multiple scripts running that need to access the same variable, is to place the var in a .ini file. I also use this to preserve the variable value after a restart. The solution is somewhat slower since the data is saved to the hard disk / SSD, but it works beautifully.
Example of writing the value "S" to variable "State" in group "Finish"
IniWrite, S, C:\2Podcasts\FinishOptions.ini, Finish, State
In an other script (other AutoHotKey instance), I read the value and assign it to the variable "FinishOption".
IniRead, FinishOption, C:\2Podcasts\FinishOptions.ini, Finish, State
If you want to toggle values (True/False), you could use this. This will do an IF on the current value of the variable AND set the variable to the opposite value.
If (MyLoop := !MyLoop) ; Toggle the variable "MyLoop" True/False
{
Do something
}
Else
{
Do something else
}
return

Variable sized arrays in Objective-C?

Okay, so apparently this works:
void foo(size_t s) {
int myArray[s];
// ... use myArray...
}
Is this really legal? I mean, it must be, because it compiles (where the C compiler would reject it as non-constant). The first part of my question is: how does this work? I assume it's allocating it on the stack? Is this different from using alloca()?
Practically, I found some code that does this:
void bar(size_t chunkSize) {
CFReadStreamRef foo = NULL;
// ...some stuff to init foo...
while (stuffToDo) {
UInt8 buffer[chunkSize];
// ...read some data from stream into buffer
// using CFReadStreamRead()...
}
}
This works. However, when I move the buffer allocation from inside the loop to the first line of the function (directly before foo is declared), the function... stops working. In the debugger it gets to the first access of local variables and then just... exits. I don't see any exceptions being thrown, it doesn't crash, it just program carries on running (in reality the function returns a string and that return value is NULL, which is what the return variable is initialized to). I'm not sure what's going on. The second part of my questions is, in light of the first part, what the heck is going on?
it is legal in C99, although dangerous, and yes -- it is like alloca.
because it's like alloca, you want reasonably sized arrays when allocating on the stack. i am not sure if this is defined if the length is zero, but you could definitely cause a stack overflow if the array is 'large enough' to do so.
as far as what is going on -- pulling it out of the loop should make no difference if the sizes are reasonable. i suspect you are seeing undefined behavior because a parameter value is too large (or perhaps 0) -- you should validate the chunkSize parameter. the assembly will tell you why pulling it out of the loop makes a difference (assuming everything else in the program is well-formed).

Understanding what Fault, Error and Failure mean

Consider the following class:
class Xyz {
public int count;
public void numZero (int[] x) {
// Effects: if x == null throw NullPointerException
// else return the number of occurrences of 0 in x
int count = 0;
for (int i = 1; i < x.length; i++) //we have a bug here
{
if (x[i] == 0)
{
count++;
}
}
this.count = count;
}
}
I'm trying to wrap my head about what Fault, Error and Failure really mean.
Fault
From what I've come to understand, a Fault in this context would be a flaw in the code's written logic.
So in this case the Fault would be the fact that the code instructs the computer to start iterating over all elements of v with a start index of 1 instead of the expected 0.
Error
When running the above method, we always get an Error but in once instance (when v.length == 0), as what we really want is to iterate over all elements of x, but since we're starting with i = 1, that is not really happening.
With an empty vector as input, as we don't enter the for loop, so our incorrect code isn't run, meaning that the Error doesn't happen, and everything happens as should in theory.
Failure
Since our code has a Fault that in execution-time will almost always manifest in a Error, we only have a Failure when we effectively see the incorrect output.
Assuming that an Error effectively happened in my program, we only have a Failure if it is in some way visible to the outside world. That is, had I private int count; instead of public int count; I'd never ever have an Error in my class (of course it'd be the most useless class ever!). Is this right?
Is everything I said correct or am I erring in something?
Thanks
Failure: A difference from the expected result. This is the problem
you observe.
Fault: The cause of the failure.
Error: The mistake which caused the fault to occur. e.g, typos.
An example of failure, fault and error.
pre: param is an integer.
post: returns the product of the param multiplied by 2.
1. int double (int param) {
2. int result;
3. result = param * param;
4. return result;
5. }
• A call to double(3) returns 9, but the post condition says it should return 6.
• Result 9 represents a failure.
• The failure is due to the fault at line 3, ( "* param" is used instead of "* 2")
• The error is a typo, ( someone typed "* param" instead of "* 2" by mistake).
Why give three different labels for a "Bug"?
They help communicate how precisely you know what the problem is.
Saying "failure" means you know something is wrong but don't know the cause.
Saying "fault" means you know the cause, but don't know why the fault occurred.
Saying "Error" means you know why the fault occurred; e.g.: The coder was distracted by a firetruck passing by.
You could ask, "But why did the person make a typo?" But that gets into into human factors and out of the scope of the question.
Source: Zhen Ming (Jack) Jiang - EECS 4413, Software Testing, York University.
First, a failure occurs whenever the actual service delivered by a system deviates from its expected service. Note that since even specifications can go wrong, the definition does not rely on them.
Second, an error is the part of the system state that may lead to a failure. The state of the system can go wrong but never reach the output, thus not lead to a failure.
Third, a fault is the cause of an error. It can be a design fault, a cosmic ray or whatever. If, as you point out, the fault is not activated, no error is produced.
Take a look at the basic concepts and terminology of dependability for more information.
Error is a deviation from the actual and the expected result. It represents the mistakes made by the people.
Faults are the result of an error. It is the incorrect step or process due to which the program or the software behaves in an unintended manner
Bug is an evidence of Fault in a program due to which program does not behave in an intended manner
Failure is an inability of the system or components to perform its required function. Failure occurs when Faults executes
Defect is said to be detected when Failure occurs.
There are a plurality of different definitions, the one I personally prefer is the following:
Fault -> Error -> Failure
Fault: The verified or hypothesized cause of an error (malfunctions, external interference, design errors).
Error: The manifestation of a fault within a program or data structure (difference between actual output and expected output).
Failure: The event that occurs when an error reaches the service interface, altering the service itself (leads to the inability of a system or component to perform required function according to its specification).
The Error in Error/Fault/Failure refers to the human error that introduced the problem. The human error was the incorrect thinking that caused the user to create an incorrect for statement in your example.
Errors are hard to measure or understand. It is difficult in many cases to know what the developer was thinking when the made the error that introduced the fault. That is why they like to differentiate between error and fault. We can see that there is a fault in the code, but it is hard to know why the error was created. It could be that the code was correct, and then during a subsequent change, the for loop was changed.
I always remember that an Error by a programmer leads to a fault in the code that results in a failure for the user. Not all errors result in a fault. Not all faults result in failures.
The software Fault refers to a bug in the code. And it is DURING the software activity.
While software Failure is when the system misbehaves. This is observed LATER than a fault.
Fault may be the cause for a Failure. Fault is "WHAT" and Failure is "WHEN".
Those are only fundamentals, but still I hope that it sheds some light on the matter.