gpt4 book ai didi

.net - F# 不向函数传递任何内容,获取 null 作为参数值

转载 作者:行者123 更新时间:2023-12-03 04:38:51 26 4
gpt4 key购买 nike

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

我将 None 作为第一个参数传递给函数 some,但当函数执行时,parentNode 的值为 null (我不关心它为 None 打印 null,函数参数值是 null 而不是 None)。我最终在打印函数行上收到空引用错误,因为parentNode 为空。我尝试对参数进行元组并更改顺序,但这没有帮助。我偷偷怀疑这与柯里化(Currying)有关,但我不知所措......

由于公司问题,我必须将真实的 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

最佳答案

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

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

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

let sprintOption v = 
if Option.isNone v then "None" else sprintf "%A" v

关于.net - F# 不向函数传递任何内容,获取 null 作为参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com