Skip to content

Promises that refer to themselves #3

@soegaard

Description

@soegaard

Hi,

I was looking for examples of Julia macros and decided to
look for delay and force macros. The one in promises.jl
looks like this:

    force(p::Promise) -> Any
To 'force' a promise means to run its code.
The results will be saved, which means forcing twice will run only once.
See also: `@delay`
"""

force(p::Promise) = begin
    if p.evaluated
        p.value
    else
        p.value = p.value()
        p.evaluated = true
        p.value
    end
end

There is a potential problem for promises that refer to themselves.
Forcing such a promise could lead to the promise being forced (and the value being set).
In such a case, the value from first evaluation of the promise ought to be used.

Compare with this definition from the R5RS report:

define make-promise
  (lambda (proc)
    (let ((result-ready? #f)
          (result #f))
      (lambda ()
        (if result-ready?
            result
            (let ((x (proc)))
              (if result-ready?
                  result
                  (begin (set! result-ready? #t)
                         (set! result x)
                         result))))))))

Rationale:   A promise may refer to its own value, as in the last example above. Forcing such a promise may cause the promise to be forced a second time before the value of the first force has been computed. This complicates the definition of make-promise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions