Haskell 预处理器 - 带花括号的井号

Haskell preprocessor - pound sign with curly braces

浏览 Haskell SDL 绑定的源代码时,我偶然发现了以下内容:

#include "SDL/SDl.h"

fromAudioFormat AudioS8 = #{const AUDIO_S8}

#{const AUDIO_S8} 部分的实际作用是什么?乍一看,它从 SDL.h 中提取了一个符号,但我想知道它到底做了什么以及如何构造类似的东西。我对 const 修饰符特别好奇。

这是 hsc2hs 语法。 const 指令表示 "output the literal numeric value of this C expression into the generated .hs file"。 # 只是调用预处理器。大括号是可选的 - 如果省略大括号,hsc2hs 只会读到行尾,所以你可以很容易地写

fromAudioFormat AudioS8 = #const AUDIO_S8

有关特定预处理器指令的更多信息,请参阅 the docs