Skip to content

Instantly share code, notes, and snippets.

@jfrank-summit
Last active April 15, 2019 17:35
Show Gist options
  • Save jfrank-summit/2a692b473596368f529a1715f499846d to your computer and use it in GitHub Desktop.
Save jfrank-summit/2a692b473596368f529a1715f499846d to your computer and use it in GitHub Desktop.
F# Signature File - Constrained Types
module StringConstraints
open System
let stringPattern str pattern =
if String.IsNullOrEmpty(str) then
let msg = sprintf "%s: Must not be null or empty" str
Error msg
elif System.Text.RegularExpressions.Regex.IsMatch(str, pattern) then
Ok (str)
else
let msg = sprintf "'%s' must match the pattern '%s'" str pattern
Error msg
type EmailAddress (emailAddress:string) =
member x.Value =
let pattern = @"^\S+@\S+\.\S+$"
stringPattern emailAddress pattern
module StringConstraints
val stringPattern : string -> string -> Result<string, string>
//[<Sealed>]
type EmailAddress =
new : string -> EmailAddress
member Value : Result<string, string>
@jfrank-summit
Copy link
Author

your approach seems to make most sense to me! @tamizhvendan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment