Skip to content

docs: Update functions chapter for v1#178

Open
mark-koch wants to merge 1 commit into
mainfrom
mk/functions
Open

docs: Update functions chapter for v1#178
mark-koch wants to merge 1 commit into
mainfrom
mk/functions

Conversation

@mark-koch

Copy link
Copy Markdown

Closes #162

@mark-koch
mark-koch requested a review from CalMacCQ as a code owner July 13, 2026 09:23
Here, we tried to assign either ``a_function`` or ``b_function`` to the variable ``f``, but the compiler tells us that these values have different types.
This is because the default type assigned to a function value statically tracks the corresponding definition it came from.
These are the ``def a_function(n: int) -> int`` and ``def b_function(n: int) -> int`` types that showed up in the error message above.
To fix this, we can follow the compiler's suggestion and annotate ``f`` with a ``Function`` type.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The compiler suggestion is implemented in Quantinuum/guppylang#2013 but not released yet, so doesn't show up in the current build :/

def return_callable() -> Callable[[int], bool]:
return is_even

return_callable.check()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The current error message is very bad, see Quantinuum/guppylang#2027

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm a little confused by the return_callable and return_function snippets. Returning the Function type instead of Callable is given as a workaround. However its not obvious to me what this workaround achieves. Type checking fails in both cases with the same error message.

Image

@CalMacCQ CalMacCQ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good :) I'm a tad confused by the return_function and return_callable examples so I've asked for some clarification on that.

Only other suggestion is to cross-link to the API docs for Guppy Function and Callable.

def return_callable() -> Callable[[int], bool]:
return is_even

return_callable.check()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm a little confused by the return_callable and return_function snippets. Returning the Function type instead of Callable is given as a workaround. However its not obvious to me what this workaround achieves. Type checking fails in both cases with the same error message.

Image

return f
def call_a_or_b(flag: bool) -> None:
if flag:
f: Function[[int], int] = a_function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If these type hints were not added would the type check fail? What if the two annotations were different?

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

Successfully merging this pull request may close these issues.

Document function and callable types

2 participants