将entryGetText转换为Int gtk2hs

时间:2016-08-29 07:19:45

标签: haskell io gtk2hs

我正在尝试将entryText转换为字符串,因此我可以将其用于其他功能。

`unsafePerformIO (entryGetText txtMagn)`

在尝试编译此代码时,我收到错误消息:

`Couldn't match type ‘IO a0 -> a0’ with ‘[Char]’
    Expected type: String
      Actual type: IO a0 -> a0
    Probable cause: ‘unsafePerformIO’ is applied to too few arguments
    In the first argument of ‘putStrLn’, namely ‘unsafePerformIO’`

提前致谢

2 个答案:

答案 0 :(得分:3)

这是因为你写了

putStrLn unsafePerformIO (entryGetText txtMagn)

在此,您将unsafePerformIO作为参数传递给putStrLn。你的意思是:

putStrLn (unsafePerformIO (entryGetText txtMagn))

现在到unsafePerformIO。顾名思义,它是不安全,所以你最好清楚地知道你想要完成什么。要安全地从IO中提取您的价值并在以后使用它:

text <- entryGetText txtMagn
putStrLn text

答案 1 :(得分:0)

要获取该值,您必须使用read函数:

val <- getLine
someFunctionWhichNeedsTheValue (read val ::Float) 
相关问题