Last active
April 15, 2019 17:35
-
-
Save jfrank-summit/2a692b473596368f529a1715f499846d to your computer and use it in GitHub Desktop.
F# Signature File - Constrained Types
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
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 |
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
module StringConstraints | |
val stringPattern : string -> string -> Result<string, string> | |
//[<Sealed>] | |
type EmailAddress = | |
new : string -> EmailAddress | |
member Value : Result<string, string> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your approach seems to make most sense to me! @tamizhvendan