This works great with "makes illegal states unrepresentable" approach. It helps to reduce a need for boring unit tests and lets you focus more on expressing domain in code directly.
I kind of can do it for Strings by doing something like this
type ValidStrings = 'name' | age | 'dob';
The easy way around this is a function to determine this function greaterThan2(num) return num > 2;
but it'd be cool to express this level of dynamism as a type.Smart constructor technique works well with 'parse, don't validate' approach [0]. You can push type construction to the boundries of your system so that you can work on a domain code with more precise types. It's not always so rosy however as too much types can become a burden.
[0] https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...
(* GreaterThanTwo.fsi *)
type t
val make : int -> t option
val to_int : t -> int
(* GreaterThanTwo.fs *)
type t = Value of int
let make int = if int > 2 then Some (Value int) else None
let to_int (Value int) = int
Now, `GreaterThanTwo.make x` gives you a value of type `GreaterThanTwo.t option`, meaning it can be `Some` value or `None`. If it's `Some` value, that means it really does contain an int greater than two and you can get that int using `GreaterThanTwo.to_int`.I saw in a different HN post a few weeks ago that jet.com heavily used F# but obviously that's no more.
If a company were using F# and I were looking to switch and all the other stuff was a good match, the language choice would be a big pull for me.
While I use and am learning F# for a side project, there's no substitute for working with one professionally and shipping production code with it. I wish it'd be possible to convince my engineering group to use it.
(If you're interested in quant finance in London, I work at G-Research which uses F#; I write almost solely in F# there. You can contact me privately if you like at [email protected].)
More broadly there seems to be a big push for updated (perhaps even modern) versions of established languages and I think this is an incredible development to witness.
Rust, Go, Typescript, Scala, Kotlin are all the rage and seem to replace C/C++, javascript, and java. Others might exist for either side as well, this is just off the top of my head. From what I see and read as well functional programming seems to be a lot more popular and a lot of these languages are either built for it or with a good base of support for it. It'd be really interesting to have some hard data to know if this is accurate or just my perception.
My only other exposure to functional programming was an undergrad class required for a CS major (thank gosh it was a requirement too). It used Scheme for the language and Structure and Interpretation of Computer programs for the text book[1]. Looking back, this was the most useful class I took and the ideas have stuck and continue to inform me today. Its the one text book I've gone back to on occasion just to take a look and I've found some versions of it for JS and F# even on github which I use to help learn the language or review the concepts in languages I'm more familiar with.
[1] https://mitpress.mit.edu/sites/default/files/sicp/full-text/...
I really love the ML-family type systems. They're really great aides to programming, and makes planning really nice. F# has that, plus that usability from C# and TS. It's also, like you said, got a really beautiful syntax IMHO.
I keep coming back to Rust though, because Rust is more general purpose, more performant, and applies better to the stuff I like to work on for my hobby projects. I'd love to use F# more but can't find a reason to at the moment.
TypeScript, Python and Rust have become my comfort languages, but I like Rust the best, because it's got a lot of really nice features from ML languages and a powerful and safe type system.