不带参数的 F# 类构造函数 --- 使用 filehelpers 和 f# 时出错
我点击了使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
关于详细语法 - 它可以做得更好。该示例是前一段时间(2 年)编写的,因此它仍然使用有点旧的语法。它可以更新为允许编写这样的内容:
我相信这要好得多(通过将注释移动到构造函数,您还可以在类型中实现您自己的功能,例如隐藏一些字段)。唯一需要做的更改是修改解析器以查找构造函数参数(而不是字段)上的属性。
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:
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).
根据错误消息,我认为如果将 ctor 添加到 oneRow 中,它就会起作用。
Based on the error message, i think it would work if a ctor is added into oneRow.
是的,它应该是带有括号的
type oneRow () =
yes, it should be
type oneRow () =
with parentheses大多数有关 FileHelpers 的帖子都相当过时。在某些情况下,最好使用而不是 csv 类型提供程序。可以在 F# 记录上使用
CLIMutable
属性来拥有默认构造函数,在这种情况下,FileHelpers 将愉快地写入和读取 csv 文件: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: