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

The usage example of the Lazy Values feature is confusing #91

Closed
DZakh opened this issue Oct 31, 2020 · 2 comments
Closed

The usage example of the Lazy Values feature is confusing #91

DZakh opened this issue Oct 31, 2020 · 2 comments

Comments

@DZakh
Copy link
Contributor

DZakh commented Oct 31, 2020

I'm new in the ReScript community and haven't written any working code yet. So for now, I'm just covering all the listed language features and I have to admit that I already love it.

But I've found that the usage example of the Lazy Values feature is confusing. The biggest accent in the doc is made on the fact that the value is only computed once. Yeah, that's cool, but then I've remembered about another cool feature called something like block let binding (literally a self-called function) where value is also computed once when it initialized.

And then I had a dilemma then what for..., and realized that the biggest advantage of the Lazy Values feature is the Lazy.force that gives you total control under when you want to compute the value of a variable and either want to do it at all.

So as an example I think it's more appropriate to add something like this:

type payload =
  | NeedFilesWithLogHi
  | NeedFilesWithLogBye
  | NoResult
let getFiles =
  lazy({
    Js.log("Reading dir")
    Node.Fs.readdirSync("./pages")
  })

let data = NeedFilesWithLogHi
switch data {
| NeedFilesWithLogHi => {
  Js.log("Hi")
  getFiles
}
| NeedFilesWithLogBye => {
  Js.log("Bye")
  getFiles
| NoResult =>
  Js.log("Bah.")
}

We kind of define the lazy value in an upper scope, but don't compute it if this is not really necessary.

After that, I'll have a question "why not to use a normal function instead" and I'll gladly listen to your arguments that a lazy value is only computed once, doesn't have a runtime overhead etc-etc. I think at that point it will be more reasonable.

@bobzhang
Copy link
Collaborator

bobzhang commented Nov 2, 2020

The lazy semantics is quite close to function.
Some minor difference:

  • it is evaluated only once
  • you can pattern match on lazy value
    On native, it is more efficient with some special runtime support.

@chenglou
Copy link
Collaborator

chenglou commented Feb 7, 2021

Thanks for both of your feedback. I've restructured the docs a bit.

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