[REPL修改记录参数时出错

问题描述 投票:1回答:1

考虑shell :: String -> CreateProcess。例如:

Prelude System.IO System.Process> shell "pwd"
CreateProcess {cmdspec = ShellCommand "pwd", cwd = Nothing, env = Nothing, std_in = Inherit, std_out = Inherit, std_err = Inherit, close_fds = False, create_group = False, delegate_ctlc = False, detach_console = False, create_new_console = False, new_session = False, child_group = Nothing, child_user = Nothing, use_process_jobs = False}

我正在尝试修改CreateProcess的记录参数,但收到REPL错误:

Prelude System.IO System.Process> shell "pwd" { cwd = "/home" }

<interactive>:7:7: error:
    • Couldn't match type ‘CreateProcess’ with ‘[Char]’
      Expected type: String
        Actual type: CreateProcess
    • In the first argument of ‘shell’, namely ‘"pwd" {cwd = "/home"}’
      In the expression: shell "pwd" {cwd = "/home"}
      In an equation for ‘it’: it = shell "pwd" {cwd = "/home"}

<interactive>:7:21: error:
    • Couldn't match expected type ‘Maybe FilePath’
                  with actual type ‘[Char]’
    • In the ‘cwd’ field of a record
      In the first argument of ‘shell’, namely ‘"pwd" {cwd = "/home"}’
      In the expression: shell "pwd" {cwd = "/home"}
shell haskell record read-eval-print-loop
1个回答
3
投票

您的代码中有两个问题。

  1. [cwd具有类型Maybe String,而不仅仅是String,您需要显式使用Just构造函数。
  2. 默认情况下,{}附加到最接近的参数,在这种情况下,这是"cwd"字符串。但是,您要指定函数调用结果的字段。

这样写代码对我有用:

ghci> (shell "cwd") { cwd = Just "/home" }
CreateProcess 
    { cmdspec = ShellCommand "cwd" 
    , cwd = Just "/home" 
    , env = Nothing
    , std_in = Inherit
    , std_out = Inherit
    , std_err = Inherit
    , close_fds = False
    , create_group = False
    , delegate_ctlc = False
    , detach_console = False
    , create_new_console = False
    , new_session = False
    , child_group = Nothing
    , child_user = Nothing
    , use_process_jobs = False
    } 
© www.soinside.com 2019 - 2024. All rights reserved.