Last active
December 18, 2015 00:48
-
-
Save mavnn/5698844 to your computer and use it in GitHub Desktop.
DuckTyping in F#
This file contains hidden or 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
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