4 ms·
i am not familiar with fstar. But I will try to provide some context badly so someone who does know it can correct me. Javascript is dynamically typed, and so
by Multicomp 6y ago
i am not familiar with fstar. But I will try to provide some context badly so someone who does know it can correct me.
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!
- jen20 6y agoThe 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-more-semantic-types/#modeling-constrained-strings-with-types https://fsharpforfunandprofit.com/posts/designing-with-types...
- globuous 6y agoOcaml 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 :)
- octachron 6y agoThe 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.