# Defining a zip code string

I'm following this

let StringOfLengthConstructor<'c> (input:string, length:int, defaultConstructor:string->'c) =
match input with
| null -> None
| x when x.Length = length -> Some(defaultConstructor(input))
| _ -> None
type String3 = | String3 of string
let String3 input = StringOfLengthConstructor<String3>(input, 3, String3)
type String4 = | String4 of string
let String4 input= StringOfLengthConstructor<String4>(input, 4, String4)

type String5 = | String5 of string
let String5 input = StringOfLengthConstructor<String5>(input, 5, String5)
type String6 = | String6 of string
let String6 input = StringOfLengthConstructor<String6>(input,6, String6)


and am trying to make more of these with hopefully less repetition. Can this be done without having to repeat type String3 =| String3 of string followed by this?

let String3 input = StringOfLengthConstructor<String3>(input, 3, String3)


See all the repetition? For the purpose of defining a zip code, a US zip code could be defined as

type ZipCode = | Us of String5


or

type ZipCode = | Us of String5*(String4 option) | Canadian of String3*String3

• Could you explain what are you trying to do and why? – svick Jun 19 '14 at 17:22
• To make life easier for reviewers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question – Simon Forsberg Jun 19 '14 at 18:00
• the difference is that the type String3 = | String 3 of string together with their smart constructor (the let String 3 looks to me like a quer try to make some) will be type-checked - and as it is you have to repeat those definitions for the compiler (I don't think we will ever get dependent-types in F#/.net ;) ) – Carsten Jul 4 '14 at 8:38

The solution can be made cleaner by re-arranging the order of the arguments and using pointfree style.

let StringOfLengthConstructor<'c> (length : int) (defaultConstructor : string -> 'c) (input : string) =
match input with
| x when x <> null && x.Length = length -> Some (defaultConstructor input)
| _ -> None

type String3 = String3 of string
let String3 = StringOfLengthConstructor 3 String3


However, I don't think that String5 * (String4 option) is a good definition of a zip code. The code should be validating on more than just the string length.

• for sure I don't think this is great validation for a zip code, it's more just the stubs of making sure I can do what I want to do at a higher level first – Maslow Jun 24 '14 at 13:05

Concerning the smart constructor thing I mentioned - I would implement the types like this:

module SafeLengthStrings =

let private assertLength (length : int) (input : string) : string =
if input.Length <> length
then failwith (sprintf "expected string of length %d but got %s" length input)
else input

type String3 = private Str3 of string
let string3 : (string -> String3) =
assertLength 3 >> Str3


This way you cannot construct malformed String3 instances at runtime (minus Reflection of course)

• yeah I see the difference, and the leaking abstraction of doing it the other way. I'm trying to walk down the exception-less ideals of some articles I've been reading lately and see where it takes me. – Maslow Jul 4 '14 at 20:57
• yeah why not - in this case I would think about getting the maybe-Monad in there – Carsten Jul 5 '14 at 6:18