Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active October 22, 2018 17:49
Show Gist options
  • Save Xliff/f02820e36f198610f8d6a9b0511a4272 to your computer and use it in GitHub Desktop.
Save Xliff/f02820e36f198610f8d6a9b0511a4272 to your computer and use it in GitHub Desktop.

I have this function:

method set_sort_func (
  &sort_func (GtkListBoxRow $a, GtkListBoxRow $b, gpointer $data),
  gpointer $user_data = gpointer,
  GDestroyNotify $destroy = GDestroyNotify
)

And I am calling it like this:

$listbox.set_sort_func(-> $a, $b {
  %messages{$a}<data><time> <=> %messages{$b}<data><time>
});

But I'm getting the following error message:

Cannot unpack or Capture `&sort_func`.
To create a Capture, add parentheses: \(...)
If unpacking in a signature, perhaps you needlessly used parentheses? -> ($x) {} vs. -> $x {}
or missed `:` in signature unpacking? -> &c:(Int) {}
  in method set_sort_func at /home/cbwood/Projects/p6-GtkPlus/lib/GTK/ListBox.pm6 (GTK::ListBox) line 259
  in block <unit> at t/41-listbox.t line 151

I've tried many variations of the above, including changing how the call was made, how the sub was declared:

VARIANT #1

Defining a sub and then passing it off to the routine:

Parameter:

&sort_func:(GtkListBoxRow $a, GtkListBoxRow $b, gpointer $data)"

Sub definition:

sub sort-func($a, $b) {
  %messages{$a}<data><time> <=> %messages{$b}<data><time>
};

Invocation:

$listbox.set_sort_func(&sort-func);

Error message:

Constraint type check failed in binding to parameter '&sort_func'; expected anonymous constraint to be met but got Sub (sub sort-func ($a, $b...)

I've tried other ways, but I always keep get something similar to the above error message. What is the best way to pass a Perl func off to subroutine that wrapps the C call?

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