In the context of a library to generate random credit card, it would be nice to let the user the possibility to set some optional options on some cards. Lets take Visa. Two lengths are available: 13 or 16 digits.
In C# a common implementation would be:
enum VisaLengthOptions
{
Thirteen,
Sixteen
}
public static string GenerateVisa(VisaLengthOptions length = VisaLengthOptions.Sixteen) {
if (length == VisaLengthOptions.Thirteen) return "4000000000001";
if (length == VisaLengthOptions.Sixteen) return "4000000000000001";
return "";
}
Since F# does not support optional parameter (for currying and simplicity reason (note that it could be done since ocaml did it)). What would be the most idiomatic way to write this in F#?
Here is a guess:
type VisaLengthOptions =
| Thirteen = 0
| Sixteen = 1
let constructVisa length =
match length with
| VisaLengthOptions.Thirteen -> "4000000000001"
| VisaLengthOptions.Sixteen -> "4000000000000001"
| _ -> ""
let generateVisa () = constructVisa VisaLengthOptions.Sixteen
So there are no optional parameter and have two functions cant have the same name even with different signatures. What wrong with this snippet?
We want our library to have a Truly Discoverable API. So we would like to avoid having two different name for basically the same logic. Should we follow the map
and map2
and goes generateVisa and generateVisa2? or like map
and mapi
and goes generateVisa and generateVisaL? Is there a one true F# way?
A good example in the .net world is Split.