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

C callback example on docs causes compile errors #5528

Closed
tomjw64 opened this issue Jan 4, 2018 · 4 comments
Closed

C callback example on docs causes compile errors #5528

tomjw64 opened this issue Jan 4, 2018 · 4 comments

Comments

@tomjw64
Copy link

tomjw64 commented Jan 4, 2018

System:
OS: WSL running Ubuntu 16.04
Crystal Version Info:

Crystal 0.24.1 (2017-12-22)

LLVM: 4.0.0
Default target: x86_64-unknown-linux-gnu

Issue:
Thought I'd try my hand at passing a closure to a C function using the example on the crystal-lang docs as a learning exercise. I ran into a couple issues trying to make the example work.

An exact copy (aside from an @Link I added) of the example code yields the following error:

Error in ticker.cr:7: class variable '@@box' of Ticker is not nilable (it's Box(Proc(Int32, Nil))) so it must have an initializer

  @@box : Box(Int32 ->)

I thought I'd just initialize it as an empty Array to just get past that line, given that there's probably some obvious solution that I missed. I changed lines in the example from:
@@box : Box(Int32 ->) and @@box = boxed_data
to
@@box : Array(Box(Int32 ->)) = [] of Box(Int32 ->) and @@box << boxed_data, respectively. I then get the following error:

Error in ticker.cr:29: instantiating 'Ticker:Module#on_tick()'

Ticker.on_tick do |tick|
       ^~~~~~~

in ticker.cr:16: no overload matches 'Array(Box(Proc(Int32, Nil)))#<<' with type Pointer(Void)
Overloads are:
 - Array(T)#<<(value : T)

    @@box << boxed_data

which sort of makes sense because Box is supposed to give us a Pointer(Void), but why do the types not match?

So it seems that either the syntax or the the types used in the module-level variable are causing problems. Are the docs out of date or have I done something wrong myself? I can't really find any alternative examples of this process.

@straight-shoota
Copy link
Member

It seems like the documentation is just wrong here.

This should be a somewhat working example, only on carc.in it doesn't link: https://carc.in/#/r/3c7j

@tomjw64
Copy link
Author

tomjw64 commented Jan 4, 2018

Even the linking is not the problem, however.
@[Link(ldflags: "#{__DIR__}/ticker.o")] will be sufficient if you have some compiled C, ticker.o, with the correct function declaration. I think it was implied that this exists in the docs, although, it wouldn't hurt to have a full example.
The original issue assumes a correct link statement. I've updated some wording to indicate this.

@tomjw64 tomjw64 changed the title Example callback code on docs causes compile errors Example C callback code on docs causes compile errors Jan 4, 2018
@tomjw64 tomjw64 changed the title Example C callback code on docs causes compile errors C callback example on docs causes compile errors Jan 4, 2018
@tomjw64
Copy link
Author

tomjw64 commented Jan 5, 2018

Ah! The example differs on the official language reference and the "Passing a Proc to a C Function" section in the API Docs. The API Docs for Proc contain a correct example using @@box : Pointer(Void)? thus the language reference should be updated.

@asterite
Copy link
Member

asterite commented Jan 5, 2018

Closed in favor of crystal-lang/crystal-book#186

@asterite asterite closed this as completed Jan 5, 2018
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

No branches or pull requests

3 participants