0 people like it.

Alternative to using spaces in discriminated union cases

This shows an alternative to using spaces in the discriminated union cases, which makes it harder to write in code, and tends to be needed when interoping with the likes of WPF or any other element that uses the ToString of the union case to display its value. Also useful as an alternative to using special characters like á or ç which make it harder to work with colleagues that don't have keyboards with those characters. Uses code from snippet http://fssnip.net/9l

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open Microsoft.FSharp.Reflection

let toString (x:'a) = 
    match FSharpValue.GetUnionFields(x, typeof<'a>) with
    | case, _ -> case.Name

type SpecialCharactersUnion = 
    | Ola
    | Nao
    | RegularCharactersButWithSpaces
    | Normal
    override this.ToString() =
        match this with
        | Ola -> "Olá"
        | Nao -> "Não"
        | RegularCharactersButWithSpaces -> "Regular Characters But With Spaces"
        | _ -> toString this

//Usage:
// Everytime a union case tostring gets called, the special cases will override it and present a string with the special characters.
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
val toString : x:'a -> string

Full name: Script.toString
val x : 'a
type FSharpValue =
  static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetRecordField : record:obj * info:PropertyInfo -> obj
  static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
  static member GetTupleField : tuple:obj * index:int -> obj
  static member GetTupleFields : tuple:obj -> obj []
  static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
  static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
  static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
  static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
  static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
  ...

Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.GetUnionFields : value:obj * unionType:System.Type * ?allowAccessToPrivateRepresentation:bool -> UnionCaseInfo * obj []
static member FSharpValue.GetUnionFields : value:obj * unionType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> UnionCaseInfo * obj []
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val case : UnionCaseInfo
property UnionCaseInfo.Name: string
type SpecialCharactersUnion =
  | Ola
  | Nao
  | RegularCharactersButWithSpaces
  | Normal
  override ToString : unit -> string

Full name: Script.SpecialCharactersUnion
union case SpecialCharactersUnion.Ola: SpecialCharactersUnion
union case SpecialCharactersUnion.Nao: SpecialCharactersUnion
union case SpecialCharactersUnion.RegularCharactersButWithSpaces: SpecialCharactersUnion
union case SpecialCharactersUnion.Normal: SpecialCharactersUnion
val this : SpecialCharactersUnion
override SpecialCharactersUnion.ToString : unit -> string

Full name: Script.SpecialCharactersUnion.ToString
Raw view Test code New version

More information

Link:http://fssnip.net/7VN
Posted:5 years ago
Author:Micael Morais
Tags: discriminated union