Reputation: 10695
I have a function archive_lb_files
, and I want to stub out all but one of the matches using a test function. I am not sure how to correct the stub matches, so I do not get
This expression was expected to have type string -> bool, but here has type unit
errors.
Here is the code snippet, and FileMaint.fm.archive_lb_file lb_dir archive_dir fn
returns a bool.
(* Subroutine to process files we fetch from IC, and then need to be uploaded to Munis. *)
let process_payment_processor_files spool_dir unload_dir paymt_dir report_date =
use fHLog = FileMaint.fm.open_log_file_out (FileMaint.fm.generate_log_file ())
// We'll flesh this in when Stratus is installed, and we know the directory names. cmn 5/26/2017
ignore
let test_print fn =
printfn "%A" fn
let archive_lb_files lb_dir archive_dir report_date =
use fHLog = FileMaint.fm.open_log_file_out(FileMaint.fm.generate_log_file () )
let lb_fnam_template = "*_lockbox.txt"
let dir_list = Directory.GetFiles(lb_dir, lb_fnam_template)
for pfn in dir_list do
let fn = Path.GetFileName(pfn)
match fn with
| "cb_lockbox.txt" -> FileMaint.fm.archive_lb_file lb_dir archive_dir fn
| "ics_lockbox.txt" -> (test_print fn)
| "ic_lockbox.txt" -> test_print fn
| _ -> test_print "does not exist"
In answer to the answers and comments, how does archive_lb_file
return a function?
(* Moves a file to a unique file name but not including a central directory in between a source and destination. *)
let archive_lb_file lb_path archive_path lockbox_file_name copy_only =
use fH = new StreamWriter(base_log_file, true)
let rc =
try
if (0 = String.Compare(copy_only, "copy_only")) then
File.Move((lb_path + lockbox_file_name), archive_path) |> ignore
fH.WriteLine(generate_time_stamp + ": " + lb_path + lockbox_file_name + " moved to " + archive_path)
else
File.Copy((lb_path + lockbox_file_name), archive_path) |> ignore
fH.WriteLine(generate_time_stamp + ": " + lb_path + lockbox_file_name + " copied to " + archive_path)
true
with ex ->
fH.WriteLine(generate_time_stamp + ": " + "An error occrred trying to move or copy " +
lb_path + lockbox_file_name + " to " + archive_path)
printfn "%A" ex
false
rc
Upvotes: 1
Views: 193
Reputation: 12346
All patterns in your match statement must return the same type. Your test_print
returns unit, but your FileMaint.fm.archive_lb_file
returns a function string -> bool
. If you change your method to
let test_print fn =
printfn "%A" fn
true
will fix the error you're getting, even though this looks weird.
Edit:
Since you posted the missing function, the error you're seeing is caused by not passing the copy_only
argument in your call.
match fn with
| "cb_lockbox.txt" -> FileMaint.fm.archive_lb_file lb_dir archive_dir fn true
| "ics_lockbox.txt" -> (test_print fn)
| "ic_lockbox.txt" -> test_print fn
| _ -> test_print "does not exist"
Adding true
will call the function. The reason why you're not getting an error with the call is because of currying functions.
https://fsharpforfunandprofit.com/posts/currying/
Upvotes: 3
Reputation: 80765
All branches of the match expression must have the same type. Judging by the error message you posted, the first branch of your match expression has type string -> bool
, which means that your call to FileMaint.fm.archive_lb_file
is probably missing one argument: it expects another string
, and after it gets one, it promises to return a bool
.
From this, the solution will have two parts:
FileMaint.fm.archive_lb_file
(I can't help you any more with this, because I don't see the definition of that function and therefore don't know what the missing argument is supposed to be).test_print
function return the same type - i.e. bool
. To achieve that, just add a return value:.
let test_print fn =
printfn "%A" fn
true
That said, I suspect that your program actually has a logical error: FileMaint.fm.archive_lb_file
returns a bool, but you're ignoring it. Should you? If the return value doesn't matter to you, should the function return it in the first place?
Upvotes: 3