/fsharp5-net5p6-6.fsx Secret
Last active
August 30, 2020 23:07
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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