我嘗試ifdef在以下使用:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
#ifdef APP_DEBUG
print "test"
#endif
然而結果是:
x.hs:6:3: error: Variable not in scope: (#) :: IO () -> t1 -> t0
|
6 | #ifdef APP_DEBUG
| ^
x.hs:6:4: error:
Variable not in scope: ifdef :: t2 -> (a0 -> IO ()) -> [Char] -> t1
|
6 | #ifdef APP_DEBUG
| ^^^^^
x.hs:6:10: error: Data constructor not in scope: APP_DEBUG
|
6 | #ifdef APP_DEBUG
| ^^^^^^^^^
...
我哪里錯了?
我似乎無法在 ghc 的檔案中找到足夠的資訊:https ://downloads.haskell.org/~ghc/latest/docs/html/users_guide/utils.html?highlight = ifdef
我發現的只是:
#if ?condition?, #ifdef ?name?, #ifndef ?name?, #elif ?condition?, #else, #endif, #error ?message?, #warning ?message? 條件編譯指令不加修改地傳遞給C程式、C 檔案和 C 頭檔案。將它們放在 C 程式中意味著將跳過 Haskell 檔案的適當部分。
uj5u.com熱心網友回復:
指令寫在行首,所以:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
#ifdef APP_DEBUG
print "test"
#endif
C 前處理器在Haskell 編譯器之前運行。因此它不理解 Haskell,例如會洗掉某個代碼片段(在這種情況下,如果APP_DEBUG未定義)。
正如@chi 所說,將哈希 ( #)寫為第一行就足夠了,因此:
{-# LANGUAGE CPP #-}
main :: IO ()
main = do
print "hello"
# ifdef APP_DEBUG
print "test"
# endif
也是可能的。
編譯時,APP_DEBUG可以像這樣設定標志:
ghc -DAPP_DEBUG x.hs
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370608.html
標籤:哈斯克尔
