我需要将记录添加到我的F#项目中.对于我们使用的C#代码:Log4net或NLog(可能是.Net的两个最流行的日志框架).
在F#代码中使用的最佳选择是什么? 我的意思是,是否有任何特定的日志框架编写用于F#代码?
我使用的项目包含很多从单个基类继承的类.在单元测试中,我需要按类型和数据比较接收结果.
当条件列表包含足够多的不同条件时,我按类型使用匹配比较时编译器抛出OutOfMemoryException.
例如,在编译期间跟随F#代码引发System.OutOfMemoryException(参数错误FS0193)(并且在抛出异常之前编译大约需要30秒)
type IBaseClass() = class end
type IChildClass1 () = inherit IBaseClass ()
type IChildClass2 () = inherit IBaseClass ()
type IChildClass3 () = inherit IBaseClass ()
type IChildClass4 () = inherit IBaseClass ()
type IChildClass5 () = inherit IBaseClass ()
type IChildClass6 () = inherit IBaseClass ()
type IChildClass7 () = inherit IBaseClass ()
type IChildClass8 () = inherit IBaseClass ()
type IChildClass9 () = inherit IBaseClass ()
type IChildClass10 () = inherit …Run Code Online (Sandbox Code Playgroud) 我正在使用字符串,它可能包含代理unicode字符(非BMP,每个字符4个字节).
当我使用" \ Uxxxxxxxxv "格式来指定F#中的代理字符时 - 对于某些字符,它给出的结果与C#的情况不同.例如:
C#:
string s = "\U0001D11E";
bool c = Char.IsSurrogate(s, 0);
Console.WriteLine(String.Format("Length: {0}, is surrogate: {1}", s.Length, c));
Run Code Online (Sandbox Code Playgroud)
得到: Length: 2, is surrogate: True
F#:
let s = "\U0001D11E"
let c = Char.IsSurrogate(s, 0)
printf "Length: %d, is surrogate: %b" s.Length c
Run Code Online (Sandbox Code Playgroud)
得到: Length: 2, is surrogate: false
注意:某些代理字符在F#("\ U0010011","\ U00100011")中有效,但其中一些不起作用.
问:这是F#中的错误吗?如何使用F#处理字符串中允许的代理unicode字符(F#有不同的格式,或者只有使用方式 Char.ConvertFromUtf32 0x1D11E)
更新:
s.ToCharArray()给出F#[| 0xD800; 0xDF41 |]; 对于C#{ 0xD834, 0xDD1E }
我正在为f#Lexer和Parser使用fslex/fsyacc实用程序.如果输入文本的语法不正确,则必须知道它发生的位置.
可以在Lexer中确定不正确的词位(标记),如果使用了错误的符号或单词则抛出异常:
rule token = parse
...
| integer { INT (Int32.Parse(lexeme lexbuf)) }
| "*=" { failwith "Incorrect symbol" }
| eof { EOF }
Run Code Online (Sandbox Code Playgroud)
这个问题更多地与Parser(fsyacc)有关 - 如果输入文本具有正确的令牌并且被Lexer成功地标记化,但是在解析期间发生了错误(例如,错误的令牌顺序或规则中缺少令牌)
我知道如果捕获异常,这会给位置(行和列),解析失败:
try
Parser.start Lexer.token lexbuf
with e ->
let pos = lexbuf.EndPos
let line = pos.Line
let column = pos.Column
let message = e.Message // "parse error"
...
Run Code Online (Sandbox Code Playgroud)
但是有可能(如果是 - 如何做?)也确定AST类,解析失败.
例如,是否可以在我的parser.fsy文件中编写类似于以下内容的内容:
Expression1:
| INT { Int $1 }
...
| _ { failwith "Error with parsing in Expression1"}
Run Code Online (Sandbox Code Playgroud) 我需要在文件路径中添加一些额外的查询信息作为查询参数,以便稍后在文件处理期间解析路径.我虽然System.Uri类可以帮助我,但看起来它没有给我我对本地文件路径的期望.
var fileUri = new Uri("file:///c://a.txt?select=10")
// fileUri.AbsoluteUri = "file:///c://a.txt%3Fselect=10"
// fileUri.Query = ""
var httpUri = new Uri("http://someAddress/a.txt?select=10")
// httpUri.AbsoluteUri = "http://someaddress/a.txt?select=10"
// httpUri.Query = "?select=10"
Run Code Online (Sandbox Code Playgroud)
在"ftp://someAddress/a.txt?select = 10"的情况下 - 查询字符串也为空
我知道System.Uri可能会解析" a.txt?select = 10 "来更正文件名" a.txt%3Fselect = 10 ",但是为什么 - 如何逃避这个?
提前致谢
我有一个函数,可以返回不同的类型,我使用歧视联合.我需要的是将有区别的联合中的一种类型转换为另一种类型.还有一些类型可以转换为所有其他类型(String),但是某些类型只能转换为String(MyCustomType)
为此,我已将成员方法ConvertTo添加到ResultType:
type MyTypes =
| Boolean = 1
| Integer = 2
| Decimal = 3
| Double = 4
| String = 5
| MyCustomType = 6
type ResultType =
| Boolean of bool
| Integer of int
| Decimal of decimal
| Double of double
| String of string
| MyCustomType of MyCustomType
with
member this.ConvertTo(newType: MyTypes) =
match this with
| ResultType.Boolean(value) ->
match newType with …Run Code Online (Sandbox Code Playgroud) 我目前的项目使用AST有40种不同类型(被破坏的联合),并且这种AST中的几种类型具有循环依赖性.类型不是那么大,因此我将它们放在一个文件中,并type ... and ...为相互依赖的类型应用构造.
现在,我正在添加函数以在AST中的每个元素下进行一些计算.因为,有许多函数有几行代码,为了使源代码更清晰,我将这些函数分离到不同的文件中.
在没有循环依赖的情况下也可以,当依赖函数在同一个文件中时也可以使用 - 在这种情况下我可以使用let rec function1 ... and function2 ...构造
但它不适用于我的情况.
我也错误地认为,签名文件可以帮助我,但它们的行为不同于C++ - 它们用于定义函数/类型访问模式(内部/公共),还可以在这里添加函数/类型注释标题...
我看到的唯一可能的解决方案是将所有函数移动到一个文件并使用let rec ... and ... and ... and ... and ...构造
可能有人有不同的想法?
提前致谢.