discriminated-union

F# comparing discriminated unions' by case identifier

﹥>﹥吖頭↗ 提交于 2019-12-23 15:43:20
问题 Is there a way to compare discriminated unions by their case-identifiers in F#? type MyUnion = | MyString of string | MyInt of int let x = MyString("hello") let y = MyString("bye") let z = MyInt(25) let compareCases a b = // compareCases x y = true // compareCases x z = false // compareCases y z = false How do I implement compareCases function in a generic way? I.e. something like the following, but more generic (reflection is ok): let compareCases a b = match a with | MyString(_) -> match b

F# List of Union Types

拥有回忆 提交于 2019-12-23 12:26:52
问题 I want a list of Reports. Report can be either Detail or Section types. module Data type Section = { Header: string; Lines: string list; Total: string } type Detail = { State: string; Divisions: string list; Sections: Section list } type Summary = { State: string; Office: string; Sections: Section list } type Report = Detail | Summary Then in my code, I'd like to do the following: let mutable (reports:Report list) = [] ... reports <- detail::reports // or reports <- summary::reports The

Union types in Java

◇◆丶佛笑我妖孽 提交于 2019-12-23 07:47:09
问题 I've been working with C# for a while and trying to get more familiar with Java. So I'm trying to migrate some of the basic patterns I use on daily basis in C# even only to understand the gap between JVM and dotnet and figure out how to deal with them. Here is the first problem I encountered - an option type - somethiong which is quite easy to achieve in many languages i.e. Koltlin: sealed class Option<out T : Any> { object None : Option<Nothing>() data class Some<out T : Any>(val value: T) :

Can you encapsulate multi case discriminated unions?

蓝咒 提交于 2019-12-23 01:02:36
问题 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 -

Comparing F# discriminated union instances via pattern matching

痞子三分冷 提交于 2019-12-21 13:59:12
问题 Firstly, apologies for the poor title - I don't understand enough F# to describe the problem better. Consider this simple DU: type Money = | USD of decimal | GBP of decimal | EUR of decimal static member (+) (first: Money, second: Money) = match first, second with | USD(x), USD(y) -> USD(x + y) | GBP(x), GBP(y) -> GBP(x + y) | EUR(x), EUR(y) -> EUR(x + y) | _ -> failwith "Different currencies" I'm representing money in different currencies, and overloading the (+) operator so that I can

F# discriminated union syntax clarification

半城伤御伤魂 提交于 2019-12-20 01:13:52
问题 I'm reading Expert F# 4.0 and at some point (p.93) the following syntax is introduced for list : type 'T list = | ([]) | (::) of 'T * 'T list Although I understand conceptually what's going on here, I do not understand the syntax. Apparently you can put [] or :: between parentheses and they mean something special. Other symbols aren't allowed, for example (++) or (||) . So what's going on here? And another thing is the 'operator' nature of (::) . Suppose I have the following (weird) type:

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

烂漫一生 提交于 2019-12-19 08:30:41
问题 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>

TypeScript: derive map from discriminated union

ぐ巨炮叔叔 提交于 2019-12-18 17:02:11
问题 I have a discriminated union type that differentiates types based on a string literal field. I would like to derive a mapped type that maps all of the types in the union to their corresponding discriminator literal values. e.g. export type Fetch = { type: 'fetch', dataType: string }; export type Fetched<T> = { type: 'fetched', value: T }; // union type discriminated on 'type' property export type Action = | Fetch | Fetched<Product>; // This produces a type 'fetch' | 'fetched' // from the type

Read from an enum without pattern matching

拈花ヽ惹草 提交于 2019-12-18 09:08:44
问题 The Rust documentation gives this example where we have an instance of Result<T, E> named some_value : match some_value { Ok(value) => println!("got a value: {}", value), Err(_) => println!("an error occurred"), } Is there any way to read from some_value without pattern matching? What about without even checking the type of the contents at runtime? Perhaps we somehow know with absolute certainty what type is contained or perhaps we're just being a bad programmer. In either case, I'm just

Scrap Your Boilerplate in f#

断了今生、忘了曾经 提交于 2019-12-17 22:44:27
问题 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? 回答1: Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?) Here's the code for a reflection-based version