Search code examples
shellhaskellrecordread-eval-print-loop

REPL error when modifying record arguments


Consider shell :: String -> CreateProcess. For example:

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}

I'm trying to modify the record arguments of CreateProcess, but am getting a REPL error:

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"}

Solution

  • You have two problems in your code.

    1. cwd has type Maybe String, not just String, you need to use the Just constructor explicitly.
    2. By default {} are attached to the closest argument, in this case, this is the "cwd" string. However, you want to specify a field of the function call result.

    Writing your code like this works for me:

    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
        }