Skip to content

Instantly share code, notes, and snippets.

@cartermp
Last active August 30, 2020 23:07
#r "nuget: FSharp.SystemTextJson"
open System.Text.Json
open System.Text.Json.Serialization
open System.Runtime.CompilerServices
nameof(+) // gives '+'
nameof op_Addition // gives 'op_Addition'
type C<'TType> =
member _.TypeName = nameof<'TType> // Nameof with a generic type parameter via 'nameof<>'
/// Simplified version of EventStore's API
[<Struct; IsByRefLike>]
type RecordedEvent = { EventType: string; Data: ReadOnlySpan<byte> }
/// My concrete type:
type MyEvent =
| AData of int
| BData of string
// use 'nameof' instead of the string literal in the match expression
let deserialize (e: RecordedEvent) : MyEvent =
match e.EventType with
| nameof AData -> AData (JsonSerializer.Deserialize<int> e.Data)
| nameof BData -> BData (JsonSerializer.Deserialize<string> e.Data)
| t -> failwithf "Invalid EventType: %s" t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment