代码之家  ›  专栏  ›  技术社区  ›  George

修改记录参数时出现REPL错误

  •  1
  • George  · 技术社区  · 5 年前

    考虑 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"}
    
    1 回复  |  直到 5 年前
        1
  •  3
  •   Shersh    5 年前

    你的代码有两个问题。

    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
        }