Additional constructors in F#

左心房为你撑大大i 提交于 2019-12-22 03:43:45

问题


I am trying to create an additional constructor in F# that does some extra work (i.e. reads a basic csv file) as follows:

type Sheet () =
  let rows = new ResizeArray<ResizeArray<String>>()
  let mutable width = 0

  new(fileName) as this = 
    Sheet() 
    then
      let lines = System.IO.File.ReadLines fileName
      for line in lines do
        let cells = line.Split ','
        rows.Add(new ResizeArray<String> (cells)) //line 16
        if cells.Length > width then width <- cells.Length

but I get the following errors:

Error   1   The namespace or module 'rows' is not defined   C:\Users\ga1009\Documents\PhD\cpp\pmi\fsharp\pmi\Csv.fs 16
Error   2   The value or constructor 'width' is not defined C:\Users\ga1009\Documents\PhD\cpp\pmi\fsharp\pmi\Csv.fs 17
Error   3   The value or constructor 'width' is not defined C:\Users\ga1009\Documents\PhD\cpp\pmi\fsharp\pmi\Csv.fs 17

What am I doing wrong?


回答1:


As Daniel pointed out, the F# design is that you have one main constructor that typically takes all the arguments needed by the class and performs the initialization. Other constructors can either provide default values for the arguments or can calculate them from some other information.

In your case, I think that the best design would be to pass rows as constructor argument. Then you can add two additional constructors (one that loads a file and other that provides empty list).

This makes the code a bit simpler, because you do not have to check if the argument is supplied (as in Daniel's version). I also did a few other simplifications (i.e. calculate width functionally and use sequence comprehensions - if Sheet does not modify the data, you could also avoid using ResizeArray):

type Sheet private (rows) =  
  // The main constructor is 'private' and so users do not see it,
  // it takes columns and calculates the maximal column length
  let width = rows |> Seq.map Seq.length |> Seq.fold max 0

  // The default constructor calls the main one with empty ResizeArray
  new() = Sheet(ResizeArray<_>())

  // An alternative constructor loads data from the file
  new(fileName:string) =
    let lines = System.IO.File.ReadLines fileName 
    Sheet(ResizeArray<_> [ for line in lines -> ResizeArray<_> (line.Split ',') ])



回答2:


rows and width are not in scope. You can make members to get/set them, or (my recommendation) make the constructor with the most args the primary:

type Sheet (fileName) =
  let rows = new ResizeArray<ResizeArray<string>>()
  let mutable width = 0

  do
    match fileName with 
    | null | "" -> ()
    | _ ->
      let lines = System.IO.File.ReadLines fileName
      for line in lines do
        let cells = line.Split ','
        rows.Add(new ResizeArray<string> (cells)) //line 16
        if cells.Length > width then width <- cells.Length

  new() = Sheet("")

Generally, secondary constructors are intended to be overloads of the primary constructor, so they're prevented from interacting with class internals (fields). This encourages a single path of initialization (and better design).



来源:https://stackoverflow.com/questions/12056573/additional-constructors-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!