In this SML code I'm trying to open a file selected by the user but I keep getting a tycon mismatch error

huangapple go评论49阅读模式
英文:

In this SML code I'm trying to open a file selected by the user but I keep getting a tycon mismatch error

问题

你的代码存在一个类型不匹配(tycon mismatch)的问题。问题发生在 openAndHandleFile 函数中。在这里,你需要处理 TextIO.openIn fileName 的返回值,它返回一个 TextIO.instream option 类型的值。但是你的 readAndPrintLine 函数期望的参数是一个 TextIO.instream 类型。

为了解决这个问题,你可以使用模式匹配来处理 TextIO.openIn fileName 的返回值,并分别处理 NONESOME instream 的情况。这样,你可以根据是否成功打开文件来调用适当的函数。

以下是修改后的代码:

fun openAndHandleFile fileName =
    case TextIO.openIn fileName of
        NONE => print ("Error: Failed to open the file " ^ fileName ^ ".\n")
      | SOME instream => readAndPrintLine instream

fun read_file () = let
    val _ = print "Please enter a filename to be parsed: \n"
    val fileNameOpt = TextIO.inputLine TextIO.stdIn
in
    case fileNameOpt of
        NONE => print "No filename provided.\n"
      | SOME fileName => openAndHandleFile fileName
end

(* 调用 read_file 函数启动程序 *)
val _ = read_file ()

这样修改后的代码应该能够处理 TextIO.openIn fileName 返回的不同情况,解决了类型不匹配的问题。

英文:

I am working on a Standard ML (SML) program that reads a file given by the user and prints the first line of the file. I have written the following code:

fun readAndPrintLine instream =
    case TextIO.inputLine instream of
        NONE => (TextIO.closeIn instream; print "End of file.\n")
      | SOME text => (TextIO.closeIn instream; print ("Read line: " ^ text ^ "\n"))

 (* -->I believe the tycon mismatch is in the openAndHandleFile function here: *)

fun openAndHandleFile fileName =  (*I believe the problem lies in this block of code*)
    case TextIO.openIn fileName of
        NONE => print ("Error: Failed to open the file " ^ fileName ^ ".\n")
      | SOME instream => readAndPrintLine instream

fun read_file () = let
    val _ = print "Please enter a filename to be parsed: \n"
    val fileNameOpt = TextIO.inputLine TextIO.stdIn
in
    case fileNameOpt of
        NONE => print "No filename provided.\n"
      | SOME fileName => openAndHandleFile fileName
end

(* Call the read_file function to start the program *)
val _ = read_file ();

However when I try to run the following code I get the following error:

testing4.sml:7.5-9.51 Error: case object and rules do not agree [tycon mismatch]
  rule domain: TextIO.instream option
  object: TextIO.instream
  in expression:
    (case (TextIO.openIn fileName)
    of NONE => print ("Error: Failed to open the file " ^ fileName ^ ".\n")
     | SOME instream => readAndPrintLine instream)
val it = () : unit

I am new to SML and I know it can be strict about data types, but I'm not sure where the issue is. I think the problem lies in the "openAndHandleFile" function.

What am I doing wrong in my code, and how can I fix the tycon mismatch error?

答案1

得分: 1

问题在于TextIO.openIn不返回一个选项类型,因此你无法匹配SOMENONE构造函数。如果TextIO.openIn失败,它会引发一个Io异常

你需要处理这个异常,以应对文件无法打开的情况。

handle ... => print "未提供文件名。\n"
英文:

The problem is that TextIO.openIn does not return an option type, so you can't match on the SOME or NONE constructors. If TextIO.openIn fails, it instead raises an Io exception.

You would want to handle this exception to address the case where the file cannot be opened.

readAndPrintLine (TextIO.openIn fileName) 
handle ... => print "No filename provided.\n"

huangapple
  • 本文由 发表于 2023年4月7日 02:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定