marcobambini / gravity

Gravity Programming Language
https://gravity-lang.org
MIT License
4.3k stars 227 forks source link

Gravity IO model #127

Open YurySolovyov opened 7 years ago

YurySolovyov commented 7 years ago

I thought I'll create a ticket to discuss various topics about IO model for Gravity.

  1. In async/await ticket @parro-it mentioned that he's working on libuv bindings to Gravity, which is cool, though I wonder if event loop should be build-in rather then come in a form of binding.
  2. I think the main answer that IO model should give is the answer to the question: "how do you get the data into a variable?"
// blocking. Execution won't continue until data is ready.
var data = File.read(path);

// async with callbacks (node, browsers)
File.read(path, func(error, data) {
  // ...
});

// async with Promises (node, browsers)
File.read(path).then(func(data) {
  // ...
});

// async/await
var data = await File.read(path);

// streaming

var data = [];

Stream.read(path).on('data', func(chunk) {
  data.push(chunk);
}).on('end', func() {
  data = data.join('');
});

Of course, these examples are not state-of-the art ones, but short enough to show different models, you are totally welcome to propose any other ideas.

One thing I wanted to discuss is do we really need Promises as underlying pattern for async/await or we can somehow make Fibers better instead so that they support this use-case? Maybe Fiber can save last returned result into a variable that can be accessed later?

var fiber = Fiber.create({
  // or last Fiber.yield(), you get the idea :)
  return File.read(path);
});

var data = await fiber;

I'm not proposing any of these as one and only way of doing things, I just want discussion to get started :)

marcobambini commented 3 years ago

I added a much more simpler File optional class with https://github.com/marcobambini/gravity/commit/ce5c8745a1a5a02fd760c858a22b7f8677c109d9

It can be improved and enhanced but it is a starting point.