F#中的其他构造函数 [英] Additional constructors in F#

查看:76
本文介绍了F#中的其他构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在F#中创建一个额外的构造函数,以完成一些额外的工作(即读取基本的csv文件),如下所示:

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

但是出现以下错误:

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

我在做什么错了?

正如Daniel所指出的,F#设计是您有一个主要的构造函数,该构造函数通常采用该类所需的所有参数并执行初始化.其他构造函数可以为参数提供默认值,也可以从其他信息中计算出它们.

在您的情况下,我认为最好的设计是将rows作为构造函数参数传递.然后,您可以添加两个其他的构造函数(一个用于加载文件的构造函数,另一个提供空列表的构造函数).

这使代码更加简单,因为您不必检查是否提供了参数(如Daniel的版本一样).我还做了其他一些简化操作(即在功能上计算width并使用序列理解-如果Sheet不修改数据,您也可以避免使用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 ',') ])

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?

解决方案

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 ',') ])

这篇关于F#中的其他构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆