Skip to content

clarify note on forcing thunks in the C API#10848

Open
fricklerhandwerk wants to merge 3 commits into
NixOS:masterfrom
fricklerhandwerk:fixup-docs
Open

clarify note on forcing thunks in the C API#10848
fricklerhandwerk wants to merge 3 commits into
NixOS:masterfrom
fricklerhandwerk:fixup-docs

Conversation

@fricklerhandwerk

Copy link
Copy Markdown
Contributor

@prednaz follow-up on #10842 (comment)

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions Bot added the c api Nix as a C library with a stable interface label Jun 4, 2024
@edolstra edolstra requested review from roberth and removed request for edolstra June 4, 2024 15:21

@roberth roberth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this isn't correct, and it's not really actionable before other changes. (#10499)

Comment thread src/libexpr-c/nix_api_expr.h Outdated
Comment on lines +132 to +133
* @note This function is mainly needed before calling @ref getters.
* All API calls that return a `Value` force thunks automatically.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a mistake in the design, and it already isn't true anymore that all returned Values are forced.

See #10499 for the reasons why, and #10537 for a real world counterexample.

We'll revise this doc entirely when fixing that issue.

Comment thread src/libexpr-c/nix_api_expr.h Outdated
Co-authored-by: Philipp <philipp.zander@tweag.io>
@fricklerhandwerk fricklerhandwerk requested a review from roberth June 11, 2024 15:00
Comment thread src/libexpr-c/nix_api_expr.h Outdated
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
*
* @note You do not need this function as long as you only use API calls that return a `Value`. This function is mainly
* needed before calling @ref getters.
* @note This function is called before calling `nix_get_type()` (and therefore before most other @ref getters), because lazy evaluation should be transparent to users.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @note This function is called before calling `nix_get_type()` (and therefore before most other @ref getters), because lazy evaluation should be transparent to users.
* @note This function should be called before calling `nix_get_type()` (and therefore before most other @ref getters), because lazy evaluation should be transparent to users.

Otherwise it sounds a bit like we're describing what the bindings do. It is the reader's responsibility.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @note This function is called before calling `nix_get_type()` (and therefore before most other @ref getters), because lazy evaluation should be transparent to users.
* @note You only need to call this function before `nix_get_type()` (and therefore before most other @ref getters), because lazy evaluation should be transparent to users of this API.

How about this then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not necessarily true. Suppose in your application you have a hardcoded Nix function, defined in a string literal, so you know how it behaves. For example:

# passed to nix_expr_eval_from_string

{ foo, bar }:
let
  # fancy application-specific computations are declared here
in
{
  inherit baz;
  n = builtins.length qux;
}

In such a case it's valid to

  1. call the function
  2. retrieve the n Value
  3. nix_value_force n
  4. not check the type, because we know its type statically
  5. call nix_get_int

Obviously a toy example, but this is a useful implementation pattern when you have a situation where a few things related things need to be computed by Nix and the application then reads the values it needs. Unlike individual function calls, you take advantage of shared computations this way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, maybe I'm misunderstanding something big time here. Apparently it's not required to call it beforehand, but sometimes it is? But saying "you should" is not very informative - do you have to or not, what are the considerations exactly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe just delete the note if it's not gonna age well anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought, "you need nix_value_force before any api function if and only if that api function does not return a Value" was a beautifully simple rule. is it wrong? will #10499 make it wrong? cc @roberth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants