discriminated-union

Another failure at deserializing data with discriminated unions, in F#

我怕爱的太早我们不能终老 提交于 2020-06-29 03:47:31
问题 Following a question where the answer provided a working solution to serialize / deserialize discriminated unions (IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer) I have now a practical case where this fails (although it works in simpler cases). here is the test code: open System.Collections.Generic open Microsoft.FSharpLu.Json open Newtonsoft.Json open Newtonsoft.Json.Serialization // set up the serialization / deserialization based on answer from: // https:

Purpose of a single case discriminated union

江枫思渺然 提交于 2020-02-29 10:20:06
问题 I'm defining a monadic observable/reactive parser. This behaves quite differently to a normal parser as it is a continuous query. The underlying type is: IObservable<'a> -> IObservable<'b> From looking at various parser implementations in functional languages, it seems as though the more appropriate way to define things is a single case discriminated union: type Pattern<'a,'b> = Pattern of (IObservable<'a> -> IObservable<'b>) Which means I then need to extract the underlying function to use

How do I get a discriminated union case from a string?

南楼画角 提交于 2020-02-22 06:11:46
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

How do I get a discriminated union case from a string?

对着背影说爱祢 提交于 2020-02-22 06:08:12
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

TypeScript discriminated union with generics

别等时光非礼了梦想. 提交于 2020-01-24 20:50:09
问题 Is there a way to set up a discriminated union that lets you capture a specific type with each union member? I'm trying to write a type safe command handler along the lines of: interface GetUsersCommand { // returns User[] type: 'GET_USERS' } interface UpdateUserNameCommand { // returns void type: 'UPDATE_USER_NAME' userId: string name: string } type Command<Result> = GetUsersCommand | UpdateUserNameCommand class CommandExecutor { execute<Result>(command: Command<Result>): Result { switch

XML Schema for Tagged Union

荒凉一梦 提交于 2020-01-17 06:54:10
问题 I have an XML doc that includes a repeating series of addresses whose actual content depends on the value of an included enumeration. I suppose this is a type of "tagged union" [or "discriminated union"?]: { <AddressList> <Address> <AddressType type="addressEnum" fixed="CanadianAddress"/> <AddressValue> <Street type="textstring">123 Yonge Street</Street> <Province type="provinceEnum" value="Ontario"/> <PostalCode type="postalCodeType" value="M1N 2O3"/> </AddressValue> </Address> <Address>

Defining a variable as one variant of a discriminated union in TypeScript

你说的曾经没有我的故事 提交于 2020-01-06 02:22:05
问题 I have the following typescript code which uses a discriminated union to distinguish between some similar objects: interface Fish { type: 'FISH', } interface Bird { type: 'BIRD', flyingSpeed: number, } interface Ant { type: 'ANT', } type Beast = Fish | Bird | Ant function buildBeast(animal: 'FISH' | 'BIRD' | 'ANT') { const myBeast: Beast = animal === 'BIRD' ? { type: animal, flyingSpeed: 10 } : {type: animal} } In the function buildBeast it accepts a string that complies with all possible

F# Discriminated Union - “downcasting” to subtype

◇◆丶佛笑我妖孽 提交于 2020-01-02 05:27:28
问题 I don't really know what the proper title for this question should be, but: I have a discriminated union called MyDiscriminatedUnion in F# : type MyDiscriminatedUnion = | Foo of Foo | Bar of Bar where Foo and Bar are record types: type Foo = { ... } type Bar = { ... } I created a value of union type Foo : let foo = Foo { ... } The compiler tells me that foo is of type MyDiscriminatedUnion . Then I want to pass foo into a function that expects type Foo, not MyDiscriminatedUnion . Therefore the

Questions regarding C++ non-POD unions

自闭症网瘾萝莉.ら 提交于 2019-12-28 16:54:29
问题 C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active at a time, now my questions are rather simple. What is the default value of uny? - undefined? Whenever my class is destructed, which members (within the union), if any will be destructed? Suppose I have to std::typeinfo to keep track of which is the active member, should I then call the destructor

F# Convert 'a discriminated union to string

﹥>﹥吖頭↗ 提交于 2019-12-23 19:26:57
问题 I'm trying to convert a discriminated union to string but I don't understand why this code is not working. type 'a sampleType = | A of 'a | B of 'a let sampleTypeToString x = match x with | A (value) -> string value | B (value) -> string value This is the fsharp interactive output sampleTypeToString A(2);; Stopped due to error System.Exception: Operation could not be completed due to earlier error Successive arguments should be separated by spaces or tupled, and arguments involving function