Skip to content

Instantly share code, notes, and snippets.

@Savelenko
Created June 29, 2022 13:37
Show Gist options
  • Save Savelenko/49b8417c9d85953056f64162bc9c6dd6 to your computer and use it in GitHub Desktop.
Save Savelenko/49b8417c9d85953056f64162bc9c6dd6 to your computer and use it in GitHub Desktop.
Data format selection with static abstract member-based encoder
type IEncodable<'t> =
static abstract Encode : 't -> byte[]
/// Selects Base64 encoding for `a`.
type Base64<'a when 'a :> IEncodable<'a>> = Base64 of 'a with
interface IEncodable<Base64<'a>> with
member this.Encode (Base64 a) =
let toBase64 = failwith "..."
toBase64 ('a.Encode a)
// Default encoding
let bytes = string.Encode "Hello"
// Base64 encoding
let byte64 = Base64<_>.Encode (Base64 "Hello") // Hypothetical syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment