寓言将字符串转换为枚举而无需匹配或查找

Fable convert string to enum without match or lookup

在 Fsharp 中,您可以将字符串转换为枚举 link 如下:

type Langs = 
    | En = 0
    | Afr = 1

let tryLang str =
    try 
        Enum.Parse(typedefof<Langs>, str) :?> Langs
    with e ->
        Langs.En

在寓言中我得到以下错误:

error FABLE: Cannot resolve System.Enum.Parse

有没有一种方法可以在不使用匹配语句或其他查询的情况下进行转换?

谢谢

简短的回答是:不,fable 不能这样做。

Fable 可以编译大部分 F#,但很少编译 .Net BCL(基础 Class 库)。

不过,您可能会对 StringEnum attribute 感兴趣以解决您的特定问题。