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

clarify note on forcing thunks in the C API #10848

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fricklerhandwerk
Copy link
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
Copy link
Member

@roberth roberth left a comment

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 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
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.

src/libexpr-c/nix_api_expr.h Outdated Show resolved Hide resolved
Co-authored-by: Philipp <philipp.zander@tweag.io>
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
@@ -129,8 +129,7 @@ nix_err nix_value_call_multi(
*
* This function converts these Values into their final type.
*
* @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
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
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
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
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
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
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