小编Pet*_*ner的帖子

如何在F#中实现null-safe运算符

我想知道你是否可以在F#中添加类似"null-safe"-operator的东西.我知道可能有计划在C#中为这样一个运营商提供下一个更大的版本.

如果没有办法实现这样的行为,是否有办法包装一个可能会抛出一个NullReferenceException捕获异常并返回的块的语句null.

我想到了类似的东西:

let sth = %% <@ someobject.method().another().andAnother() @>
Run Code Online (Sandbox Code Playgroud)

其中%%是执行表达式并检查异常的自定义运算符.

我做的一个尝试是:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.QuotationEvaluation

let (~%%) (right:Expr) =
  try 
    right.CompileUntyped()
    right()
  with
  | ex -> null
Run Code Online (Sandbox Code Playgroud)

但这不符合我的要求(事实上它甚至没有编译:)

我通读了:

有没有人有想法如何优雅地为链式方法创建这样的单行try-catch?

f# exception-handling operator-overloading quotations nullreferenceexception

4
推荐指数
1
解决办法
567
查看次数

如何在VS2013中运行FParsec

如何在VS2013专业版中运行FParsec?

我尝试使用以下nuget包:

我试图自己编译https://bitbucket.org/fparsec/main的源代码并使用生成的dll:FParsec.dllFParsecCS.dll.

但无论哪种方式,我总是得到运行教程代码的以下异常:

System.MissingMethodException
Method not found: 
  Microsoft.FSharp.Core.FSharpFunc`2<FParsec.CharStream`1<!!0>,FParsec.Reply`1<Double>> FParsec.CharParsers.pfloat()"
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供逐步解决方案,让谁在VS2013上运行FParsec?

这是我尝试运行的代码:

open FParsec

[<EntryPoint>]
let main args =
  let str s = pstring s // This line works
  let floatBetweenBrackets = str "[" >>. pfloat .>> str "]" // This line breaks
  Console.ReadKey() |> ignore
  0
Run Code Online (Sandbox Code Playgroud)

f# parsing exception fparsec visual-studio-2013

3
推荐指数
1
解决办法
265
查看次数

如何使用 FParsec 解析字符串值

如何从另一个字符串中解析出一个简单的字符串。

在 FParsec 教程中给出了以下代码:

let str s = pstring s
let floatBetweenBrackets = str "[" >>. pfloat .>> str "]"
Run Code Online (Sandbox Code Playgroud)

我不想解析支持之间的浮点数,而是解析表达式中的字符串。

某事。喜欢:

Location := LocationKeyWord path EOL

给定一个辅助函数

let test p s =
    match run p s with
    | Success(result,_,_)  -> printfn "%A" result
    | Failure(message,_,_) -> eprintfn "%A" message
Run Code Online (Sandbox Code Playgroud)

和一个解析器函数:let pLocation = ...

当我打电话时test pLocation "Location /root/somepath"

它应该打印"/root/somepath"

我的第一次尝试是将教程代码修改如下:

let pLocation = str "Location " >>. str
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

Error 244   Typeerror. Expected:
    Parser<'a,'b>    
Given:
    string -> …
Run Code Online (Sandbox Code Playgroud)

f# parsing fparsec

3
推荐指数
1
解决办法
1251
查看次数

是否可以通过F#中的度量单位标记创建区分联合?

是否可以通过F#中的测量单位标签创建区分联合类型?

我想写... 如下:

type DomainObject =
| Pixel           of int
| ScaledPixel     of int
| Centimeter      of float
| Unset

let var1 = 10<px> // should equal: let var1 = Pixel(10)
let var2 = 0<us>  // should equal: let var2 = Unset

let process sth =
  match sth with
  | Pixel(p) -> ...
  | Centimeter(c) -> ...
  // etc.
Run Code Online (Sandbox Code Playgroud)

使用NumericLiterals这样的事情是可能的.但是,人们只能使用Neil P.表示的少量文字.

f# units-of-measurement discriminated-union

3
推荐指数
1
解决办法
203
查看次数