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

let a = ref(None) should not compile to var a; in a for loop #6647

Closed
aaronz0 opened this issue Feb 24, 2024 · 3 comments
Closed

let a = ref(None) should not compile to var a; in a for loop #6647

aaronz0 opened this issue Feb 24, 2024 · 3 comments

Comments

@aaronz0
Copy link

aaronz0 commented Feb 24, 2024

The following code:

   for i in 1 to 5 {
     let a = ref(None)
     if i == 3 {
       a := Some(3)
     }
     Js.log(a.contents)
   }

gets compiled to:

for(var i = 1; i <= 5; ++i){
  var a;
  if (i === 3) {
    a = 3;
  }
  console.log(a);
}

rescript playground

reason playground

Expected result:
Variable a is re-initialized to undefined in each iteration of the loop.

undefined
undefined
3
undefined
undefined

Actual result:

undefined
undefined
3
3
3

Correct compilation result should be:

for(var i = 1; i <= 5; ++i){
  var a = undefined;
  if (i === 3) {
    a = 3;
  }
  console.log(a);
}
@cristianoc
Copy link
Collaborator

I guess this will indirectly fix the issue?
#6102

@fhammerschmidt
Copy link
Member

As a workaround you can use null from RescriptCore:

for i in 1 to 5 {
  let a = ref(null)
  if i == 3 {
    a := Value(3)
  }
  Js.log(a.contents)
}

@cknitt
Copy link
Member

cknitt commented Aug 25, 2024

Fixed in v12 via #6102.

@cknitt cknitt closed this as completed Aug 25, 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

4 participants