Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fable convert string to enum without match or lookup

Tags:

f#

fable-f#

In Fsharp you can convert string to enum link follows:

type Langs = 
    | En = 0
    | Afr = 1

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

In fable I get the following error:

error FABLE: Cannot resolve System.Enum.Parse

Is there a way to do the conversion without having to use a match statement or some other lookup?

Thanks

like image 377
onemorecupofcoffee Avatar asked Jan 29 '26 09:01

onemorecupofcoffee


1 Answers

The short answer is: No, fable cannot do this.

Fable can compile most of F#, but very little of .Net BCL (Base Class Library).

You might, however, be interested in StringEnum attribute to solve your particular problem.

like image 101
nilekirk Avatar answered Feb 02 '26 12:02

nilekirk