类定义上的OCaml错误

时间:2013-09-17 01:01:36

标签: ocaml

我正在定义一个类如下

    class dataframe (csv_filepath:string) =
        object
               (* Load the csv, make it square and print the first line *)
               initializer  
                    let print_first_line csv_filepath =
                        let f = Csv.square (Csv.load csv_filepath) in
                        let top_line = Csv.sub 0 0 1 (Csv.columns f) f in
                        Csv.print_readable top_line;
                        Printf.printf "\n";
        end;;

并收到错误消息

   end;;
   ^^^
   Error: Syntax error

我没有看到任何错误,我完全迷失了,有人知道发生了什么事吗?

1 个答案:

答案 0 :(得分:1)

您必须在初始化程序中编写表达式。你在这里写的是函数定义toplevel短语。正确的初始化程序应该看起来像::

initializer
  let print_first_line csv_filepath =
    let f = Csv.square (Csv.load csv_filepath) in
    let top_line = Csv.sub 0 0 1 (Csv.columns f) f in
    Csv.print_readable top_line;
    Printf.printf "\n"
  in
  (* Use print_first_line you defined *)
  print_first_line csv_filepath

或只是简单地

initializer
  let f = Csv.square (Csv.load csv_filepath) in
  let top_line = Csv.sub 0 0 1 (Csv.columns f) f in
  Csv.print_readable top_line;
  Printf.printf "\n"