JsonProvider"这不是常量表达式或有效的自定义属性值"

Mic*_*cah 9 f# f#-interactive type-providers f#-data

鉴于代码:

#if INTERACTIVE
#r "bin\Debug\FSharp.Data.dll"

#endif

open System
open FSharp.Data
open FSharp.Data.Json

let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }"""

//here is where i get the error
let Schema = JsonProvider<testJson>
Run Code Online (Sandbox Code Playgroud)

最后一行不断给出错误"这不是一个常量表达式或有效的自定义属性值" - 这是什么意思?我怎样才能读到这个JSON?

svi*_*ick 15

该字符串必须标记为常量.为此,请使用[<Literal>]属性.此外,类型提供程序创建一个类型,而不是值,因此您需要使用type而不是let:

open FSharp.Data

[<Literal>]
let testJson = """{ "workingDir":"hello", "exportDir":"hi there", "items":[{ "source":"", "dest":"", "args": {"name":"that"} }] }"""

type Schema = JsonProvider<testJson>
Run Code Online (Sandbox Code Playgroud)