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

Incorrect ReScript snippet and JS output in docs under "Design Decisions" block example #469

Open
s6o opened this issue Jan 2, 2022 · 1 comment

Comments

@s6o
Copy link

s6o commented Jan 2, 2022

Documentation link:
https://rescript-lang.org/docs/manual/latest/let-binding#design-decisions

Current ReScript snippet in doc:

if displayGreeting {
  let message = "Enjoying the docs so far?"
  Js.log(message)
}

respective JS output in doc:

if (displayGreeting) {
  console.log("Enjoying the docs so far?");
}

When the starter project is cloned locally as described the ReScript snippet does not compile:

  1 │ if displayGreeting {
  2 │   let message = "Enjoying the docs so far?"
  3 │   Js.log(message)

  The value displayGreeting can't be found

FAILED: cannot make progress due to previous errors.

A similar result can also be observed in the playground linked from the ReScript snippet

When the ReScript snippet is fixed in the most obvious way the generated JS output does not match the documentation:

let displayGreeting = true
if displayGreeting {
  let message = "Enjoying the docs so far?"
  Js.log(message)
}

the actual JS generated output is (header and exports are omitted):

console.log("Enjoying the docs so far?");

var displayGreeting = true;

To get the if statement in JS output the ReScript snippet could be modified to:

let displayGreeting = ref(true)
if displayGreeting.contents {
  let message = "Enjoying the docs so far?"
  Js.log(message)
}

but the resulting JS output below still does not match the documention:

var displayGreeting = {
  contents: true
};

if (displayGreeting.contents) {
  console.log("Enjoying the docs so far?");
}
@ryyppy
Copy link
Member

ryyppy commented Jan 19, 2022

This example is more or less a symbolic to get the point across.. I am not sure if it would make much sense to add the extra noise.

Regarding the output: Since the boolean would be known during compile time, the compiler applies extra optimizations. For the correct output, you'd need to make displayGreeting an unknown external, or a function parameter etc:

@val external displayGreeting: bool = "displayGreeting"

if displayGreeting {
  let message = "Enjoying the docs so far?"
  Js.log(message)
}

this would compile to:

if (displayGreeting) {
  console.log("Enjoying the docs so far?");
}

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

2 participants