discriminated-union

Use of the and keyword in F# in discriminated unions

大城市里の小女人 提交于 2019-12-04 16:21:05
问题 I was faced today with the following DUs declarations: type Grammar = Definition list and Definition = Def of string * Expression and Range = | Char of char | Range of char * char Why would one use the keyword and instead of type , here? 回答1: The and is needed for the definitions of Grammar and Definition to compile correctly. The Grammar type is listed first but depends on the type Definition which is defined later. In order to compile properly it must be linked with and which tells the F#

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

痞子三分冷 提交于 2019-12-04 15:25:21
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 underlyingValue let asCase2 = BinaryCase2 underlyingValue let shapeName = match asCase1 with | BinaryCase1 (x,y)

Kotlin and discriminated unions (sum types)

假装没事ソ 提交于 2019-12-04 09:52:12
问题 Does Kotlin have anything like discriminated unions (sum types)? What would be the idiomatic Kotlin translation of this (F#): type OrderMessage = | New of Id: int * Quantity: int | Cancel of Id: int let handleMessage msg = match msg with | New(id, qty) -> handleNew id qty | Cancel(id) -> handleCxl id 回答1: The common way of implementing this kind of abstraction in an OO-language (e.g. Kotlin or Scala) would be to through inheritance: open class OrderMessage private () { // private constructor

Concise pattern match on single case discriminated union in F#

雨燕双飞 提交于 2019-12-03 08:17:04
问题 Say I have the following single case discriminated union: type OrderId = OrderId of string At some point I need the actual string. The way I've found for extracting it is: let id = match orderId with OrderId x -> x Is there a more concise way of doing this? I understand that my use is a special case and the match makes sense in order to make sure you've covered the possibilities, just wondering if there's a way of doing something like: let OrderId id = orderId 回答1: You're almost there.

When to use a Discriminate Union vs Record Type in F#

淺唱寂寞╮ 提交于 2019-12-03 07:43:45
问题 I am trying to get the basics of F# clear before moving on to complex examples. The material I'm learning has introduced both Discriminate Unions and Record types. I have reviewed the material for both, but it is still unclear to me why we would use one over the other. Most of the toy examples I have created seem to be implementable in both. Records seem to be very close to what I think of as an object in C#, but I am trying to avoid relying on mapping to c# as a way to understand F# So...

Kotlin and discriminated unions (sum types)

江枫思渺然 提交于 2019-12-03 04:15:12
Does Kotlin have anything like discriminated unions (sum types)? What would be the idiomatic Kotlin translation of this (F#): type OrderMessage = | New of Id: int * Quantity: int | Cancel of Id: int let handleMessage msg = match msg with | New(id, qty) -> handleNew id qty | Cancel(id) -> handleCxl id The common way of implementing this kind of abstraction in an OO-language (e.g. Kotlin or Scala) would be to through inheritance: open class OrderMessage private () { // private constructor to prevent creating more subclasses outside class New(val id: Int, val quantity: Int) : OrderMessage() class

When to use a Discriminate Union vs Record Type in F#

回眸只為那壹抹淺笑 提交于 2019-12-02 20:28:34
I am trying to get the basics of F# clear before moving on to complex examples. The material I'm learning has introduced both Discriminate Unions and Record types. I have reviewed the material for both, but it is still unclear to me why we would use one over the other. Most of the toy examples I have created seem to be implementable in both. Records seem to be very close to what I think of as an object in C#, but I am trying to avoid relying on mapping to c# as a way to understand F# So... Are there clear reason to use one over the other? Are there certain canonical cases where one applies?

Generic F# function: How to get the Type of an F# Discriminated Union?

前提是你 提交于 2019-12-01 06:52:22
Code example: http://www.tryfsharp.org/create/dutts/Generics.fsx I have some mapping code in my F# which takes a C# object and wraps it in a discriminated union. module MyModule = type MappedThings = | DoThings of External.Things.DoThings type MappedStuff = | DoStuff of External.Stuff.DoStuff As I always use the same name in my discriminated union as the external object I would like to try to make my mapping code generic for scalability. This is what I've tried so far: let toDomain<'T> external : 'T = let found = FSharpType.GetUnionCases(typeof<'T>) |> Seq.where (fun t -> t.Name = external

Printing F# discriminated union

孤者浪人 提交于 2019-11-30 20:16:21
I am writing a F# program which parses a string into a AST type which is a discriminated union. When I use fsi (on Mono + Mac OS X) to run my code, the AST is printed out in a nice format. But when I use printfn "%s" <| ast.ToString() I get something like FSI_0002.Absyn+clazz . Writing a ToString method for all the discriminated union types would be a big chore. How do I make the value print the way fsi does it? Have you tried printfn "%A" ast ? The %A specifier takes into consideration the StructuredFormatDisplayAttribute [MSDN] , if present. Kwang Yul Seo To convert a discriminated union

Narrowing a return type from a generic, discriminated union in TypeScript

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:18:35
I have a class method which accepts a single argument as a string and returns an object which has the matching type property. This method is used to narrow a discriminated union type down, and guarantees that the returned object will always be of the particular narrowed type which has the provided type discriminate value. I'm trying to provide a type signature for this method that will correctly narrow the type down from a generic param, but nothing I try narrows it down from the discriminated union without the user explicitly providing the type it should be narrowed down to. That works, but