How to take the results of a function that returns an object and pass it to a discriminatory union in F #?
Problem scenario, I am working with javascript executor on webdriver in selenium. The docs indicate that the output should be an object of type certian or type list. (Ref https://www.w3.org/TR/webdriver/#executing-script )
I would like to give the returning object some structure, translating it into a discriminated union, so that later it can be compared.
Casting does not work directly, and it is not allowed to create constructors for union types, so I cannot drop them exactly. What is the correct way to do this?
type JsResult =
| E of IWebElement
| I of Int64
| B of bool
| S of String
| LE of IWebElement list
| LI of Int64 list
| LB of bool list
| LS of String list
| N of Object
override self.ToString () =
match self with
| E e -> e.Text
| I i -> i.ToString()
| B b -> b.ToString()
| S s -> s
| LE le -> String.concat " " (le |> Seq.map(fun x-> x.Text))
| LI li -> String.concat " " (li |> Seq.map(fun x-> x.ToString()))
| LB lb -> String.concat " " (lb |> Seq.map(fun x-> x.ToString()))
| LS ls -> String.concat " " ls
| N _ -> String.Empty
let execute script : JsResult = (browser :?> IJavaScriptExecutor).ExecuteScript(script) :> JsResult
Yojin source
share