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

What's the resume function? #47

Closed
zaoqi-unsafe opened this issue Jul 11, 2017 · 4 comments
Closed

What's the resume function? #47

zaoqi-unsafe opened this issue Jul 11, 2017 · 4 comments

Comments

@zaoqi-unsafe
Copy link

What's the resume function?

@Pauan
Copy link

Pauan commented Jul 11, 2017

@zaoqi-unsafe The resume function is like a callback in JavaScript.

Let's say you are using some Node.js APIs in JavaScript:

function foo(cb) {
  fs.readFile("foo", function (err, data) {
    fs.writeFile("bar", data, cb);
  });
}

When the readFile function is finished, it will call the callback with the file contents.

Now consider this Koka code:

fun foo(): asyncx () {
  val data = read-file("foo")
  write-file("bar", data)
}

The Koka compiler will translate the above code into this JavaScript:

function read_file(path, resume) {
  // Do some stuff then call resume
}

function write_file(path, data, resume) {
  // Do some stuff then call resume
}

function foo(resume) {
  read_file("foo", function (data) {
    write_file("bar", data, resume);
  });
}

In other words, Koka automatically added in the callbacks for you.

When you write an effect handler, resume is the callback.

So if read-file never calls resume, then the read-file function will never return.

And if read-file does call resume, then the first argument to resume is the return value for read-file

@zaoqi-unsafe
Copy link
Author

@Pauan

val state = handler(i) {
  return x -> x
  get()    -> resume(i,i)
  set(j)   -> resume(j,())
}

@Pauan
Copy link

Pauan commented Jul 11, 2017

@zaoqi-unsafe Handlers can have extra state (in this case i). When you call resume, the first argument is the new state, and the second argument is the return value.

So in this case i is the current state, and resume(i,i) keeps the current state, and resume(j,()) sets the state to j

@b-studios
Copy link
Member

Thanks for answering @Pauan -- I'll go ahead and close the issue.

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