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

block or non-block ? I don't understand #2613

Open
igouy opened this issue Nov 7, 2024 · 1 comment
Open

block or non-block ? I don't understand #2613

igouy opened this issue Nov 7, 2024 · 1 comment

Comments

@igouy
Copy link

igouy commented Nov 7, 2024

example.toit:31:9: error: Argument mismatch for 'List.sort'
Argument '--in-place' was passed with block type, but must be non-block
  items.sort --in-place=: | a b | a[1] < b[1] ? 1 : -1
        ^~~~
Compilation failed

;but here =: seems required;

/opt/tmp/started$ /opt/src/toit/bin/toit.compile -o example example.toit
example.toit:36:38: error: Argument mismatch for 'Map.get'
Method does not take an unnamed block argument, but one was provided
Argument '--if-absent' was passed with non-block type, but must be block
  return (base_counts code.size seq).get code 
                                     ^~~
Compilation failed

By trial and error I figured out how to make it compile but it seems like both situations are the same?

example.txt

@kasperl
Copy link
Member

kasperl commented Nov 7, 2024

So --in-place is a named boolean argument that you set to true and the block itself is an unnamed block argument. So the correct code is equivalent to this:

items.sort --in-place=true (: | a b | a[1] < b[1] ? 1 : -1)

For the --if-absent case, the argument is a named block argument, so you have to pass it a block. Blocks are stack-allocated and governed by special rules so they cannot outlive the surrounding method activation. You can read more about it here: https://docs.toit.io/language/blocks-and-lambdas.

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

No branches or pull requests

2 participants