Skip to content

Peeking cells example issue #3

Description

@pushedx

Docs reference: https://gutev.dev/live_cells_cpp/md_doc_22-expressions.html

I noticed a few issues with the following example:

auto a = live_cells::variable(0);
auto b = live_cells::variable(1);

auto c = live_cells::computed([=] {
	return a() + live_cells::peek(b);
	});

auto watch = live_cells::watch([=] {
	std::cout << c() << std::endl;
	});

a = 3; // Prints: 4
b = 5; // Doesn't print anything
a = 7; // Prints: 13

The std::cout << c() << std::endl; line generates the following error:

binary '<<': no operator found which takes a right-hand operand of type 'live_cells::make_cell<live_cells::compute_cell_base<_T0,live_cells::constant_cell<T>,live_cells::peek_cell<live_cells::mutable_cell<T>>>>' (or there is no acceptable conversion)

Reading the code docs:

  * The argument cells, referenced within \a compute, are
  * determined dynamically. For this to work, the values of the
  * argument cells must be referenced using the function call
  * operator and not the \p Cell::value() getter method.

I was able to fix the error by instead doing:

auto c = live_cells::computed([=] {
    return a() + live_cells::peek(b)();
});

The last minor thing is that the // Prints: 13 should be // Prints: 12

After making those changes and adding a few extra logging lines, I end up with this -

Example:

auto a = live_cells::variable(0);
auto b = live_cells::variable(1);

auto c = live_cells::computed([=] {
	std::cout << "[computed] c = ";
	auto result = a() + live_cells::peek(b)();
	std::cout << result << " [/computed]" << std::endl;
	return result;
	});

auto watch = live_cells::watch([=] {
	std::cout << "[watch] " << c() << " [/watch]" << std::endl;
	});

std::cout << "The next line should be '4'" << std::endl;
a = 3; // Prints: 4

std::cout << "(No next line)" << std::endl;
b = 5; // Doesn't print anything

std::cout << "The next line should be '12'" << std::endl;
a = 7; // Prints: 12

Output:

[watch] [computed] c = 1 [/computed]
[computed] c = 1 [/computed]
1 [/watch]
The next line should be '4'
[watch] [computed] c = 4 [/computed]
4 [/watch]
(No next line)
The next line should be '12'
[watch] [computed] c = 12 [/computed]
12 [/watch]

Is that correct or should the original error be addressed another way?

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