F# Fable-无法获取泛型参数的类型信息,请内联或插入类型解析器
我试图在fable中编写一个通用的json解码函数。它似乎是在FSharp中编译的,但我得到了以下代码的错误消息: [使用Thoth.Json库和Fable.PowerPack中的Fetch库]F# Fable-无法获取泛型参数的类型信息,请内联或插入类型解析器,f#,fable-f#,F#,Fable F#,我试图在fable中编写一个通用的json解码函数。它似乎是在FSharp中编译的,但我得到了以下代码的错误消息: [使用Thoth.Json库和Fable.PowerPack中的Fetch库] let autoDecoder<'a> (json:string) (value:obj) : Result<'a, Thoth.Json.Decode.DecoderError> = let tryDecode = Thoth.Json.Decode.Auto.from
let autoDecoder<'a> (json:string) (value:obj) : Result<'a, Thoth.Json.Decode.DecoderError> =
let tryDecode = Thoth.Json.Decode.Auto.fromString<'a>(json)
let getDecoderError (str:string) : Thoth.Json.Decode.DecoderError = ( "Auto decode Error", Thoth.Json.Decode.FailMessage str)
Result.mapError getDecoderError tryDecode
let自动解码器=
让tryDecode=Thoth.Json.Decode.Auto.fromString函数保持通用性
感谢我是fable的新手,但我无法让它工作,fable编译器不允许在没有指定类型的情况下自动解码-此处失败:
Thoth.Json.Decode.Auto.fromString<'a>(str, true)
Thoth.Json.Decode.Auto.fromString
模型,Cmd.ofPromise getCustomers()LoadedCustomerData获取错误
|已加载的客户数据响应->
匹配
|Ok qdj->{model with gridData=queryDataFromJson qdj;message=“Loaded Customer Data”},Cmd.none
|错误str->{model with message=str},Cmd.none
我想寓言是在告诉你要像这样使用内联
:
let inline autoDecoder<'a> (json:string) (value:obj) : Result<'a, Thoth.Json.Decode.DecoderError> =
let tryDecode = Thoth.Json.Decode.Auto.fromString<'a>(json)
let getDecoderError (str:string) : Thoth.Json.Decode.DecoderError = ( "Auto decode Error", Thoth.Json.Decode.FailMessage str)
Result.mapError getDecoderError tryDecode
谢谢,这是我为寓言而编撰的东西;但函数本身不起作用——json是一个空字符串,我只是使用了另一个“答案”,因为我没有时间去弄清楚原因
type Msg =
| Start
| LoadedCustomerData of Result<QueryDataForJson, string>
..
let getCustomers () = promise {
let! response = Fetch.fetch "http://localhost:5000/spa/api/customers" http.getHeaders
let! text = response.text()
return Thoth.Json.Decode.Auto.fromString<QueryDataForJson>(text, true)
}
..
let update (msg:Msg) (model:Model) =
match msg with
| Start ->
model, Cmd.ofPromise getCustomers () LoadedCustomerData FetchError
| LoadedCustomerData resp ->
match resp with
| Ok qdj -> { model with gridData= queryDataFromJson qdj; message= "Loaded Customer Data" }, Cmd.none
| Error str -> { model with message = str }, Cmd.none
let inline autoDecoder<'a> (json:string) (value:obj) : Result<'a, Thoth.Json.Decode.DecoderError> =
let tryDecode = Thoth.Json.Decode.Auto.fromString<'a>(json)
let getDecoderError (str:string) : Thoth.Json.Decode.DecoderError = ( "Auto decode Error", Thoth.Json.Decode.FailMessage str)
Result.mapError getDecoderError tryDecode
let inline autoDecoder<'a> (json:string) : Result<'a, Thoth.Json.Decode.DecoderError> =
Thoth.Json.Decode.Auto.fromString<'a> json
|> Result.mapError (fun (str:string) -> "Auto decode Error", Thoth.Json.Decode.FailMessage str)