fpco / inline-c

284 stars 49 forks source link

Pure blocks? #38

Closed mitchellwrosen closed 8 years ago

mitchellwrosen commented 8 years ago

Is there any way to write "pure" blocks? For example,

foo :: CInt
foo = [C.pureBlock| int {
    int out;
    some_func(&out);
    return out;
} |]

Or, as a workaround, I wonder if this is equivalent?

foo :: CInt
foo = unsafePerformIO [C.block| int {
    int out;
    some_func(&out);
    return out;
} |]

EDIT: Ah, I see you expose the internal module. I think I can just define it myself:

pureBlock :: QuasiQuoter
pureBlock = genericQuote Pure (inlineItems Safe)
bitonic commented 8 years ago

Glad you found that yourself, sorry about the delay.

It's a feature that we're currently not leveraging, but I did not kill the code. Note that using genericQuote Pure has some consequences regarding the type which is derived by inline-c. Specifically, for function pointers, if you pass Pure it's going to expect pure Haskell functions.

My position is that using unsafePerformIO explicitly is better because it emphasizes the risks of running C code in a pure fashion, but maybe a module that does that is convenient anyhow...