不带参数的 F# 类构造函数 --- 使用 filehelpers 和 f# 时出错

发布于 2024-10-17 03:56:24 字数 933 浏览 6 评论 0原文

我点击了使用 F# 和 filehelpers 解析 CSV< 的链接/a>.以下代码出现编译器错误“记录类 oneRow 需要一个没有参数(公共或私有)的构造函数”

[<DelimitedRecord(",")>]
type oneRow=
  class
    [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>]
    val date: DateTime
    val value: bool
  end
let engine = new FileHelperEngine(typeof<oneRow>)
let tmp = engine.ReadFile("test.csv")

编辑 该解决方案看起来比 c# 版本相当冗长。我需要添加 ()mutable[]

  type oneRow() =
      class
        [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>]
        [<DefaultValue>]
        val mutable date: DateTime
        [<DefaultValue>]
        val mutable value: bool
      end

但类似的代码可以在 C# 中使用,无需指定构造函数。谁能帮我修复 F# 代码吗?谢谢。

I followed the link of parse CSV using F# and filehelpers. got compiler error for the following code "The record class oneRow need a constructor with no args (public or private)"

[<DelimitedRecord(",")>]
type oneRow=
  class
    [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>]
    val date: DateTime
    val value: bool
  end
let engine = new FileHelperEngine(typeof<oneRow>)
let tmp = engine.ReadFile("test.csv")

EDIT
The solution looks quite verbose than c# version. I need add (), mutable and [<DefaultValue>]

  type oneRow() =
      class
        [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>]
        [<DefaultValue>]
        val mutable date: DateTime
        [<DefaultValue>]
        val mutable value: bool
      end

But similar code works in C# without specify a constructor. Could anyone help me fix the F# code? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凝望流年 2024-10-24 03:56:24

C# 将为您创建一个构造函数。 F# 没有(大概是因为无参数构造函数意味着可变性,因此并不完全鼓励。)

例如,在您的代码中 - 您将如何设置这些属性,它们仍然是不可变的。

C# will create you a constructor. F# doesn't (presumably because parameterless constructors imply mutability, and so are not exactly encouraged.)

For example, in your code - how are you going to set those properties, they're still immutable.

瞳孔里扚悲伤 2024-10-24 03:56:24

关于详细语法 - 它可以做得更好。该示例是前一段时间(2 年)编写的,因此它仍然使用有点旧的语法。它可以更新为允许编写这样的内容:

[<DelimitedRecord(",")>]
type OneRow
   ( [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>] 
     date:DateTime,
     value:bool ) =
   member x.Date = date
   member x.Value = value

我相信这要好得多(通过将注释移动到构造函数,您还可以在类型中实现您自己的功能,例如隐藏一些字段)。唯一需要做的更改是修改解析器以查找构造函数参数(而不是字段)上的属性。

Regarding the verbose syntax - It can be made nicer. The sample was written some time ago (2 years), so it is still using a little old syntax. It could be updated to allow writing something like this:

[<DelimitedRecord(",")>]
type OneRow
   ( [<FieldConverter(ConverterKind.Date, "M/d/yyyy")>] 
     date:DateTime,
     value:bool ) =
   member x.Date = date
   member x.Value = value

I believe this is a lot nicer (and by moving annotations to the constructor, you can also implement your own functionality in the type and e.g. hide some fields). The only change that needs to be done is to modify the parser to find attributes on constructor parameters (and not fields).

云朵有点甜 2024-10-24 03:56:24

根据错误消息,我认为如果将 ctor 添加到 oneRow 中,它就会起作用。

new () = { date = new DateTime() ; value = false}

Based on the error message, i think it would work if a ctor is added into oneRow.

new () = { date = new DateTime() ; value = false}
独孤求败 2024-10-24 03:56:24

是的,它应该是带有括号的 type oneRow () =

yes, it should be type oneRow () = with parentheses

淡淡绿茶香 2024-10-24 03:56:24

大多数有关 FileHelpers 的帖子都相当过时。在某些情况下,最好使用而不是 csv 类型提供程序。可以在 F# 记录上使用 CLIMutable 属性来拥有默认构造函数,在这种情况下,FileHelpers 将愉快地写入和读取 csv 文件:

#if INTERACTIVE
#I @"..\packages\FileHelpers\lib\net45"
#r "FileHelpers.dll"
#endif

open FileHelpers
open System

[<DelimitedRecord(",");CLIMutable>]
type TestFileHelp =
    {test1:string
     test2:string
     [<FieldConverter(ConverterKind.Date, "yyyy/MM/dd")>]
     date:DateTime
    }

let f1 = [|{test1="a";test2="b";date=DateTime.Now};{test1="c";test2="d";date=DateTime.Now}|]
let fengine = new FileHelperEngine<TestFileHelp>()
fengine.WriteFile(@"c:\tmp\testrec.csv",f1)    

Most of the posts concerning FileHelpers are rather dated. In some cases though it's nice to use instead of the csv type provider. One can use the CLIMutable attribute on an F# record to have a default constructor and in this case FileHelpers will happily write and read the csv file:

#if INTERACTIVE
#I @"..\packages\FileHelpers\lib\net45"
#r "FileHelpers.dll"
#endif

open FileHelpers
open System

[<DelimitedRecord(",");CLIMutable>]
type TestFileHelp =
    {test1:string
     test2:string
     [<FieldConverter(ConverterKind.Date, "yyyy/MM/dd")>]
     date:DateTime
    }

let f1 = [|{test1="a";test2="b";date=DateTime.Now};{test1="c";test2="d";date=DateTime.Now}|]
let fengine = new FileHelperEngine<TestFileHelp>()
fengine.WriteFile(@"c:\tmp\testrec.csv",f1)    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文