5

这真的很奇怪,我担心我刚刚做了一些愚蠢的事情,但我无法弄清楚。

我将作为第一个参数传递None给函数some,但是当函数执行时,值为parentNodenull (我不关心它打印 null for None,函数参数值 IS null not None)。我最终在打印功能行上得到一个空引用错误,因为 parentNode 为空。我试图对 args 进行元组并更改顺序,但这没有帮助。我偷偷怀疑这与咖喱有关,但我不知所措......

对于公司问题,我必须用空字符串替换真实的 url 值,但如果有帮助,它是 xsd 的 url

这是代码:

#light
open System
open System.Xml
open System.Net
open System.Collections.Generic

type StartResult =
    | Parameters of XsdParserParameters
    | Xsd of Xsd

and Xsd(text) =
    let rows = new List<string>()

    member this.Text
        with get() = text

    member this.Rows
        with get() = rows

and XsdParserParameters() =
    let mutable url = ""

    member this.Url
        with get() = url
        and set(value) = url <- value

    member this.Start() =
        try
            use client = new WebClient()
            let xsd = client.DownloadString(this.Url)
            StartResult.Xsd(Xsd(xsd))
        with e ->
            StartResult.Parameters(this)

let processor () =
    let parameters = XsdParserParameters()
    parameters.Url <- ""
    match parameters.Start() with
    | StartResult.Parameters(xpparams) ->
        //some error
        ()
    | StartResult.Xsd(xsd) ->

        let rec some (parentNode : XmlNode option) (node : XmlNode) =
            let a = ()

            for subNode in node.ChildNodes do
                match subNode.LocalName with
                | "complexType" ->
                    xsd.Rows.Add(
                        sprintf
                            "%O~%s~%d~%d~%s~%s~%O" 
                            parentNode 
                            subNode.Value 
                            1 
                            1 
                            (subNode.Attributes.GetNamedItem("name").Value)
                            "" 
                            false)
                    some (Some(subNode)) subNode 
                | "sequence" ->
                    some parentNode subNode 
                | "element" ->
                    xsd.Rows.Add(
                        sprintf 
                            "%O~%s~%d~%d~%s~%s~%O" 
                            parentNode 
                            subNode.Value 
                            1 
                            1 
                            (subNode.Attributes.GetNamedItem("name").Value) 
                            "" 
                            false)
                    some (Some(subNode)) subNode 
                | _ ->
                    ()

        let xdoc = new XmlDocument();
        xdoc.LoadXml(xsd.Text)

        some (None) (xdoc.DocumentElement)

processor()

printfn "Done..."
Console.ReadLine() |> ignore
4

2 回答 2

4

Option<'T>Github 上的源代码)使用[<CompilationRepresentation([CompilationRepresentationFlags.UseNullAsTrueValue)>]导致空值大小写(None在本例中)null在运行时由 表示的属性。

于 2012-05-03T16:12:47.450 回答
4

不幸的是,这是 F# 打印出来的方式None

> sprintf "%O" None;;
val it : string = "<null>"

您可以轻松地为类型编写自定义sprintf函数option,例如:

let sprintOption v = 
    if Option.isNone v then "None" else sprintf "%A" v
于 2012-05-03T16:04:42.477 回答