discriminated-union

What is the Enum.GetName equivalent for F# union member?

I want to get the equivalent of Enum.GetName for an F# discriminated union member. Calling ToString() gives me TypeName+MemberName, which isn't exactly what I want. I could substring it, of course, but is it safe? Or perhaps there's a better way? ...

DIscriminated Union & let binding?

Why are let bindings not permitted in a discriminated union? I assume it has to do with let bindings being executed in a default constructor? On a secondary note any suggestions on how I could rewrite AI_Choose would be appreciated. I want to keep the weighted priority in a tuple with the AI. My idea is to have AI_Weighted_Priority inh...

Optimal representation of expressions in F#

I'm working on a library to generate SQL from LINQ expressions (basically a modified subset of LINQ-to-SQL). I'm using discriminated unions to model the SQL expressions, but have encountered some (perceived?) limitations. I want to do something like the following (note the last line): type SqlSourceExpression = | Table of string ...

Getting a typed array from a discriminate union of different types of arrays in F#

If I have a discriminate union of different types of arrays how can i convert them to their 'actual' types? type ItemStuff = | Colors of string[] | Sizes of int[] let foo = Sizes [|1;2;3|] After running the above when I get the value of foo I see: val foo : ItemStuff = Sizes [|1;2;3|] How can I get an actual array of int's...

Upgrading FParsec: upgrade discriminated unions to satisfy the new equality/comparison constraints

So, by a hilarious series of events, I downloaded the FParsec source and tried to build it. Unfortunately, it's not compatible with the new 1.9.9.9. I fixed the easy problems, but there are a couple of discriminated unions that still don't work. Specifically, Don Syme's post explains that discriminated unions containing items of type ob...

How can I duplicate the F# discriminated union type in C#?

I've created a new class called Actor which processes messages passed to it. The problem I am running into is figuring out what is the most elegant way to pass related but different messages to the Actor. My first idea is to use inheritance but it seems so bloated but it is strongly types which is a definite requirement. Have any ideas?...

How to use symbols/punctuation characters in discriminated unions

I'm trying to create a discriminated union for part of speech tags and other labels returned by a natural language parser. It's common to use either strings or enums for these in C#/Java, but discriminated unions seem more appropriate in F# because these are distinct, read-only values. In the language reference, I found that this symb...

Discriminated union in C#

[Note: This question had the original title "C (ish) style union in C#" but as Jeff's comment informed me, apparently this structure is called a 'discriminated union'] Excuse the verbosity of this question. There are a couple of similar sounding questions to mine already in SO but they seem to concentrate on the memory saving benefits ...

Shared cases in F# discriminated unions

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 wit...

Can discriminated unions refer to each other?

I'm building an expression tree using discriminated unions. The below code: type IntExpression = | TrueIsOne of BoolExpression type BoolExpression = | LessThan of IntExpression * IntExpression | And of BoolExpression * BoolExpression | Or of BoolExpression * BoolExpression | Bool of bool throws an error because Bo...

Discriminated unions and inheritance

Hi all, I'm looking to create a scene-graph for my F# project somthing like: root ->player_bob -->torch ->enemy_1 -->extravagant_hat -->enemies_cat_jess --->fleas --->fur_ball ->loot etc,etc. Each item needs to hold a collection of game objects to represent it's children. e.g enemy1's list contains a cat and a hat and the cats list...

Scrap Your Boilerplate in f#

I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language? ...

Where can I find a serializer for F# discriminated unions?

I need to persist an abstract syntax tree represented using F# discriminated unions to a human readable compact format, such as the format already used in the F# language to construct discriminated unions, that I can read back in to discriminated union instances later. I'm a bit surprised the F# library doesn't support this as it surely ...

How can I transform a large discriminated union tree to a readable form?

The following type is clearly quite large so manually writing the code to convert this to a readable form would be tedious. I would like to know the simplest way to display the tree in a readable form. type Element = | Nil | Token of Token | Expression of Element * Element | ExpressionNoIn of Element * Element | AssignmentExpression of ...

F# discriminated union picking 0 or 1 from a list

Hi all, Given a mapping program where I map from an array of strings to a discriminated union, I want to select an instance of a particular DU type. I know that there will be 0 or 1 instances. Is there a smarter way to do it than this? type thing = { One:int; Two:int; Three:int} type data = | Alpha of int | Bravo of thing ...