DEV Community

Discussion on: First Impressions of F#

Collapse
onpikono profile image
Ondrej Pinka

As for the functions / values without type signatures, an IDE (VS Code + Ionide extension, or Visual Studio) can display them for you.

It is interesting that something you find confusing (how compiler infers function's type based on which "variant" is used first) I find really clear and actually very helpful. I am a total beginner but I already feel like I am in control of my own code, I get to decide what I want the function to do with what kind of data (and only that kind of data). Or, if I am are reading code after someone, I have better idea what kind of things the function might do, or what value I can pass it by reading the function's name and type signature (what type of inputs it consumes and what type of data it gives back). It may seem very restrictive at first but I actually find it very liberating - I don't need to keep in my head all the nuances of what can go wrong when I use a particular function in particular context, or also spend long minutes trying to find out what went wrong and where (eg. I accidentally added int and string, which may have resulted into casting int to string and then string concatenation instead of integer addition). In F# the compiler will help me by not allowing me to do just that. For that matter, if I still want a function to be more "generic", I can use keyword inline in the function definition and compiler will try to infer function's type at each place it is used, instead of just once at its first call.

For example, if I read unit sale price and quantity of units sold from a database, I can make sure (by defining custom types, such as Qty of float and UnitPrice of float and then by specifying those in the function's signature) that my totalSales function will accept only those two types of values and no other - I cannot accidentally pass in two quantities and multiply them (that makes no sense, it would be a bug), or any other numerical value. The compiler would stop me and demand the correct type of values.

Another example, F# allows to define types as units of measure. So you can further improve your code to prevent mistakes - it makes no sense to sum litres, kilograms, pieces and cubic meters. You can refine the Quantity type further and then create functions which ensure nowhere in the codebase you / someone else adds wrong quantities by mistake. Because if function works for litres only and you pass it a value holding kgs, the program will not compile. If you still want to sum kgs and litres, you need to create a function doing the conversion from kgs to litres (using density) and then use the returned value in your sum litres function.

I hope this makes sense and that you find it helpful or interesting :)

Collapse
dewofyouryouth_43 profile image
Jacob E. Shore Author • Edited on

Like I've said, I'm not confused about why you can't use the same function to add and concatenate. I just prefer being able to tell what the function does just by looking at the definition. I would rather just assign types to the function itself.