The only difference between F# and C# with respect to the notion of a "type which represents a string of length 15" is the amount of code to do it - as described by Scott Wlaschin [1]. You could do the equivalent in C# too. I don't know if this is also the case in Ocaml, though.
[1]: https://fsharpforfunandprofit.com/posts/designing-with-types...
Re:Verified Programming in F*: A Tutorial
by globuous on Monday January 04, 2021 @03:08PM
(#25637150)
Ocaml beginner here, didn't know you could restrict an Ocaml type to a string of a given length !! How would you go about that ?? Cheers :)
Re:Verified Programming in F*: A Tutorial
by octachron on Tuesday January 05, 2021 @09:09AM
(#25645629)
The easiest way is probably to define a private subtype of string (type small_string = private string) with smart constructors that enforces the restriction at runtime. This often means that creating such values may fail, but if you have a value of this type you are assured that the constraint on the size holds.
Javascript is dynamically typed, and so has no type checking until runtime, you don't know whether a passed in parameter is a string or not until you try to use it.
C# is statically typed, so the compiler knows whether something is a string when passed in at compile time, not making you wait until runtime.
Ocaml/F# have a stronger type system than c#, so you get the compiler looking for you not only that something is a string, but that it is a named type like fifteenString, which is a string with a length of always 15 characters.
F* takes this even further, where the compiler checks for you that you've passed in a fifteenString and that it matches criteria in its value like "starts with hello" or "does not contain bye".
C# saves you from needing to check if a param is even a astring at runtime.
F# additionally saves you from needing to check if it is null and 15 characters in length at runtime.
F* additionally saves you needing to check its value starts with hello at runtime.
Again this is a single language feature probably poorly explained!