Skip to content

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

@DZakh

Description

@DZakh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions