discriminated-union

Can you encapsulate multi case discriminated unions?

孤者浪人 提交于 2019-12-06 20:47:30
I see that you can enforce constructor usage of single-case discriminated unions, can you do the same with multi-case? for example type MemberId = | MemberId of int | MemberGuid of Guid I'm currently trying in the fsi like this val create : int -> T option val create : Guid -> T option but I'm guessing like C#, F# won't allow you to overload based on return type for the unwrap: val value : T -> string Edit --------------- MemberId.fsi = module MemberId open System type _T val createId : int -> _T option val createGuid : Guid -> _T option val value : _T -> 'a MemberId.fs = module MemberId open

How to fold over a discriminated union

蓝咒 提交于 2019-12-06 12:27:39
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 -> a.AttemptBody :: List.map (fun ab -> ab.ABBody) a.AttemptBranches | Let l -> l.LetBody :: List

Is is possible to pattern match on the underlying shape of a discriminated union?

末鹿安然 提交于 2019-12-06 11:17:13
问题 Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1

Discriminated union structural/custom equality

和自甴很熟 提交于 2019-12-06 06:20:55
I have the following discriminated union: type ActCard = Cellar of card list | Chapel of (card option * card option* card option* card option) | Smithy | Spy of (card -> bool * card -> bool) It had structural equality until I added the card -> bool to Spy . This question is helpful for how to do custom equality for records. However, I'm not sure how best to implement it in this situation. I would prefer to not have to enumerate each case in ActCard : override x.Equals(yobj) = match x, yobj with | Spy _, Spy _ -> true | Cellar cards, Cellar cards2 -> cards = cards2 (* ... etc *) What is a

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

十年热恋 提交于 2019-12-05 21:46:41
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 possible via C# I'm aware that the above is NOT idiomatic use of F# but I am trying to optimize the

F# Discriminated Union - “downcasting” to subtype

≯℡__Kan透↙ 提交于 2019-12-05 18:03:17
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 compiler complains. How do I tell the compiler that foo is of type Foo ? I have tried: let foo:Foo

Comparing Discriminated Unions

ⅰ亾dé卋堺 提交于 2019-12-05 08:53:30
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 NUnit), please? An example discriminated union (very simple) type AST = | Variable of string | Class of

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

前提是你 提交于 2019-12-05 01:56:28
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; Description = "desc" } Is what I am trying to do possible. The compiler is giving out stating that it expects

Discriminated union member methods

那年仲夏 提交于 2019-12-04 17:53:05
问题 I want to define a method shared by all members of a discriminated union. Currently I've implemented it like this, but it seems really inelegant- surely there is a better way. Suggestions? type A = {AData:string} member this.SharedMethod (x:float) : int= ... type B = {BData:float} member this.SharedMethod (x:float) : int= ... type AB = | A of A | B of B let CallSharedMethod (ab:AB) x = match ab with | AB.A(a') -> a'.SharedMethod x | AB.B(b') -> b'.SharedMethod x 回答1: What about something like

F#: combining together discriminated unions and class hierarchies?

旧街凉风 提交于 2019-12-04 17:11:40
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 described in Node are orthogonal and separate from the existing Tag/String classes. Mousing over gives