我有两个名为SimpleJSON.hs的haskell文件,另一个是Main.hs
--File: SimpleJSON.hs
module SimpleJSON
(
JValue (..)
,getString
,getInt
,getDouble
,getBool
,getObject
,getArray
,isNull
) where
data JValue = JString String
| JNumber Double
| JBool Bool
| JNull
| JObject [(String, JValue)]
| JArray [JValue]
deriving (Eq, Ord, Show)
Run Code Online (Sandbox Code Playgroud)
和
--File: Main.hs
module Main () where
import SimpleJSON
main = print (JObject [("foo", JNumber 1), ("bar", JBool False)])
Run Code Online (Sandbox Code Playgroud)
所以在编译时我正在做
ghc -c SimpleJSON.hs
Run Code Online (Sandbox Code Playgroud)
和
ghc simple Main.hs SimpleJSON.o
Run Code Online (Sandbox Code Playgroud)
然后我得到错误
Main.hs:1:1: error:
The IO action ‘main’ is not exported by module ‘Main’
|
1 | module Main () where
| ^
Run Code Online (Sandbox Code Playgroud)
如何解决此编译错误?
应该
module Main where
Run Code Online (Sandbox Code Playgroud)
要么
module Main (main) where
Run Code Online (Sandbox Code Playgroud)
这个答案专门针对Real World Haskell的读者。这本书似乎使用了旧版本的 Haskell,main无需在导出列表中明确提及。在最新版本中,情况已不再如此。
有两个方面需要提及:
module Main (main) where这样写才能main导出SimpleJSON.hs并在编译时提及它Main.hs,即简单地运行ghc -o simple Main.hs就足够了总而言之,亚马逊页面上对该书的几条评论提到,书中涵盖的某些方面已经过时。因此,如果您正在阅读本书,请务必使用其他来源来了解差异。