discriminated-union

TypeScript: Conditional type array of union type distribution

本秂侑毒 提交于 2019-12-10 20:24:22
问题 I have a conditional type that uses a generic type T to determine an Array<T> type. As a contrived example: type X<T> = T extends string ? Array<T> : never; The issue I am having is when I provide a union type, it is being distributed as a union of 2 array types instead of an array of my union type. // compiler complains because it expects Array<'one'> | Array<'two'> const y: X<'one' | 'two'> = ['one', 'two']; Is there a way to type this such that my conditional type produces an Array<'one' |

Wildcard for type when matching discriminated unions

随声附和 提交于 2019-12-10 20:03:58
问题 In the following real world example I do a match: type Style = Nice | Cool | Ugly type Color = Blue | Yellow | Orange | Grey | Cyan type ClothingProperties = Style * Color type Clothes = | Jeans of ClothingProperties | Pullover of ClothingProperties | Shirt of ClothingProperties type Person = | Person of string * Clothes let team = [Person("Jan", Jeans (Cool, Blue)); Person("Pete", Shirt (Nice, Cyan)); Person("Harry", Pullover (Ugly, Grey))] let matchPerson person= match person with | Person

Passing discriminated unions to InlineData attributes

爷,独闯天下 提交于 2019-12-10 17:37:16
问题 I am trying to unit test a parser that parses a string and returns the corresponding abstract syntax tree (represented as a discriminated union). I figured it would be pretty compact to use Xunit.Extensions' attribute InlineData to stack all test cases on one another: [<Theory>] [<InlineData("1 +1 ", Binary(Literal(Number(1.0)), Add, Literal(Number(1.0))))>] ... let ``parsed string matches the expected result`` () = However, compiler complains that the second argument is not a literal

Shared cases in F# discriminated unions

馋奶兔 提交于 2019-12-10 13:34:42
问题 I want to write something like this: type NumExp = Num of float type Exp = | Num of float | Dot of NumExp * NumExp | Op of string * Exp * Exp let getValue (Num(n) : NumExp) = n The compiler complains about a conflict between NumExp and Exp in getValue . Even the following fails: let getValue (nn : NumExp) = match nn with | Num(n) -> n Is there a way to use the same case in both discriminated unions that works with functions? The DU definitions themselves are OK. I want to use the same case to

F# limitations of discriminated unions

狂风中的少年 提交于 2019-12-10 10:16:30
问题 I am trying to port a small compiler from C# to F# to take advantage of features like pattern matching and discriminated unions. Currently, I am modeling the AST using a pattern based on System.Linq.Expressions: A an abstract base "Expression" class, derived classes for each expression type, and a NodeType enum allowing for switching on expressions without lots of casting. I had hoped to greatly reduce this using an F# discriminated union, but I've run into several seeming limitations: Forced

F# - Can I return a discriminated union from a function

半城伤御伤魂 提交于 2019-12-10 01:50:06
问题 I have the following types: type GoodResource = { Id:int; Field1:string } type ErrorResource = { StatusCode:int; Description:string } I have the following discriminated union: type ProcessingResult = | Good of GoodResource | Error of ErrorResource Then want to have a function that will have a return type of the discriminated union ProcessingResult: let SampleProcessingFunction value = match value with | "GoodScenario" -> { Id = 123; Field1 = "field1data" } | _ -> { StatusCode = 456;

F#: combining together discriminated unions and class hierarchies?

江枫思渺然 提交于 2019-12-09 20:05:43
问题 Let's say I have a significant class hierarchy: Tag ControlFlowTag IfTag ForTag JumpTag HTMLTag DivTag and I want to make a list interspersed with these and strings. let MyList = [tagA, tagB, "some text", tagC] and I thought I could discriminated union it type Node = | Tag of Tag | String of String let MyList: list<Node> = [tagA, tagB, "some text", tagC] but alas, it doesn't work without let MyList: list<Node> = [Tag tagA, Tag tagB, String "some text", Tag tagC] Obviously the Tag and String

How to fold over a discriminated union

六眼飞鱼酱① 提交于 2019-12-07 23:51:28
问题 I'm attempting to implement a fold over a discriminated union. The DU is called Expr, and represents a program expression, and is often recursive. I'm attempting to write a fold that accumulates the result of an operation over the Exprs recursively. Below is my attempt to write the fold. let rec foldProceduralExpr (folder : 's -> Expr list -> 's) (state : 's) (expr : Expr) : 's = let children = match expr with | Series s -> s.SerExprs | Lambda l -> [l.LamBody; l.LamPre; l.LamPost] | Attempt a

How to create a Union type in F# that is a value type?

£可爱£侵袭症+ 提交于 2019-12-07 17:31:17
问题 Normal F# Discriminated Unions are reference types. How can I create a simple (non-recursive and with only value-type fields) union type in F# that is a value type? Based on some internet searching my current (non-working) attempt looks as follows: [<StructLayout(LayoutKind.Explicit)>] type Float = [<DefaultValue>] [<FieldOffset 0>] val mutable Val1 : float [<DefaultValue>] [<FieldOffset 0>] val mutable Int1 : int new (a:float) = {Val1 = a} The following blog post appears to show what is

Comparing Discriminated Unions

◇◆丶佛笑我妖孽 提交于 2019-12-07 06:13:37
问题 I'm a newbie to F# and I'm playing around with FParsec. I would use FParsec to generate an AST. I would like to use FsUnit to write some tests around the various parts of the parser to ensure correct operation. I'm having a bit of trouble with the syntax (sorry, the exact code is at work, I can post a specific example later) so how exactly could one compare two discriminated unions (one the expected, the other the actual result)? Could someone provide a tiny code example using FsUnit (or