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
type String3 = | String 3 of string
together with their smart constructor (thelet 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 ;) ) \$\endgroup\$ – Carsten Jul 4 '14 at 8:38