Skip to content

Instantly share code, notes, and snippets.

@mavnn
Last active December 18, 2015 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavnn/5698844 to your computer and use it in GitHub Desktop.
Save mavnn/5698844 to your computer and use it in GitHub Desktop.
DuckTyping in F#
type DeliveryResult =
| Success of SuccessfulDelivery
| Bounce of Bounce
let TopicTest = Regex(@"Some relevant regex here", RegexOptions.Compiled)
let inline TryExtractTopic record =
let envId = ( ^a : (member EnvId : string) record)
match TopicTest.IsMatch(envId) with
| false -> None
| true ->
Some <| TopicTest.Match(envId).Groups.["topic"].Value
let inline TryPublish<'a when 'a : (member EnvId : string)> (record : 'a) =
match TryExtractTopic record with
| Some topic ->
use chan = bus.OpenPublishChannel()
chan.Publish<'a>(record, fun x -> x.WithTopic(topic) |> ignore)
| None -> ()
let Publish record =
match record with
| Bounce b ->
TryPublish b
| Success s ->
TryPublish s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment